php – 将阿拉伯语写入图像时出错

前端之家收集整理的这篇文章主要介绍了php – 将阿拉伯语写入图像时出错前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我以前的相关问题: @H_301_1@php work with images : write complete word in arabic,ttf font

@H_301_1@我的问题是:

@H_301_1@>如果我想在图像中写入احمد,它会显示为دمحا
嗯,我修好了,现在输出:احمد

@H_301_1@使用此功能

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) ;

}
@H_301_1@但新问题是:

@H_301_1@ححد

@H_301_1@(分开的字母)应该是:

@H_301_1@احمد

@H_301_1@如何解决这个问题?

你的倒转阿拉伯字符的方式没有考虑到连接字形的本质.
但是,解决PHP / GD不能自动支持RTL语言(如阿拉伯语)的问题是一个有效的手段. @H_301_1@您需要做的是使用完全符合您预期的ar-php库.

@H_301_1@确保您的PHP文件编码是unicode / UTF.
例如>打开记事本>另存为>编码为UTF-8:

@H_301_1@使用imagettftext在PHP中使用阿拉伯语排版的示例:

<?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);
    $grey = imagecolorallocate($im,128,128);
    $black = imagecolorallocate($im,0);
    imagefilledrectangle($im,599,299,$white);

    // Add the text
    imagettftext($im,50,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); 
?>
@H_301_1@输出

猜你在找的PHP相关文章