php imagettftext和特定的表情符号 [英] php imagettftext and specific emoji

查看:448
本文介绍了php imagettftext和特定的表情符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,即在其他符号和象形文字" unicode块下绘制表情符号.

I've run into a problem drawing emoji that are under the 'Miscellaneous Symbols And Pictographs' unicode block.

这是示例代码:

<?php
    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);

    //&#127744; is the unicode html entity for a cyclone. It is under 'Miscellaneous Symbols And Pictographs'.
    $text = "&#127744; is a cyclone!";
    imagettftext($im, 20, 0, 5, 22, $black, $font, $text);

    //&#9924; is the unicode html entity for a snowman. It is under 'Miscellaneous Symbols'.
    $text = "&#9924; is a snowman!";
    imagettftext($im, 20, 0, 5, 52, $black, $font, $text);

    imagepng($im);
    imagedestroy($im);
?>

以下是输出:

这是它的外观:

如您所见,这些表情符号都位于不同的Unicode块下.旋风器位于其他符号和象形文字"下,并且绘制了HTML实体而不是实际的字符,但雪人位于其他符号和象形图"下方,并且正确绘制了.我已仔细检查以确保我使用的字体同时包含两个字符.

As you can see, these emoji are both under different Unicode blocks. The cyclone is under 'Miscellaneous Symbols And Pictographs' and the HTML entity is drawn instead of the actual character, but the snowman is under 'Miscellaneous Symbols' and is drawn correctly. I have double checked to make sure the font I am using contains both characters.

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

To be clear, I want the actual character to be drawn and not the HTML entity.

呈现为UTF8的相同字符:

The same characters rendered as UTF8:

推荐答案

我遇到了与Abigail TTF相同的问题.经过研究,我发现了此链接

I ran into this same kind of a problem with the Abigail TTF. After some research I found THIS LINK

https://bugs.php.net/bug.php?id=17955

其中包含以下示例脚本:

Which had this example script in it:

<?php
$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文件以正确显示.但是我认为这只是找到它们在其中放置实际字体(例如您的气旋图像)的问题.

Their answer: You must convert your string to Unicdoe correctly. This got the Abigail font to show up properly. Unfortunately, I'm still looking at how to get a non-standard TTF file to show up properly. But I think it is just a matter of finding where they put the actual fonts (like your cyclone image).

另一个令人沮丧的因素是GD有时会以正方形显示,有时会保留字符空白.我真的希望总是给出空白字符,因为它以WIDTH = 0和HEIGHT = 0的形式返回,而正方形可以以很多不同的尺寸返回.如果GD标准化后总是返回没有大小的空白字符,那么您所要做的就是寻找它.否则-您必须跳过箍圈才能找到返回的正方形.

The other frustrating factor is that GD will put in squares sometimes and sometimes it will leave the character blank. I really would prefer the blank character to always be given since this comes back as WIDTH=0 and HEIGHT=0 whereas the square can come back with a lot of different sizes. If GD standardized on always giving back a blank character with no size then all you have to do is look for that. Otherwise - you have to jump through hoops to figure out a square was returned.

我希望这会有所帮助! :-)

I hope this helps! :-)

这篇关于php imagettftext和特定的表情符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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