imagettftext和阿拉伯语:如何在RTL模式下编写?

时间:2017-10-11 09:00:28

标签: php gd arabic right-to-left imagettftext

我需要以RTL模式写入imagettftext,' caus阿拉伯语就是这样的:我不是故意要还原这些字母,我的意思是一个类似CSS的RTL(方向:rtl) ,右边的对齐标志......我怎么能?

我简单的代码:

require('static/I18N/Arabic.php');
$Arabic = new I18N_Arabic('Glyphs');
$font="static/ArabicModern-Light.ttf";
$testo=$Arabic->utf8Glyphs($testo);
imagettftext($im, 26, 0, 560, 345, $textcolor, $font, "\"".$testo."\"");

谢谢!

enter image description here

1 个答案:

答案 0 :(得分:-1)

最终查找并下载Ar-PHP 4.0后,我更新了此答案以使用阿拉伯语。由于我不懂阿拉伯语,我使用了Arabic Wikipedia page中的'العربية'文字。

<强>来源:

<?php

require_once('./Arabic.php');

$iw = 300; // image width.
$ih = 150; // image height.

$size = 40;
$angle = 0;
$font = 'arial';
$text = 'العَرَبِيَّة';

$obj = new I18N_Arabic('Glyphs');
$text = $obj->utf8Glyphs($text);

$box = imagettfbbox($size, $angle, $font, $text);
$tw = abs($box[6] - $box[4]); // text width.
$th = abs($box[7] - $box[1]); // text height.

$im = imagecreatetruecolor($iw, $ih);
imagefill($im, 0, 0, 0x00c0c0c0); // set a grey background.

// left aligned.
imagettftext($im, $size, $angle, 0, $th, 0x00000000, $font, $text);

// centre aligned.
imagettftext($im, $size, $angle, ($iw / 2) - ($tw / 2), $th * 2, 0x00000000, $font, $text);

// right aligned.
imagettftext($im, $size, $angle, $iw - $tw, $th * 3, 0x00000000, $font, $text);

header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);

结果(使用PHP 7.1生成):

enter image description here

来自维基百科的参考图片:

enter image description here

编辑: 以下是对text you linkedcomment的前五行的比较,运行我编写的代码上面(显然有一些微小的变化来处理多行):

左侧的原始图像(裁剪),右侧生成的图像。

enter image description here enter image description here

除了没有正确对齐之外,这是匹配。