使用 PHP imagettftext() 将东亚字符呈现为正方形

发布于 2024-07-14 06:53:45 字数 255 浏览 5 评论 0原文

我正在尝试使用 PHP imagettftext 函数用 verdana 文本渲染图像。 但是,所有东亚字符均未正确渲染。 我尝试使用其他字体,如 tahoma 和 Lucida Grande,但都不起作用。 然而,Arial Unicode 却可以完美地工作。

问题是我不想使用 Arial 作为我的字体。 有什么方法可以使用像 Verdana 这样的字体进行渲染吗?

(不是 unicode 和/或字体专家;所以对问题的解释会很有帮助)

I'm trying to render images with verdana text using PHP imagettftext function. However, all east asian characters are not being rendered correctly. I tried using other fonts like tahoma and Lucida Grande, but neither works. Arial Unicode, however, works perfectly.

The problem is that I don't want to use Arial as my font. Is there any way I could render using a font like Verdana?

(not a unicode and/or font expert; so explanation of the problem would be helpful)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单身情人 2024-07-21 06:53:46

Verdana、Tahoma 或 Lucida Grande 都不包含中文/日文/韩文字符,因此它们永远无法“正确”渲染它们。 没有字体包含每个 Unicode 代码点的字形,因此无论您选择哪种字体,最终都会在某些时候丢失字形。

有一种类似于 Verdana 的字体,包含一些 CJK 字符 - 特别是日语汉字。 也许这足以涵盖您想要做的事情? 它称为 Meiryo,随 Windows Vista/7 一起提供。 您还可以在此处获取它。 然而它是一个 TrueType Collection (.ttc) 文件,我不确定 PHP/GD 是否可以直接处理; 许多东亚字体都是 .ttc 文件,因为它们通常包含基于相同字形形状的比例字体和等宽字体。

如果 PHP 无法处理 .ttc,您可能需要使用字体编辑器或来自TrueType SDK(ttsdk.zip,从 microsoft.com 神秘消失,但使用 Google 可以轻松找到)。

None of Verdana, Tahoma or Lucida Grande contain Chinese/Japanese/Korean characters so they will never be able to render them “correctly”. No font contains glyphs for every Unicode code point, so whichever you pick you're going to end up with missing glyphs at some point.

There is a font that is like Verdana and contains some CJK characters — specifically Japanese kanji. Maybe that's enough to cover what you're trying to do? It's called Meiryo and comes with Windows Vista/7. You can also get it here. However it is a TrueType Collection (.ttc) file, which I'm not sure if PHP/GD can handle directly; many East Asian fonts come as .ttc files, because they typically contain both proportional and monospace fonts based on the same glyph shapes.

If PHP can't cope with .ttc, you might need to convert the Meiryo-proportional font out of it into a separate .ttf file, using either a font editor, or the simple ‘breakttc.exe’ command-line tool from the TrueType SDK (ttsdk.zip, mysteriously vanished from microsoft.com, but easily locatable using Google).

零度℉ 2024-07-21 06:53:46

IIRC 您的 truetype 字体中必须包含 unicode 字符。

您可以在 Windows 的字符映射表工具中查看某种字体有多少个字符。

IIRC your truetype font must have unicode characters within it.

You can see how many characters a certain font has in the Character Map tool in windows.

疯到世界奔溃 2024-07-21 06:53:46

我之前曾直接使用东亚字体用 imagettftext 渲染日语字符。 有一些随 Windows 一起提供的: “simsun”(中文) “MS Gothic”(日语) “Ms Mincho”(日语) 最后两个相当于日语文本的 sans serif 和 serif。 我认为中国人也有这两种风格。 在我的旧 php 代码中,它使用带有“.ttc”扩展名的字体文件,不知道为什么。 也许 TTF 是定义,而实际字体是 TTC 文件? (那是很久以前的事了)

无论如何,至于使用Arial等,在Windows上至少它的工作方式是系统检测到“西方”Unicode范围之外的字符,并会自动切换到默认安装的“亚洲”之一渲染这些的字体。 这导致一些用户获得中文字符而不是日文字符,反之亦然,因为其中许多字符共享相同的 unicode 范围,但根据使用的字体,您会得到“中文”样式或日文样式。

混合两种字体看起来不太好。 纯粹使用日语字体将以“等宽”字体呈现字母字符,这看起来不太好。

如果所有其他方法都失败,您可以尝试将字符串拆分为单独的西方/东方字符范围子字符串(循环字符并测试 unicode 范围),然后使用适当的字体呈现每个部分。 请记住,东方字体是等宽的(至少是中文/日文,我不知道韩文等)。

I've rendered Japanese characters with imagettftext before, by using the East Asian font directly. There are a few which come packaged with Windows : "simsun" (Chinese) "MS Gothic" (Japanese) "Ms Mincho" (Japanese) The last two are the equivalent of sans serif and serif for Japanese text. I think the Chinese ones have both styles too. In my old php code it uses font files with a ".ttc" extension, not sure why. Perhaps TTF is the definition and the actual font is a TTC file? (that was long ago)

Anyway, as for using Arial etc, on Windows at least the way it works is that the system detectes characters outside of the "western" Unicode range, and will automatically switch to one of the default installed "Asian" fonts to render those. Thise cause among other things, some users to get chinese characters instead of Japanese or vice versa, because many of those characters share the same unicode range, but depending on which font is used, you egt the "chinese" stlye or the japanese style.

Mixing both fonts doesn't look great. Using purely a Japanese font will render the alphabetic characters in a "monospaced" font, which doesn't look too good.

If all else fail, you could try to split your string into separate western/eastern character range substrings (loop on characters and test unicode range), and then render each part with the appropriate font. Keep in mind the Eastern fonts are monospaced (at least chinese/japanese, I don't know about korean, etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文