0

I'm trying to change the encoding of my imagefttext() result.
My idea is to print some text in "Arabic" language onto a specified image , but the result appear in strange form.

I used this code, but it is work correctly just with English string :

mb_internal_encoding('UTF-8');
$im =imagecreatefromjpeg('x.jpg');
$textcolor=imagecolorallocate($im, 0, 0, 255);
imagefttext($im, 18, 0, 10, 20,$textcolor,"Fonts/tahoma.ttf","مسعود أوزبل");
imagejpeg($im,"mozil.jpg",100);
imagedestroy($im);

Can i have any solution for this problem ?
Thanks

2 Answers2

0

Arabic characters can not be treated as an array. Also imagefttext() does not support RTL languages like Arabic out of the box. What you need to do is to reverse every character without changing its shape (glyph/symbol). @Lars answer won't work because a char in Arabic might change based on its position in the word. See here for a similar answer and a solution: Error while writting Arabic to image

Community
  • 1
  • 1
Nasser Al-Wohaibi
  • 4,562
  • 2
  • 36
  • 28
-1

in the comments of the manual for imagettftext, there's an example for RTL languages. Try this

    $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;
} 
Lars
  • 5,757
  • 4
  • 25
  • 55