天天看点

php 表情选择,php imagettftext和特定的表情符号

我遇到了一个问题,绘制了“杂项符号和象形文字”unicode块下的表情符号.

这是一个示例代码:

header('Content-Type: image/png');

$im = imagecreatetruecolor(290, 60);

$grey = imagecolorallocate($im, 200, 200, 200);

$black = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, 289, 59, $grey);

$font = 'seguisym.ttf';

$font = realpath($font);

//🌀 is the unicode html entity for a cyclone. It is under 'Miscellaneous Symbols And Pictographs'.

$text = "🌀 is a cyclone!";

imagettftext($im, 20, 0, 5, 22, $black, $font, $text);

//⛄ is the unicode html entity for a snowman. It is under 'Miscellaneous Symbols'.

$text = "⛄ is a snowman!";

imagettftext($im, 20, 0, 5, 52, $black, $font, $text);

imagepng($im);

imagedestroy($im);

?>

这是输出:

这是它应该是什么样子:

如您所见,这些表情符号都在不同的Unicode块下.旋风是在“其他符号和象形文字”下,并且绘制HTML实体而不是实际角色,但是雪人在“其他符号”下并且被正确绘制.我已经仔细检查以确保我使用的字体包含两个字符.

要清楚,我希望绘制实际字符而不是HTML实体.

呈现为UTF8的相同字符:

解决方法:

我遇到了与阿比盖尔TTF同样的问题.经过一些研究,我找到了这个链接

其中有这个示例脚本:

$str = "this is a test";

$str = iconv("UCS-2", "UTF-8", preg_replace("/(.)/","\xf0\$1", $str));

$fsize = 32;

$ffile= "C:/wamp/www/gm/fonts/Aztec/101_Aztec SymbolZ.ttf";

$size = ImageTTFBBox($fsize, 0, $ffile, $str);

$txt_width = ($size[2] - $size[0]);

$txt_height = ($size[1] - $size[7]);

$im_width = $txt_width * 1.5;

$im_height = $txt_height * 1.5;

$im = ImageCreateTrueColor($im_width, $im_height);

$black = ImageColorAllocate($im, 0, 0, 0);

$white = ImageColorAllocate($im, 255, 255, 255);

$txt_x = ($im_width - $txt_width) / 2;

$txt_y = ($im_height - $txt_height) / 2 + $txt_height;

ImageFilledRectangle($im, 0, 0, $im_width, $im_height, $white);

imagecolortransparent( $im, $white );

ImageTTFText($im, $fsize, 0, $txt_x, $txt_y, $black, $ffile, $str);

Imagegif($im, "./test.gif");

ImageDestroy($im);

?>

他们的回答是:您必须正确地将您的字符串转换为Unicdoe.这使得Abigail字体能够正确显示.不幸的是,我仍然在研究如何正确显示非标准的TTF文件.但我认为这只是找到他们放置实际字体的位置(如旋风图像).

另一个令人沮丧的因素是,GD有时会放入正方形,有时它会使角色留空.我真的更喜欢永远给出空白字符,因为它返回WIDTH = 0和HEIGHT = 0而广场可以返回有很多不同的大小.如果GD标准化总是给出一个没有大小的空白字符,那么你所要做的就是寻找它.否则 – 你必须通过箍来找出一个正方形被退回.

我希望这有帮助!

标签:php,unicode,gd,imagettftext

来源: https://codeday.me/bug/20191003/1848431.html