在GD中显示从右到左连接的语言(如波斯语和阿拉伯语)-可能的PHP错误 [英] Displaying right to left connected languages (like persian and Arabic) in GD - Possible PHP bug
问题描述
我想用php gd库创建一个带有文本的图像.
I want to create an image with a text on it with php gd library.
一切都很好,但是当我尝试使用imagefttext()
从右向左连接的语言(如波斯语)写一个字时,我的文本从左向右(反向)呈现,并且字符不再连接.
everything is fine, but when I try to write a word from a right to left connected language (like Persian ) using imagefttext()
, my text is rendering from left to right (inverse) and the chars are not connected any more .
关联字符的示例:ماه 未连接字符的示例:ماه
example of connected chars : ماه example of not connected chars : م ا ه
这是我的代码:
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);
渲染的图像:
我知道我的问题不是编码.因为我已经尝试过了:
I know my problem is not encoding.because I already tried this :
$text = mb_convert_encoding($text, "HTML-ENTITIES", "UTF-8");
结果是一样的.
有一些可用的库可以解决此问题.并且我知道你们中的大多数人都不熟悉波斯语或阿拉伯语语言,但是我的问题是为什么gd
本身不支持从右到左连接的语言?
there are some library available that maybe can solve this problem. and I know that most of you are not familiar with Persian or Arabic languages, but my question is why gd
does not support right to left connected languages natively ?
这可能是gd库中的错误吗?
could this be a bug in gd library ?
推荐答案
似乎没有任何帮助(设置正确的语言环境),原生PHP支持存在漏洞,这可能就是创建第三方软件包的原因:
Seems that nothing helps (setting correct locale), native PHP support is buggy, that's probably why 3rd party package was created:
魅力十足:
只需将库复制到您的PHP目录(不必是管理员).用法很简单:
Just copy the library to your PHP directory (don't have to be the admin). The usage is simple:
// init:
include('php-gd-farsi-master/FarsiGD.php');
$gd = new FarsiGD();
....
// then convert your text:
$tx = $gd->persianText($str, 'fa', 'normal');
您甚至不必设置正确的语言环境! :-)
You don't even have to set the correct locale! :-)
整个示例代码:
<?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);
?>
这篇关于在GD中显示从右到左连接的语言(如波斯语和阿拉伯语)-可能的PHP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!