php - 如何在 PHP 中将文本转换为图像时加入乌尔都语字母

标签 php urdu

我使用TIC将文本转换为图像。

我对此进行了很多搜索,但似乎是 Unicode 问题(开头中间和结尾字母的 unicode)或者可能是内容类型,因为图像是 PNG 格式。

如果我在没有图像转换的情况下使用 content type text/htmlcharset=UTF-8 进行回显,我会得到带有连接乌尔都语字母的所需输出。

require_once 'lib/tic.php';
$text="زہرہ نور ";

TIC::factory('C:\Windows\Fonts\Nastalique.ttf')
->setText($text)
->setPadding(10)
->setBgColor('ff0000')
->setFontColor(0xff, 0xff, 0x00)
->setFontSize(24)->create(true);

退出放置为

ز ہ ر ہ  ن و ر 

最佳答案

你可以这样做:

$text = "زہرہ نور";

// Make it RTL
preg_match_all('/([^\d]|\d+)/us', $text, $ar);
$text = join('',array_reverse($ar[0]));

// Set font
$font = 'C:\Windows\Fonts\Nastalique.ttf';

// Create the image
$im = imagecreatetruecolor(160, 160);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

// Create some colors
imagefilledrectangle($im, 0, 0, 159, 159, $white);

// Set the headers
header('Content-type: image/gif');

// Add the text
imagettftext($im, 12, 0, 20, 20, $black, $font, $text);
imagegif($im);
imagedestroy($im);

如果不适合您,您可以选择使用 php-gd-farsi .

使用方法

只需将库复制到您的 PHP 目录即可。使用方法很简单:

include('php-gd-farsi-master/FarsiGD.php');
$gd = new FarsiGD();

....
// then convert your text:
$tx = $gd->persianText($str, 'fa', 'normal');

完整代码

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 = './Nastalique.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);

关于php - 如何在 PHP 中将文本转换为图像时加入乌尔都语字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30679625/

相关文章:

php - div 的 jquery 加载函数不起作用

php - 声明一个空类不好吗?

python - 如何使用谷歌翻译将乌尔都语单词音译为罗马乌尔都语

javascript - 我的 AJAX 脚本没有给出任何输出

php - 对于相同的测试套件,MySQL 5.7 比 5.5 慢 3 倍

php - CakePHP 3 中 MySQL 存储函数的 ORDER BY

c# - 根据不存在的空间拆分乌尔都语单词

c# - 从 sql 发送到手机的乌尔都语消息格式不可读

php - 引号(引号)在乌尔都语中产生问题