我想用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库中的错误吗?
似乎没有任何帮助(设置正确的语言环境),原生PHP支持存在问题,这可能就是创建3rd party包的原因:
发挥魅力:
只需将库复制到您的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);
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句