PHP5 - I want to write Hindi text on an image

I want to write Hindi text (say "पुलिसवाला आमिर खान") on an image using PHP5.

I use

$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);
+3
source share
3 answers

You can use it imagettftext()to write text to images if your host supports GD2 and FreeType (as most servers do). Detailed syntax and comments can be found 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 - replacing "yourhindifont.ttf" with your own font file name:

    <?php

    // your string, preferably read from a source elsewhere
    $utf8str = "पुलिसवाला आमिर खान";

    // buffer output in case there are errors
    ob_start();

    // create blank image
    $im = imagecreatetruecolor(400,40);
    $white = imagecolorallocate($im,255,255,255);
    imagefilledrectangle($im,0,0,imagesx($im),imagesy($im),$white);

    // write the text to image
    $font = "yourhindifont.ttf";
    $color = imagecolorallocatealpha($im, 50, 50, 50, 0); // dark gray
    $size = 20;
    $angle = 0;
    $x = 5;
    $y = imagesy($im) - 5;
    imagettftext($im, $size, $angle, $x, $y , $color, $font, $utf8str);

    // display the image, if no errors
    $err = ob_get_clean();
    if( !$err ) {
        header("Content-type: image/png");
        imagepng($im);
    } else {
        header("Content-type: text/html;charset=utf-8");
        echo $err;
    }

    ?>

, UTF-8 ( ) . , .

+1

imagettftext .
( realpath ).

+3

, Truetype, imagestring() .

+2

All Articles