php imagettftext and specific emoji

徘徊边缘 提交于 2019-11-28 12:18:33

问题


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

Here is an example code:

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

Here is the output:

Here is what it should look like:

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.

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

The same characters rendered as UTF8:


回答1:


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



来源:https://stackoverflow.com/questions/23707654/php-imagettftext-and-specific-emoji

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