2014-06-07 12 views
5

Chciałbym wygenerować obraz z pewnym tekstem na nim, języki LTR wydają się działać dobrze, ale gdy próbuję go w języku arabskim, to po prostu nie działa , zobacz ryk screenshot gdzie rysuję 3 ciągi tekstowe z prezentowanym kodzie tekstNapisz tekst RTL (arabski) do obrazu za pomocą PHP

enter image description here

Oto mój kodu testu (z pewnymi uwagi):

// 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); 
+0

Próbowałeś na rozwiązanie [tutaj] (http://www.php.net //manual/en/function.imagettftext.php#91028) Zakładam? Lub [ten może?] (Http://stackoverflow.com/questions/2444015/right-align-text-in-an-image-with-imagettftext-php) .. może nie, może próbowałeś tylko [ten?] (http://forums.phpfreaks.com/topic/44370-solved-make-text-string-read-from-right-to-left-in-imagettftext-function/) – Ohgodwhy

+0

Tak, masz rację , ale nie sądzę, że ten jeden http://stackoverflow.com/questions/2444015/right-align-text-in-an-image-with-imagettftext-php pomoże mi, ale spróbuję, ale spróbuję , dzięki za szybką odpowiedź :) –

+0

W rzeczywistości to rozwiązanie nie wskazuje na mój problem. –

Odpowiedz

6

Wreszcie znalazłem rozwiązanie kilka innych forów, czyli ta niewielka biblioteka word2uni i jest doskonale działało, cała sprawa jest o konwersji arabskiej litery na Unicode symboles, praktycznie, przekształcając w ten sposób:

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

do tego:

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

i stosując tę ​​bibliotekę, co czyni ta konwersja, tak w moim poprzednim kodzie to działa w ten sposób (po tym biblioteki):

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

+1

link jest łamany dla word2uni. potrzebuję tego. prosimy o pomoc. – hosein

+0

@hosein, skontaktuj się ze mną przez e-mail, mogę wysłać to do Ciebie –

+0

Nie mogę znaleźć Twojego adresu e-mail w stackoverflow. proszę podać mi swój adres e-mail – hosein

0

można sprawdzić tę bibliotekę

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

tutaj jest przykładem, jak go używać

require('../I18N/Arabic.php'); 
$Arabic = new I18N_Arabic('Glyphs'); 
$text = 'بسم الله الرحمن الرحيم'; 
$text = $Arabic->utf8Glyphs($text);