I want to write a Hindi text(say "पुलिसवाला आमिर खान" ) on an image using PHP5.
I am using
$im = @imagecreate(210, 50);
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 7, 15, 15, utf8_encode("पुलिसवाला आमिर खान..."), $text_color);
ImagePNG($im, 'a.png');
imagedestroy($im);
Use function imagettftext and hindi font.
Path to font should be absolute (use realpath or just write absolute path).
You will definitely need a solution that works with a Truetype font,
imagestring()
won't cut it.Get hold of a TTF font containing Hindi characters tht you can use
get rid of the
utf8_encode()
, just make sure the file is UTF8 encodeduse
imagefttext()
instead ofimagestring()
You can use
imagettftext()
to write text onto images as long as your host supports GD2 and FreeType (as most servers do). You can find its detailed syntax and comments here:http://php.net/manual/en/function.imagettftext.php
Find a font (*.ttf or *.otf) that supports Hindi characters. Put the font file in the same directory as your script, and then try this code -- substituting "yourhindifont.ttf" for your own font filename:
If you plan to input Hindi directly into your source, be sure to set the document encoding of your source to UTF-8 (without BOM) in your editor beforehand. Otherwise the string will not be stored as expected.