Php 如何将非英语字符(如阿拉伯语或波斯语字符)写入图像?
如何使用PHP GD库将阿拉伯语或波斯语字符写入图像Php 如何将非英语字符(如阿拉伯语或波斯语字符)写入图像?,php,gd,Php,Gd,如何使用PHP GD库将阿拉伯语或波斯语字符写入图像 i、 e.“احان”尝试使用imagettftext <?php // http://localhost/test.php?text=احسان // test.php file $font = 'C:/Windows/Fonts/Arial.ttf'; $text = $_GET['text']; // [switch to right to left] // try comparing of using this bloc
i、 e.“احان”尝试使用imagettftext
<?php
// http://localhost/test.php?text=احسان
// test.php file
$font = 'C:/Windows/Fonts/Arial.ttf';
$text = $_GET['text'];
// [switch to right to left]
// try comparing of using this block and not using this block
$rtl = array();
for($i=0; $i<strlen($text); $i+=2) {
$rtl[] = substr($text, $i, 2);
}
$rtl = array_reverse($rtl);
$rtl = implode($rtl);
$text = $rtl;
// [/switch to right to left]
$im = imagecreatetruecolor(65, 35);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 500, 100, $white);
imagettftext($im, 12, 0, 10, 20, $black, $font, $text);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
使用此功能将文本传递给imagettftext
<?php
function revUni($text) {
$wordsArray = explode(" ", $text);
$rtlCompleteText='';
for ($i = sizeOf($wordsArray); $i > -1; $i = $i-1) {
//$lettersArray = explode("|", str_replace(";|", ";", $wordsArray[$i]));
$lettersArray = explode(";", $wordsArray[$i]);
$rtlWord='';
for ($k = sizeOf($lettersArray); $k > -1; $k = $k-1) {
if (strlen($lettersArray[$k]) > 1) { // make sure its full unicode letter
$rtlWord = $rtlWord."".$lettersArray[$k].";";
}
}
$rtlCompleteText = $rtlCompleteText." ".$rtlWord;
}
return $rtlCompleteText;
}
?>
像数组一样简单地反转阿拉伯字符是行不通的。您需要考虑阿拉伯字形,并用准确的Unicode符号替换每个字形。请参见此处,了解类似的问题和解决方案:我已经基于库编写了一个composer包,我不记得它的名称。我修改了库并修复了它的一些bug
你可以找到来源。您还可以通过运行以下命令使用composer安装它:
composer require quince/persian-gd
- 它与波斯人的性格没有关系
- 它是可定制的
- 字符串不会溢出图像画布
请测试它,并发送错误报告,建议和
谢谢你面临什么问题?你在使用什么代码?@Bruce Aldridge你的编辑缩小了问题的范围。让它在网络搜索中不那么有用。我在泰米尔语中也有类似的问题。fontsit会打印“不”而不是“不”