2

I tried to write an Arabic text in an image with PHP. This is my code:

<?php

header("Content-Type: image/png; charset=utf8");
$im = imagecreatetruecolor(150, 30);

// Create some colors
$white = imagecolorallocate($im, 100, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

$font = 'tahoma.ttf';
$t = strrev('إن الدين عند الله الإسلام');
$text=utf8_encode($t);
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
?>

Thank you.

Sky
  • 4,244
  • 7
  • 54
  • 83
AboSami
  • 259
  • 4
  • 15

2 Answers2

3

What’s the output?

The strrev function is not unicode compatible http://de2.php.net/manual/en/function.strrev.php See the comments there for examples of unicode compatible ones.

Also see the imagettftext page, I think the following comment is exactly what you need: http://de2.php.net/manual/en/function.imagettftext.php#97767

Kissaki
  • 8,810
  • 5
  • 40
  • 42
  • With your original code? Did you try / read the links I posted? – Kissaki Sep 17 '10 at 09:57
  • **Sorry, but this wont work**. The link to the comment treats Arabic chars as an array. Which assumes only one form of the char is valid, which is not. Arabic chars change shape based on position in the word, a glyph. For a similar question and an answer: [http://stackoverflow.com/questions/993265/writing-text-with-diacritic-nikud-vocalization-marks-using-pil-python-imag/25727238#25727238](http://stackoverflow.com/questions/993265/writing-text-with-diacritic-nikud-vocalization-marks-using-pil-python-imag/25727238#25727238) – Nasser Al-Wohaibi Sep 09 '14 at 21:46
  • 1
    for PHP http://stackoverflow.com/questions/7780339/write-arabic-to-image-error/25753686#25753686 – Nasser Al-Wohaibi Sep 09 '14 at 22:27
0

This will work for you:

  1. include("fagd.php");
  2. use imagettftext($im, 20, 0, 10, 20, $black, $font, fagd($text));

fagd.php codes are here:

http://cdn.shirazitco.ir/fagd.txt

ShirazITCo
  • 1,041
  • 6
  • 23
  • 38
  • amazing solution.. worked perfect for me !! but the link is broken so i added another link: http://www.koders.com/php/fidBBD43C6EF0FFB00FED0AC734BC2EFB293A6CBBC3.aspx – Zorox Jul 26 '12 at 11:31
  • 1
    hmmm i tried this but it didn't work... could you post how you did it? I still got garbled (not Arabic) characters. – user961627 Aug 10 '12 at 19:30