如何使用imagick annotateImage中文文本? [英] how to use imagick annotateImage for chinese text?

查看:1392
本文介绍了如何使用imagick annotateImage中文文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用中文文本注释图像,我现在正在使用Imagick库。

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

中文文本示例

这是中文

使用的中文字体文件是 this

The Chinese Font file used is this

该文件原名为华文黑体.ttf

The file originally is named 华文黑体.ttf

它也可以在Mac OSX中找到/ Library / Font

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

我已将它重命名为英文STHeiTi.ttf让它更容易调用php代码中的文件。

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

特别是 Imagick :: annotateImage 功能

我也是使用的答案如何在PHP中使用Imagick绘制包装文本?

我使用它的原因是因为它对英文文本和应用程序是成功的虽然不是在同一时间,但需要注释英文和中文。

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.

问题是,当我使用中文文本运行annotateImage时,我得到的注释看起来像罍

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

包含的代码此处

推荐答案

完整解决方案:

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

主要想法:

必须在表单和处理页面上设置html字符集和内部编码

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

这些行必须位于php文件的顶行。

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

使用此功能确定文本是否为中文并使用正确的字体文件

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

有关详细信息,请查看 https://stackoverflow.com/a/11219301/80353

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

在ImagickDraw对象中正确设置TextEncoding

$draw = new ImagickDraw();

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

注意大写的UTF。 Walter Tross 在答案中向我指出了这一点:https://stackoverflow.com/a/11207521/80353

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

使用preg_match_all爆炸英语单词,中文单词和空格

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

受此回答的启发 https://stackoverflow.com/a/4113903/80353

同样适用于英文文本

这篇关于如何使用imagick annotateImage中文文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆