2
votes

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!

enter image description here

1

1 Answers

1
votes

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):

enter image description here

Reference image from Wikipedia:

enter image description here


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.

enter image description hereenter image description here

Aside from not aligning correctly it's a match.