How can I write Non English Characters such as Arabic or Persian characters into an image?
Asked Answered
F

4

12

How can I write Arabic or Persian characters to an image using PHP GD library?

i.e. "احسان"

Founder answered 15/9, 2011 at 6:24 Comment(3)
What problems are you facing and what code are you using?Franklin
@Bruce Aldridge your edit narrows the scope of the question. Making it less useful in web searches.Uvea
i'm also having similar issue in tamil fontsAttraction
E
2

I had write a composer package based on a library, I don't remember the name. I modified the library and fixed some bugs it had.

You can find the source here. and you can also install it with composer by running:

composer require quince/persian-gd
  • It has no issue with Persian character
  • It's customizable
  • The string will not overflow out of the image canvas

Please test it, and send bug reports, suggestion and ...

Thanks

Escapism answered 30/9, 2015 at 15:10 Comment(0)
F
1

Use this function in order to pass text to imagettftext

<?php
function revUni($text) {

    $wordsArray = explode(" ", $text);

    $rtlCompleteText='';
    for ($i = sizeOf($wordsArray); $i > -1; $i = $i-1) {

        //$lettersArray = explode("|", str_replace(";|", ";", $wordsArray[$i]));
        $lettersArray = explode(";", $wordsArray[$i]);

        $rtlWord='';
        for ($k = sizeOf($lettersArray); $k > -1; $k = $k-1) {
            if (strlen($lettersArray[$k]) > 1) { // make sure its full unicode letter
                $rtlWord = $rtlWord."".$lettersArray[$k].";";
            }
        }

        $rtlCompleteText = $rtlCompleteText." ".$rtlWord;

    }

    return $rtlCompleteText;
}
?>
Flagon answered 3/8, 2014 at 15:1 Comment(0)
P
1

Plainly reversing Arabic characters like an array just won't work. You need to account for Arabic glyphs and substitute each with the exact Unicode symbol. see here for a similar question and a solution: Error while writting Arabic to image

Pampuch answered 9/9, 2014 at 21:27 Comment(0)
R
0

Try using imagettftext.

<?php
// http://localhost/test.php?text=احسان

// test.php file

$font = 'C:/Windows/Fonts/Arial.ttf';
$text = $_GET['text'];

// [switch to right to left] 
// try comparing of using this block and not using this block
$rtl = array();
for($i=0; $i<strlen($text); $i+=2) {
    $rtl[] = substr($text, $i, 2);
}
$rtl = array_reverse($rtl);
$rtl = implode($rtl);
$text = $rtl;
// [/switch to right to left]

$im = imagecreatetruecolor(65, 35);
$black = imagecolorallocate($im, 0, 0, 0);  
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 500, 100, $white);  
imagettftext($im, 12, 0, 10, 20, $black, $font, $text);  
header('Content-type: image/png');  

imagepng($im);  
imagedestroy($im); 
Riant answered 17/12, 2012 at 1:52 Comment(1)
it would print ا ح س ا ن not احسانSaltatory

© 2022 - 2024 — McMap. All rights reserved.