如何在图像中写入阿拉伯语或波斯语等非英语字符?

时间:2011-09-15 06:24:27

标签: php gd

如何使用PHP GD库将阿拉伯语或波斯语字符写入图像?

即。 “احسان”

4 个答案:

答案 0 :(得分:1)

使用此功能将文字传递给 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;
}
?>

答案 1 :(得分:1)

明显颠倒阿拉伯字符就像数组一样无法正常工作。您需要考虑阿拉伯语字形,并使用确切的Unicode符号替换每个字符。请点击此处查看类似的问题和解决方案:Error while writting Arabic to image

答案 2 :(得分:0)

尝试使用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); 

答案 3 :(得分:0)

我写了一个基于库的作曲家包,我不记得名字了。我修改了库并修复了它的一些错误。

您可以找到来源here。你也可以通过运行:

与composer一起安装它
composer require quince/persian-gd
  • 波斯语字符没有问题
  • 可自定义
  • 字符串不会溢出图像画布

请测试它,并发送错误报告,建议和......

由于

相关问题