php imagettftext and specific emoji

一世执手 提交于 2019-11-29 17:28:56

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

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).

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! :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!