Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make text string read from right to left in imagettftext() function

I want to write a text string from right to left instead of from left to right with the imagettftext (); function

I read in the manual that the angle variable controls this, it says that 0 angle means left to right, so I tried 180, 360 but nothing happens

What angle do I need to put it to get it to write it right to left

I am writing a hebrew text string with a font.ttf that supports hebrew characters

<?php   
$white = imagecolorallocate($background, 255, 255, 255);
$fontfile = "davidtr.ttf";
$string = "מחלדגכ";
imagettftext($background, 12, 360, 3, 17, $white, $fontfile, $string);

?>

i also used this function strrev(),

$white = imagecolorallocate($background, 255, 255, 255);
$fontfile = "davidtr.ttf";
$string = strrev("עברית");
//imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
imagettftext($background, 12, 0, 3, 17, $white, $fontfile, $string);

Now the text is screwed up on the image some letters are white boxes

Then I used this function:

function utf8_strrev($str){
   preg_match_all('/./us', $str, $ar);
   return join('',array_reverse($ar[0]));
}

It helped me a lot, but now it also reversed integers

$string = "מחל 65 דגכ";
echo utf8_strrev($string);
//Now string revered but 65 converted to 56

Can you please give me a better solution that only hebrew characters become reversed, not integers?

like image 338
Ahmad Sattar Avatar asked Apr 16 '14 21:04

Ahmad Sattar


2 Answers

You could change utf8_strrev() like that:

function utf8_strrev($str){
   preg_match_all('/([^\d]|\d+)/us', $str, $ar);
   return join('',array_reverse($ar[0]));
}

That way you match everything that isn't a digit or everything that is a sequence of digits.

So, the string "one 123 two" would result to the string "owt 123 eno".

The array $ar inside utf8_strrev() would be like that after the preg_match_all():

[0] => o
[1] => n
[2] => e
[3] => 
[4] => 123
[5] =>
[6] => t
[7] => w
[8] => o
like image 58
Master_ex Avatar answered Oct 14 '22 19:10

Master_ex


This will help you :

<?php
$s = iconv("ISO-8859-8", "UTF-8", hebrev(iconv("UTF-8", "ISO-8859-8", $s)));
?>
like image 32
s4suryapal Avatar answered Oct 14 '22 19:10

s4suryapal