favormm
V2EX  ›  问与答

nodejs中的node-canvas如何绘制中文并保存到图片。

  •  
  •   favormm · Oct 28, 2013 · 7911 views
    This topic created in 4669 days ago, the information mentioned may be changed or developed.
    最近在用nodejs,想在后台处理点图片。 就是将用户名字写到图片上。
    我试了node-canvas这个库,可以将english draw到canvas上并保存为图片,但是无法draw中文, 不知道是不是用法问题还是node-canvas这个库本来就不支持中文等像形文字?

    请高人指点迷津。
    12 replies    1970-01-01 08:00:00 +08:00
    shiny
        1
    shiny  
    PRO
       Oct 28, 2013 via iPhone
    需要字体支持中文,我试过可以绘制。
    favormm
        2
    favormm  
    OP
       Oct 28, 2013
    @shiny 大哥,你用的是什么字体?
    ijse
        3
    ijse  
       Oct 29, 2013
    要不,换gm试下吧,觉得它更强大些。
    shiny
        4
    shiny  
    PRO
       Oct 29, 2013
    @favormm 随便什么中文字体都可以,举个栗子:

    http://www.shiguanglu.com/captcha.png
    favormm
        5
    favormm  
    OP
       Oct 29, 2013
    @shiny
    大神,我用的是:https://github.com/LearnBoost/node-canvas
    然后在sample中的font.js有一个示例,我试了一下。然后运行了一下,
    var Canvas = require('canvas')
    , Font = Canvas.Font
    , fs = require('fs')
    , path = require("path");

    if (!Font) {
    throw new Error('Need to compile with font support');
    }

    会有错误,会进入if中去, Canvas.Font为空。我是用的npm install canvas的。 杂我的不支持Font呢。
    favormm
        6
    favormm  
    OP
       Oct 29, 2013
    @ijse gm我试了,我用brew安装的,结果装不上,郁闷。
    ijse
        7
    ijse  
       Oct 29, 2013
    @favormm 额,这个,难道不是要用npm装嘛? 它所依赖的GraphicMagic可以用brew
    shiny
        8
    shiny  
    PRO
       Oct 29, 2013
    @favormm OS X 里装这个 canvas 有点难装,虽然他的 wiki 页面里给了整个流程:https://github.com/LearnBoost/node-canvas/wiki

    我在 debian 里按它 ubuntu 的方式来装是没问题的,Canvas.Font是一个 Function。
    favormm
        9
    favormm  
    OP
       Oct 29, 2013
    @shiny Thks, 我再研究一下。
    favormm
        10
    favormm  
    OP
       Oct 29, 2013
    @ijse 我好像用源码安装过graphicsmagick
    后来用brew install graphicsmagick安装就会出问题
    You must `brew link libpng' before graphicsmagick can be installed,
    我也试着brew link libpng,然后提示:
    Error: Could not symlink file: /usr/local/Cellar/libpng/1.5.14/include/libpng15/pnglibconf.h
    Target /usr/local/include/libpng15/pnglibconf.h already exists. You may need to delete it.
    To force the link and overwrite all other conflicting files, do:
    brew link --overwrite formula_name

    To list all files that would be deleted:
    brew link --overwrite --dry-run formula_name
    然后我加了--overwrite, 结果提示:
    Linking /usr/local/Cellar/libpng/1.5.14... Warning: Could not link libpng. Unlinking...

    Error: Permission denied - /usr/local/include/libpng15/pnglibconf.h
    然后我又加sudo, 结果提示:
    Error: Cowardly refusing to `sudo brew link`
    You can use brew with sudo, but only if the brew executable is owned by root.
    However, this is both not recommended and completely unsupported so do so at
    your own risk.

    我就无语了。
    ijse
        11
    ijse  
       Oct 30, 2013
    @favormm 为什么没试下手动删除 /usr/local/include/libpng15/pnglibconf.h 这个文件?

    把libpng重装下试试。。
    favormm
        12
    favormm  
    OP
       Oct 31, 2013
    @ijse 后来我用脚本调用mac下的Quartz来图像处理。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1253 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:51 · PVG 07:51 · LAX 16:51 · JFK 19:51
    ♥ Do have faith in what you're doing.