gm(300,300,‘green’) .font(‘stliti’)//中文字体已写在配置文件中 .fontSize(50) .drawText(10, 150, ‘ABC验证码’) .write(“D:\node\gmtest\a1.jpg”,function(err) { if (err) console.log(err); }); ![Alt text](data:image/jpg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAA4AJYDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5yooor8PP5vCiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAClbaqlmYIBzzSVjatp811q8b3McV3p5h2Q20jcNcFiSzKRggL3PTBrejTVSVpO39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rdlxyQOldvu9qNxxjj8q6vr83UlNpe969VbT5aHb/ac3UnUcV72++l1Ztavo7HnnifwnqGvWPiTVGg8me6to4LazBBkKIwc7scFjjgD6V1Gl+KdMn0W1nS4VpSgUWq8zb8fc2dc9sfj05rb3EHPeo1hiSQyLEiyHq4UBj+NE8Yq1NU6q0jtbpolb8N9xVMwWIpKlWjpF3VtLaJW2fZa73uOBJUEjBI5GelLRRXlnjHKfE/wxeeLPDAtbEjz45hMIy20PgEbc9uufwq/4Yh1hYoTfW9npcCphrS0AdmbHUtwFHsPzrcyc8cUu8g5HB9RxXd9bn9XWHsrXb+89JY6f1ZYVpOKbafVX38vnZvsJRRRXCeaZvim1i1Dw7f20yysrxkfuF3SZ6gqPUEAisew8X3Om2UcfiLTrqxvAoDSQxNPE5x1BQHB9Qa6sNjPHWl8w7s8A+wxXZTrwVP2VSN1e+9mvT9dH0PQpYmnGl7GrDmV772a226et0+hwev6xd+KRbQW+lXy+HxNGb24eErJKoYEBYz8xXIGTjpXdU7eQcg4b1B5ptKtWjUjGEI8qV+t9+/n/VkTiMRGtCFOEOVRv1vvbV+en3W0CiiiuQ4QooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP/2Q==)
建议使用https://github.com/LearnBoost/node-canvas
windows系统下 问题解决了。再安装ImageMagick,说一下过程避免后人掉坑。
First install either GraphicsMagick or ImageMagick. Then: npm install gm
官网说GraphicsMagick 或 ImageMagick 都可以, 刚开始安装了GraphicsMagick ,网上都说这个好点是从ImageMagick 分离出来的。 var gm = require(‘gm’); gm().xxx().xxx() 这样基本上能实现很多功能了,如(剪裁、缩放、旋转等等),但是调用子类报错var imageMagick = gm.subClass({ imageMagick: true });应该是没装ImageMagick ,而且涉及到中文输出的一概乱码,网上翻遍了,试了很多,都不行。 今天想着安装ImageMagick 碰碰运气,还真就行了。但得用子类方法。 原先: var gm = require(‘gm’); gm(300,300,‘green’) .font(‘stliti’)//中文字体已写在配置文件中 .fontSize(50) .drawText(10, 150, ‘ABC验证码’) .write(“D:\node\gmtest\a1.jpg”,function(err) { if (err) console.log(err); }); 这样就算安装了ImageMagick 也是乱码。
改后: var gm = require(‘gm’); var imageMagick = gm.subClass({ imageMagick: true }); imageMagick (300,300,‘green’) .font(‘stliti’)//中文字体已写在配置文件中 .fontSize(50) .drawText(10, 150, ‘ABC验证码’) .write(“D:\node\gmtest\a1.jpg”,function(err) { if (err) console.log(err); }); 这样就能正常输出中文了。
其实,可以只安装ImageMagick 就行了的。但gm().xx().xx()的这些方法就不能用了,要通过子类调用var imageMagick = gm.subClass({ imageMagick: true });
以上就是整个问题的过程。