我想用php-gd库创建一个带有文本的图像。
一切都很好,但是当我试图用imagefttext()
从右到左连接的语言(如波斯语)写一个单词时,我的文本从左到右(逆)呈现,字符不再连接。
连接字符示例:__
未连接字符示例:__
这是我的代码:
header('Content-Type: image/jpeg');
$thumb_path = "...";
$font_path = "..."
$img = imagecreatefromjpeg($thumb_path);
$color = "...";
// __month is a Persian word : ( م ا ه --> ماه )
$text = $months." ".__month;
imagefttext($img,29, 10, 230, 135, $color, $font_path, $text); // <--
imagejpeg($img);
渲染图像:
我知道我的问题不是编码。因为我已经尝试过:
$text = mb_convert_encoding($text, "HTML-ENTITIES", "UTF-8");
结果是一样的。
有一些图书馆可以解决这个问题。我知道你们大多数人不熟悉波斯语或阿拉伯语,但我的问题是为什么
gd
不支持从右到左的本地连接语言?这可能是gd库中的一个bug吗?
最佳答案
似乎没有什么帮助(设置正确的区域设置),本地PHP支持是错误的,这可能是为什么创建第三方包:
使用php-gd-farsi
作为一种魅力:
只需将库复制到php目录(不必是管理员)。用法很简单:
// init:
include('php-gd-farsi-master/FarsiGD.php');
$gd = new FarsiGD();
....
// then convert your text:
$tx = $gd->persianText($str, 'fa', 'normal');
你甚至不必设置正确的语言环境!-)
整个示例代码:
<?php
include('php-gd-farsi-master/FarsiGD.php');
$gd = new FarsiGD();
// Create a 300x100 image
$im = imagecreatetruecolor(300, 100);
$red = imagecolorallocate($im, 0xFF, 0x00, 0x00);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
// Make the background red
imagefilledrectangle($im, 0, 0, 299, 99, $red);
// Path to our ttf font file
//$font_file = './Vera.ttf';
$font_file = './cour.ttf';
// Draw the text 'PHP Manual' using font size 13
$text = imagecreatetruecolor(200, 60);
imagefilledrectangle($text, 0, 0, 200, 60, $red);
$str = '**ماه**';
$tx = $gd->persianText($str, 'fa', 'normal');
imagefttext($text, 24, 10, 10, 50, $black, $font_file,$tx );
$im = $text;
// Output image to the browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>