关于图片生成使用gm模块乱码问题
发布于 2 个月前 作者 BrotherPeng 270 次浏览 来自 问答

服务器安装的GraphicsMagick 然后使用gm生成图片,添加中文乱码不知道怎么解决各位有遇到的吗?字体文件是从window随便找的ttf文件

gm('ss.jpg')
    .composite('cc.jpg')
    .geometry('+241+545')
    .write('aaa.jpg', function(err) {
        if(!err) console.log("Written composite image.");
        gm('aaa.jpg')
            .composite('132.jpg')
            .geometry('+100+121')
            .write('aaa.jpg', function(err) {
                if(!err) console.log("Written composite image.");
                gm('aaa.jpg')
                    .font("simfang.ttf", 12)
                    .drawText(100, 500, '士大夫ccc')
                    .write('aaa.jpg', function(err) {
                        if(!err) console.log("Written composite image.");
                    });
            });
    });**
	```
2 回复

have a try

function utf16to8(str) {  
    var out, i, len, c;  
    out = "";  
    len = str.length;  
    for(i = 0; i < len; i++) {  
    c = str.charCodeAt(i);  
    if ((c >= 0x0001) && (c <= 0x007F)) {  
        out += str.charAt(i);  
    } else if (c > 0x07FF) {  
        out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));  
        out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));  
        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));  
    } else {  
        out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));  
        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));  
    }  
    }  
    return out;  
}  

@struCoder 多谢,这个我倒没试,用其他字体库解决了问题

回到顶部