imagettftext and arabic language: how can I write in RTL mode?

≡放荡痞女 提交于 2021-02-08 10:01:49

问题


I need to write in RTL mode to imagettftext, 'caus arabic language is in this way: and I don't mean to revert the letters, I mean a css-like RTL (direction:rtl), so aligned-flag on the right... how can I?

My easy code:

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."\"");

Thanks!


回答1:


After eventually finding and downloading Ar-PHP 4.0 I've updated this answer to use Arabic. Since I don't understand Arabic I've used the text 'العَرَبِيَّة' from the Arabic Wikipedia page.

Source:

<?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);

Result (generated with PHP 7.1):

Reference image from Wikipedia:


Edit: Below is a comparison of the first five lines from the text you linked to in your comment, run through the code I wrote above (obviously with some minor changes to handle multiple lines):

Your original image (cropped) on the left, my generated image on the right.

Aside from not aligning correctly it's a match.



来源:https://stackoverflow.com/questions/46684059/imagettftext-and-arabic-language-how-can-i-write-in-rtl-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!