我想生成一个带有一些文字的图像,LTR语言似乎工作得很好,但是当用阿拉伯语尝试它时,它简单地说不起作用,请看下面的截图我绘制3个文本带有显示文本代码的字符串
这是我的测试代码(带一些注释):
// Create a 300*300 image
$im = imagecreate(300, 300);
// Yellow transparent background and blue text
$bg = imagecolorallocatealpha($im, 255, 255, 0, 45);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write an LTR string
imagestring($im, 5, 250, 100, 'test', $textcolor);
$font = 'DroidKufi-Regular.ttf'; // specify the fonts file
$text = "تجربة";
// Add the text
imagettftext($im, 10, 0, 10, 20, $textcolor, $font, $text);
// set a red text color
$textcolor = imagecolorallocate($im, 255, 0, 0);
// strrev doesn't seem to solve the problem
$text = strrev( $text );
// add the text
imagettftext($im, 10, 0, 30, 250, $textcolor, $font, $text);
imagefilledrectangle ($im, 0, 0, 1, 1, $bg);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
答案 0 :(得分:6)
最后,我在其他论坛找到了一个解决方案,就是这个小型库word2uni并且工作得很好,整个过程就是将阿拉伯字母转换为unicode符号,实际上,转换为:
$text = 'العربية';
到此:
$text = 'ﺔﻴﺑﺮﻌﻟﺍ';
并使用正在进行此转换的库,所以在我之前的代码中它会像那样(在包含库之后):
// Create a 300*300 image
$im = imagecreate(300, 300);
// Yellow transparent background and blue text
$bg = imagecolorallocatealpha($im, 255, 255, 0, 45);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write an LTR string
imagestring($im, 5, 250, 100, 'test', $textcolor);
$font = 'DroidKufi-Regular.ttf'; // specify the fonts file
$text = "تجربة";
$text = word2uni( $text );
// Add the text
imagettftext($im, 10, 0, 10, 20, $textcolor, $font, $text);
// set a red text color
$textcolor = imagecolorallocate($im, 255, 0, 0);
// strrev doesn't seem to solve the problem
$text = strrev( $text );
// add the text
imagettftext($im, 10, 0, 30, 250, $textcolor, $font, $text);
imagefilledrectangle ($im, 0, 0, 1, 1, $bg);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
答案 1 :(得分:2)
您可以查看此库
http://www.ar-php.org/Glyphs-example-php-arabic.html
这是一个如何使用它的例子
require('../I18N/Arabic.php');
$Arabic = new I18N_Arabic('Glyphs');
$text = 'بسم الله الرحمن الرحيم';
$text = $Arabic->utf8Glyphs($text);