Home>Article>Web Front-end> What to do if node gm reports an error
Node gm error solution: 1. Install "ImageMagick 6.x"; 2. Add the ImageMagick installation directory to the environment directory Path, and modify the code to "require('gm').subClass({ imageMagick: true});" That's it.
The operating environment of this tutorial: linux5.9.8 system, node-v16.18.0 version, DELL G3 computer
node gm error what to do?
A collection of various problem solutions about nodejs gm (Chinese garbled characters, non-conforming drawing, ������)
1. Chinese garbled characters
graphicsmagick does not support Chinese font drawing (undetermined)
The most direct and effective method at present is to replace ImageMagick (both functions Exactly the same, no need to worry)
First install ImageMagick 6.x
(For 7.x, please check the "Install legacy utilities (e.g. convert)" option during installation)
Then, add the ImageMagick installation directory to the environment directory Path.
And make the following changes to your code:
const gm = require('gm')//原代码 const gm = require('gm').subClass({imageMagick: true});//修改后的代码
Next, check whether you have set theChinese font, and whether theencoding has been switched to Unicode.
For Chinese fonts, I recommend using Google's "Google Noto", which is supported in basically any language.
Example:
const gm = require('gm').subClass({imageMagick: true}); var text = "你好!ImageMagick!"; var img = gm(800, 2000, "#ffffffff");//创建一个800x2000的图片 img.font("./Noto.ttf",36);//设置字体,字体大小 img.encoding('Unicode');//设置编码为Unicode img.drawText(0, 50, "'" + text + "'");//此处 "'" 不能删除,删除会导致出现问题2(坑爹) img.write("test.png", function (err) {if(err)console.error(err)});//写出图片
2. An error message appears: Error: Command failed: convert: non-conforming drawing primitive definition
The text given to drawText Just add a ' on the left and right to solve it (functions with text can be solved this way)
Example:
img.drawText(0, 50, text);//错误 img.drawText(0, 50, "'" + text + "'");//正确方法
3. Error: Error: Command failed: ���� ��
usually appears in ImageMagick7.x and after setting imageMagick: true.
Reinstall 7.x and check the "Install legacy utilities (e.g. convert)" option during installation.
It took me a day to complete this gm....
Recommended study: "node.js video tutorial"
The above is the detailed content of What to do if node gm reports an error. For more information, please follow other related articles on the PHP Chinese website!