16

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?

cigien
  • 57,834
  • 11
  • 73
  • 112
Ahmad Sattar
  • 321
  • 2
  • 10
  • I've never been great with regular expressions; I'm more of a "loop through the string" kinda guy. But the concept seems to be this: use your function utf8_strrev to reverse the whole thing, then isolate each contiguous string of numbers within the string and apply utf8_strrev to them (within the string). I could do it easily via a loop, but I'd probably get a bunch of down-votes. ;) – BrettFromLA Apr 16 '14 at 21:50

4 Answers4

5

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
Master_ex
  • 789
  • 6
  • 12
  • `/([^\d]|\d+)/us` can be simplified to `/\D|\d+/u`. The capture group needlessly increases the step count, `[^\d]` is the same as `\D`, and the `s` pattern modifier only effects `.` (non-literal dot) in regex patterns. This answer is based on [How to reverse a Unicode string](https://stackoverflow.com/a/3749543/2943403) – mickmackusa Dec 26 '21 at 22:45
1
<?php

  function hebstrrev($string, $revInt = false, $encoding = 'UTF-8'){
    $mb_strrev = function($str) use ($encoding){return mb_convert_encoding(strrev(mb_convert_encoding($str, 'UTF-16BE', $encoding)), $encoding, 'UTF-16LE');};

    if(!$revInt){
      $s = '';
      foreach(array_reverse(preg_split('/(?<=\D)(?=\d)|\d+\K/', $string)) as $val){
        $s .= ctype_digit($val) ? $val : $mb_strrev($val);
      }
      return $s;
    } else {
      return $mb_strrev($string);
    }
  }

  echo hebstrrev("מחל 65 דגכ"); // outputs: כגד 65 לחמ
  echo hebstrrev("מחל 65 דגכ", true); // outputs: כגד 56 לחמ

?>

This function reverses the string with an optional parameter to reverse the integers within the string as well. It also allows to change the encoding of the string, so it should be universal, no matter what language.

Xorifelse
  • 7,878
  • 1
  • 27
  • 38
1

This will help you :

<?php
$s = iconv("ISO-8859-8", "UTF-8", hebrev(iconv("UTF-8", "ISO-8859-8", $s)));
?>
s4suryapal
  • 1,880
  • 1
  • 18
  • 26
  • _Why_ "This will help"? This answer is missing its educational explanation. – mickmackusa Dec 26 '21 at 22:15
  • @mickmackusa check date and time of Answer , answer was given on 13-7-2016. Keep your interest in solution not in other things. Thanks – s4suryapal Dec 28 '21 at 09:36
  • I curate all content on this site regardless of the post's age. Old unexplained answer diminish the value of my efforts when I hammer a new question with an old page. You may [edit] your answer at any time. – mickmackusa Dec 28 '21 at 09:42
0

I would recommend using this function http://php.net/manual/de/function.imagettfbbox.php

<?php
$white = imagecolorallocate($background, 255, 255, 255);
$fontfile = "davidtr.ttf";
//text how it should be displayed
$string = "מחלדגכ"; //will result in:
                    //  -------------------
                    // |            מחלדגכ|
                    // |                  |
                    // |                  |
                    //  -------------------
$helper = imageTTFBbox(12,0,$fontfile,$string);
imagettftext($background, 12, 0, 15+imagesx($background)-abs($helper[4] - $helper[0]), 17, $white, $fontfile, $string);
?>

so basically you calculate the width of the text, get the width of the image, subtract those and add a padding (15). Note that the text, fontfile, fontsize and angle must be the same for both imageTTFBbox and imagettftext for it to work

If you also have to reverse the text I would recommend the solution by Master_ex.

Code not tested yet.

cigien
  • 57,834
  • 11
  • 73
  • 112