3

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

Paebbels
  • 15,573
  • 13
  • 70
  • 139
Zak
  • 591
  • 2
  • 15
  • 37

1 Answers1

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

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.

timclutton
  • 12,682
  • 3
  • 33
  • 43
  • no, this is not the answer to my question: this is a positioning of the entire text, not an RTL css-like... you know, arabian is written flag-right – Zak Oct 11 '17 at 15:48
  • I've added a picture of what I need – Zak Oct 11 '17 at 15:50
  • @Zak Your question is not clear. You explicitly state "I mean a css-like RTL, so aligned-flag on the right". Aligning text is what I've shown here. Thank you for editing your question to show the result you want, now please edit it show the current problem you have. – timclutton Oct 11 '17 at 15:51
  • I wrote css-like RTL, and I've also wrote "arabic language", so... it's pretty clear. this is the command in css to do what I want, and I need it in php-gd – Zak Oct 11 '17 at 15:58
  • @Zak Please enlighten me: _what_ is the command in CSS to which you are referring? `text-align: right;` or `direction: rtl;` or something else? – timclutton Oct 11 '17 at 16:09
  • I wrote RTL, so direction:rtl – Zak Oct 12 '17 at 07:18
  • nope, wrong again. try with a real text: https://www.antiwarsongs.org/canzone.php?lang=it&id=38294 – Zak Oct 12 '17 at 12:13
  • @Zak Any more feedback on this? If it's not working please update your question with your full code and details of your PHP version. – timclutton Oct 13 '17 at 12:33
  • I think you haven't changed the code... it's not working. my code is pn the main thread... the only code interested in the argument – Zak Oct 13 '17 at 13:24
  • Your last comment was "nope, wrong again. try with a real text...". I did, and edited my answer showing the correct and expected result. So, no, the code hasn't changed. Because it works for me. You seem convinced it doesn't work for you so I'm trying to help, but you need to provide details, as requested. – timclutton Oct 13 '17 at 13:31
  • my code is there. I've edited the code with your variation. and the result remain LTR.... maybe it depends because of the POST after a textarea (already RTL)? – Zak Oct 13 '17 at 13:56