9

I would like to generate an image with some text on it, the LTR languages seems to be working fine, but when trying it in arabic, it, simply, didn't work, see the screenshot bellow where I draw 3 text strings with the presented text code

enter image description here

Here my test code (with some comments):

// Create a 300*300 image
$im = imagecreate(300, 300);

// Yellow transparent background and blue text
$bg = imagecolorallocatealpha($im, 255, 255, 0, 45);
$textcolor = imagecolorallocate($im, 0, 0, 255);

// Write an LTR string
imagestring($im, 5, 250, 100, 'test', $textcolor);

$font = 'DroidKufi-Regular.ttf'; // specify the fonts file
$text = "تجربة";
// Add the text
imagettftext($im, 10, 0, 10, 20, $textcolor, $font, $text);


// set a red text color 
$textcolor = imagecolorallocate($im, 255, 0, 0);

// strrev doesn't seem to solve the problem
$text = strrev( $text );
// add the text
imagettftext($im, 10, 0, 30, 250, $textcolor, $font, $text);

imagefilledrectangle ($im, 0, 0, 1, 1, $bg);

// Output the image
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
Abu Romaïssae
  • 3,841
  • 5
  • 37
  • 59
  • You've tried the [solution here](http://www.php.net//manual/en/function.imagettftext.php#91028) i'm assuming? Or [this one maybe?](http://stackoverflow.com/questions/2444015/right-align-text-in-an-image-with-imagettftext-php).. perhaps you didn't, maybe you only tried [this one?](http://forums.phpfreaks.com/topic/44370-solved-make-text-string-read-from-right-to-left-in-imagettftext-function/) – Ohgodwhy Jun 07 '14 at 00:18
  • Yes you are right, but I don't think this one http://stackoverflow.com/questions/2444015/right-align-text-in-an-image-with-imagettftext-php will help me, but I'll give it a try, thanks for the fast answer :) – Abu Romaïssae Jun 07 '14 at 00:22
  • Actually that solution doesn't point near my problem – Abu Romaïssae Jun 07 '14 at 00:27
  • Did you [check this](http://www.php.net/manual/en/function.hebrev.php#87950) on [hebrev](http://www.php.net/manual/en/function.hebrev.php) ? – The Alpha Jun 07 '14 at 00:52
  • the one I talked about is this: http://stackoverflow.com/a/2444052/1358670 and I didn't try anything for hebrew since I only need a working solution for arabic – Abu Romaïssae Jun 07 '14 at 00:55
  • I'm not entirely sure which problem you're trying to solve, since it isn't immediately obvious from your question. Are you trying to change the alignment of the text? – user2428118 Jun 11 '14 at 12:31

2 Answers2

8

Finally I've found a solution in some other forums, which is this tiny library word2uni and is worked perfectly, the whole thing is about converting the arabic letters into unicode symboles, practically, converting this:

$text = 'العربية'; 

to this:

$text = 'ﺔﻴﺑﺮﻌﻟﺍ';

and using, that library, which is making this conversion, so in my previous code it would work like that (after including the library):

// Create a 300*300 image
$im = imagecreate(300, 300);

// Yellow transparent background and blue text
$bg = imagecolorallocatealpha($im, 255, 255, 0, 45);
$textcolor = imagecolorallocate($im, 0, 0, 255);

// Write an LTR string
imagestring($im, 5, 250, 100, 'test', $textcolor);

$font = 'DroidKufi-Regular.ttf'; // specify the fonts file
$text = "تجربة";
$text = word2uni( $text );
// Add the text
imagettftext($im, 10, 0, 10, 20, $textcolor, $font, $text);


// set a red text color 
$textcolor = imagecolorallocate($im, 255, 0, 0);

// strrev doesn't seem to solve the problem
$text = strrev( $text );
// add the text
imagettftext($im, 10, 0, 30, 250, $textcolor, $font, $text);

imagefilledrectangle ($im, 0, 0, 1, 1, $bg);

// Output the image
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);

( the main thread )

Abu Romaïssae
  • 3,841
  • 5
  • 37
  • 59
5

you can check this library

http://www.ar-php.org/Glyphs-example-php-arabic.html

here is an example how to use it

require('../I18N/Arabic.php');  
$Arabic = new I18N_Arabic('Glyphs'); 
$text = 'بسم الله الرحمن الرحيم';  
$text = $Arabic->utf8Glyphs($text);
Salem Ahmed
  • 1,067
  • 2
  • 12
  • 18