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

Pas*_* By 11 php arabic imagettftext

我以前的相关问题:

php使用图像:用阿拉伯语,ttf字体写完整的单词

我的问题是:

  • 如果我想写入????图像,则显示为? ? ? ?
  • 好吧,我修复了它现在输出: ? ? ? ?

使用此功能:

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

}
Run Code Online (Sandbox Code Playgroud)

但新问题是:

? ? ? ?

(分开的字母)它应该是:

????

我怎样才能解决这个问题?

Nas*_*ibi 8

反转阿拉伯字符的方式没有考虑连接字形的性质.但是,解决PHP/GD不能自动支持阿拉伯语等RTL语言的问题是一个有效的技巧.

你需要做的是使用完全符合你的意图的ar-php库.

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

在此输入图像描述

使用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, 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); 
?>
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述


小智 4

你必须使用 bidi 转换器 我用它在图像中写入波斯语

<?php
#----------------------------------------------------------------------
# Persian Image 1
#----------------------------------------------------------------------
# Copyright (c) 2011 Saeed Arab Sheybani
#----------------------------------------------------------------------
# This program is under the terms of the GENERAL PUBLIC LICENSE (GPL)
# as published by the FREE SOFTWARE FOUNDATION. The GPL is available
# through the world-wide-web at http://www.gnu.org/copyleft/gpl.html
#----------------------------------------------------------------------
# Authors: Saeed Arab Sheybani <webrefer@Gmail.com>
# Thanks to TCPDF project @ http://www.tecnick.com/ i use unicode_data.php and bidi.php from this site
#----------------------------------------------------------------------

/**
 * A function to change persian or arabic text from its logical condition to visual
 *
 * @author      Saeed Arab Sheybani <webrefer@Gmail.com>
 * @param       string  Main text you want to change it
 * @param       boolean Apply e'raab characters or not? default is true
 * @param       boolean Which encoding? default it "utf8"
 * @param       boolean Do you want to change special characters like "allah" or "lam+alef" or "lam+hamza", default is true
 */
function Persian_image(&$str)
{
    include_once('bidi.php');

    $text = explode("\n", $str);

    $str = array();

    foreach($text as $line){
        $chars = bidi::utf8Bidi(bidi::UTF8StringToArray($line), 'R');
        $line = '';
        foreach($chars as $char){
            $line .= bidi::unichr($char);
        }

        $str[] = $line;
    }

    $str = implode("\n", $str);
}
Run Code Online (Sandbox Code Playgroud)