Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My previous related question:

php work with images : write complete word in arabic , ttf font

My problem was:

  • If I want to write ???? in image it appears as ? ? ? ?
  • Well, I fixed it and now the output: ? ? ? ?

Using this function:

function arab($word){

       $w = explode(' ',$word) ;

       $f = array(array('?','?'),'?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?');

       $t = array(array('?_','?_'),'?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_','?_');

       $my_arab = '' ;

       foreach($w as $wo)
        {
             $r  = array() ;

             $wo = str_replace($f , $t ,$wo);

             $ne = explode('_', $wo) ;

             foreach($ne as $new) {
                $new = str_replace('_','',$new) ;
                array_unshift($r , $new);
             }

            $my_arab .=  ' '.implode('',$r) ;

        }

     return trim($my_arab) ;

}

But the new problem is:

? ? ? ?

(separated letters) where it should be:

????

How can I fix this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
333 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your way of reversing Arabic characters does not take into account the nature of connected glyphs. However, it is a valid trick to solve the issue of PHP/GD not automatically supporting RTL languages like Arabic.

What you need to do is to use ar-php library that does exactly what you intended.

Make sure your PHP file encoding is in unicode/UTF.
e.g. > open Notepad > Save As > encoding as UTF-8:

enter image description here

Sample usage for Arabic typography in PHP using imagettftext:

<?php 
    // The text to draw
    require('./I18N/Arabic.php'); 
    $Arabic = new I18N_Arabic('Glyphs'); 
    $font = './DroidNaskh-Bold.ttf';
    $text = $Arabic->utf8Glyphs('???? ??????');

    // Create the image
    $im = imagecreatetruecolor(600, 300);

    // Create some colors
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 599, 299, $white);

    // Add the text
    imagettftext($im, 50, 0, 90, 90, $black, $font, $text);

    // Using imagepng() results in clearer text compared with imagejpeg()
    imagepng($im, "./output_arabic_image.png");
    echo 'open: ./output_arabic_image.png';
    imagedestroy($im); 
?>

Outputs:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...