Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

unicode - php imagettftext and specific emoji

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:

The Cyclone and the Snowman

Here is what it should look like:

The Cyclone and the Snowman Part II

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:

The Cyclone and the Snowman, Part III

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...