I am using imagettftext function to write text on image but my text contain smileys and it replace smileys to square box
Here is mycode:
$black = imagecolorallocate ( $main_img, 0x00, 0x00, 0x00 );
$font_path = "Arial.ttf";
imagettftext ( $main_img, 14, 0, 73, 685, $black, $font_path, $text );
Text Sample:
test testt
Can you post which smiley's are you trying to use? It would be best if you could give us codes that you can see in windows charmap after selecting a smiley. In Arial I have found only these smiley's: ☺☻☼♀♂♠♣♥♦♪♫♯ - they all show up without any problems. I used example from php.net
Try using a font with known emoji unicode characters. Like Symbola
Firstly, we need to understand how emojis are implemented:
The last approach is what we are interested in. This is because
imagefttext
is function provided by PHP GD extension. And GD internally uses FreeType library to draw text.Emojis are supported in FreeType since version 2.5. Try executing
php -i
command to see what version of FreeType do you have. This is mine:However, after trying with some sample fonts I found here, PHP keeps throwing warning:
I'm afraid that GD library doesn't support FT_LOAD_COLOR flag. This flag is required to have colored emojis. See FreeType changelog:
To answer your question: you can't have colored smileys using
imagefttext
. I'm sorryI suggest You using PHP ImageMagick library. Though I didn't try it with emoji I'we encountered the same problem as yours with other Unicode characters when tried to render text with DG.
BTW migrating to IM is good idea at least because of better performance and less quirks in it.
Here is an example that should work for You
Here's my suggestion:
Use this to generate the colored nice looking emojis (select output .png) http://git.emojione.com/demos/latest/phpunicodetoimage.php#output
Use imagettftext and Arial.ttf for the remaining text
Append all the .png using Imagick::appendImages