将阿拉伯文字写入图片时出错 [英] Error while writting Arabic to image

查看:117
本文介绍了将阿拉伯文字写入图片时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前的相关问题:

php与图像一起使用:编写完整阿拉伯语单词,ttf字体

我的问题是:

  • 如果我要在图像中写入احمد,它将显示为د م ح ا
  • 好吧,我修复了它,现在输出了:ا ح م د
  • If I want to write احمد in image it appears as د م ح ا
  • Well, I fixed it and now the output: ا ح م د

使用此功能:

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

}

但是新的问题是:

ا ح م د

(分隔字母)应位于的位置:

(separated letters) where it should be:

احمد

我该如何解决?

推荐答案

反转阿拉伯字符的方式未考虑已连接字形的性质. 但是,这是一个有效的技巧,用于解决 PHP/GD无法自动支持RTL语言(如阿拉伯语)的问题.

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.

您需要做的是使用完全符合您要求的 ar-php

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

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

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

使用 imagettftext 在PHP中使用阿拉伯文字的示例用法:

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

输出:

这篇关于将阿拉伯文字写入图片时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆