PHP 函数 imagettftext() 写带笑脸的文本

标签 php emoji imagettftext

我正在使用 imagettftext 函数在图像上写文字,但我的文字包含笑脸,它将笑脸替换为方形框

这是我的代码:

$black = imagecolorallocate ( $main_img, 0x00, 0x00, 0x00 );
$font_path = "Arial.ttf";
imagettftext ( $main_img, 14, 0, 73, 685, $black, $font_path, $text );

文本示例:

test testt😘 Hello 👋💐 Those 👏  Mega x 👊❤️  Graphic 💋

我已经尝试更改“Arial Unicode”字体,但未解决问题。

我该如何解决这个问题?

提前致谢。

最佳答案

首先,我们需要了解表情符号是如何实现的:

Apple’s PNG images (SBIX table)

Apple implemented it using a proprietary, unpublished extension of the TrueType/OpenType specification to add PNG images to a font. These PNG images are then displayed within running text. Google also implemented similar thing, but it is not compatible with Apple’s solution.

Mozilla and Adobe’s SVG (SVG table).

These two web giants got together for the most ambitious format: SVG in OpenType. Instead of mapping a Unicode char to a glyph, we map it to an SVG image. This brings us all the lovely extra’s of SVG, including gradients, embedded bitmap images and even animation. It’s already been supported in Firefox since version 26!

Microsoft COLR/CPAL

By default, the new Segoe UI Emoji font behaves like a regular TrueType/OpenType font. It has Unicode-encoded, uncolored “base glyphs”. But there are two additional tables in the font: the COLR table links additional glyphs as layers to the base glyphs and defines the order of these layers. And the CPAL (“Color Palette”) table stores one or more color palettes for the individual layers. (The different color palettes are useful for displaying the font on dark and light backgrounds.) So when there is support for this new color feature, the base glyphs will be replaced with the colored layers.

Google’s PNG images (CBDT/CBLC tables).

Google proposed an implementation which uses PNG images for the glyphs. The glyphs are simply replaced by good old images. This works fine for smaller icons, and obviously brings all the creative freedom of bitmap images, but doesn’t scale very well. Blow up a glyph big enough, and you’ll encounter blurred pixels. It’s also going to be hard, if not impossible, to change the color of the glyphs with CSS. Interesting is that they specify that there should be no GLYF table in their implementation — the table that holds the uncolored “normal” glyphs — so it looks like there’s no fallback for when this format isn’t supported. It’s already implemented in FreeType, which is used on Android and Linux, but a proposal for OpenType would bring this to Windows and Apple machines as well.


Sources: 1 and 2

最后一种方法是我们感兴趣的。这是因为imagefttext 是PHP GD 扩展提供的功能。而GD内部使用FreeType库来绘制文字。

从 2.5 版开始,FreeType 就支持表情符号。尝试执行 php -i 命令来查看您使用的是什么版本的 FreeType。这是我的:

FreeType Support => enabled
FreeType Linkage => with freetype
FreeType Version => 2.5.2

However, after trying with some sample fonts I found here, PHP keeps throwing warning:

Warning: imagefttext(): Could not set character size

恐怕GD library不支持 FT_LOAD_COLOR旗帜。此标志必须具有彩色表情符号。查看 FreeType 变更日志:

2013-05-23  Behdad Esfahbod  

    Add support for color embedded bitmaps (eg. color emoji).

    A new load flag, FT_LOAD_COLOR, makes FreeType load color
    embedded-bitmaps, following this draft specification

      https://color-emoji.googlecode.com/git/specification/v1.html

回答您的问题:您不能使用 imagefttext 制作彩色笑脸。对不起😉。

编辑:

您不能在 GD 中也绘制黑白表情符号。这是因为 GD 只支持 1-3 个多字节 UTF-8 字符。 Emojis字符范围属于4字节UTF-8字符。

来源:https://github.com/libgd/libgd/blob/master/src/gdft.c#L341

具有多字节 UTF-8 表示的表情符号表:http://apps.timwhitlock.info/emoji/tables/unicode

关于PHP 函数 imagettftext() 写带笑脸的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32391005/

相关文章:

php - imagettftext() : calculate font size to ensure text fits image width

php - 欧元 (€) 图片ttftext

PHP imagefttext(imagettftext)不显示任何内容

php - 使用 MySQL 和 PHP 显示从多个联结表获得的信息

php - 在一行中运行多个 inkscape 命令

node.js - 如何在 Node.js(在 Windows 上)中将表情符号输出到控制台?

css - 如何为表情符号构建可靠的 css 字体堆栈?

php - 正则表达式删除新行和空格

php - 将查询字符串参数添加到 Guzzle GET 请求?

javascript - 获取包含表情符号的文本中的光标位置并插入标签