dongmie3526 2012-06-19 13:02
浏览 191
已采纳

如何使用imagick annotateImage中文文本?

I need to annotate an image with Chinese Text and I am using Imagick library right now.

An example of a Chinese Text is

这是中文

The Chinese Font file used is this

The file originally is named 华文黑体.ttf

it can also be found in Mac OSX under /Library/Font

I have renamed it to English STHeiTi.ttf make it easier to call the file in php code.

In particular the Imagick::annotateImage function

I also am using the answer from "How can I draw wrapped text using Imagick in PHP?".

The reason why I am using it is because it is successful for English text and application needs to annotate both English and Chinese, though not at the same time.

The problem is that when I run the annotateImage using Chinese text, I get annotation that looks like 罍

Code included here

  • 写回答

3条回答 默认 最新

  • dongzhan3937 2012-06-27 04:28
    关注

    Full solution here:

    https://gist.github.com/2971092/232adc3ebfc4b45f0e6e8bb5934308d9051450a4

    Key ideas:

    Must set the html charset and internal encoding on the form and on the processing page

    header('Content-Type: text/html; charset=utf-8');
    mb_internal_encoding('utf-8');
    

    These lines must be at the top lines of the php files.

    Use this function to determine if text is Chinese and use the right font file

    function isThisChineseText($text) {
        return preg_match("/\p{Han}+/u", $text);
    }
    

    For more details check out https://stackoverflow.com/a/11219301/80353

    Set TextEncoding properly in ImagickDraw object

    $draw = new ImagickDraw();
    
    // set utf 8 format
    $draw->setTextEncoding('UTF-8');
    

    Note the Capitalized UTF. THis was helpfully pointed out to me by Walter Tross in his answer here: https://stackoverflow.com/a/11207521/80353

    Use preg_match_all to explode English words, Chinese Words and spaces

    // separate the text by chinese characters or words or spaces
    preg_match_all('/([\w]+)|(.)/u', $text, $matches);
    $words = $matches[0];
    

    Inspired by this answer https://stackoverflow.com/a/4113903/80353

    Works just as well for english text

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题