Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将阿拉伯语写入图像时出错_Php_Arabic_Imagettftext - Fatal编程技术网

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

Php 将阿拉伯语写入图像时出错,php,arabic,imagettftext,Php,Arabic,Imagettftext,我先前的相关问题: 我的问题是: 如果我想在图像中写入 好的,我修复了它,现在输出:احمد 使用此功能: function arab($word){ $w = explode(' ',$word) ; $f = array(array('ا','أ'),'ب','ت','ث','ج','ح','د','ذ','ر','ز','س','ش','ص','ض','ط','ظ','ع','غ','ف','ق','ك','ل','م','ن','ه','و','ى'

我先前的相关问题:

我的问题是:

  • 如果我想在图像中写入
  • 好的,我修复了它,现在输出:
    احمد
使用此功能:

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

}
但新的问题是:

احمد

(分开的字母)应为:


如何修复此问题?

您必须使用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);
}

我使用此选项时没有任何问题:

更新: 我分叉了Persian-Log2Vis并更改了一些代码以使其正常工作。

您的反转阿拉伯字符的方式没有考虑到连接符号的性质。 然而,这是一个有效的技巧来解决PHP/GD不能自动支持RTL语言(如阿拉伯语)的问题

您需要做的是使用完全符合您意图的库

确保您的PHP文件编码为unicode/UTF
e、 g.>打开记事本>另存为>编码为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); 
?>

产出:


对不起,我不明白这个问题。如果你想让
显示为
的话,为什么要通过这个函数来传递它呢?因为它看起来像
的右边,但字母之间有空格,所以似乎破坏了阿拉伯字符串,
$f
$t
行在打印时是无效的语法,如果你将C&P添加到一个普通的编辑器中,它们会恢复正常。如果有人愿意将其与Laravel集成,请在“classmap”部分下将文件路径添加到composer.json。这比上面的解决方案更适合我。上面使用ar php的解决方案丢弃了一些字符,特别是由Android系统格式化的日期创建的非西方数字。