1

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

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • can you be more clear? how do you want the output look like? – ɹɐqʞɐ zoɹǝɟ May 17 '14 at 04:47
  • I thought I made it pretty clear, but I'll try rephrasing it. What I want is for the actual character to be drawn and not the html entity. – Nobody Important May 17 '14 at 04:51
  • Evidently `imagettftext`'s weird and pointless sort-of-HTML-character-reference-parser doesn't support characters outside the Basic Multilingual Plane. What happens if you write the character either directly (`" is a cyclone!"` and save file as UTF-8) or as UTF-8 byte escapes `"\xF0\x9F\x8C\x80 is a cyclone!"`? (May still not work, but should get closer...) – bobince May 17 '14 at 14:33
  • When I do either of those suggestions, this is the output: http://i.imgur.com/NjJfOnA.png I'm assuming that the 4 characters drawn somehow correlate with the character's bytes. – Nobody Important May 17 '14 at 17:26
  • I don't think it's going to work. GD doesn't currently recognize UCS-4 CMAP. It'll fall back to using a UCS-2 CMAP. Characters beyond the BMP aren't reachable. – cleong May 19 '14 at 06:00

1 Answers1

0

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

Mark Manning
  • 1,427
  • 12
  • 14