2014-06-07 5 views
5

Ich möchte ein Bild mit etwas Text darauf generieren, scheint die LTR-Sprachen gut zu funktionieren, aber wenn es auf Arabisch versucht, es einfach nicht funktioniert siehe den Screenshot unten, wo ich zeichne 3 Text-Strings mit dem vorgestellten TextcodeSchreibe RTL (arabisch) Text zu Bild mit PHP

enter image description here

Hier ist mein Test-Code (mit Kommentaren):

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

Sie haben die [Lösung hier] versucht, verwenden (http://www.php.net //manual/en/function.imagetttext.php#91028) nehme ich an? Oder [vielleicht dieser?] (Http://stackoverflow.com/questions/2444015/right-align-text-in-a-image-with-imagetttext-php) .. vielleicht hast du es nicht, vielleicht hast du es nur versucht [dieser?] (http://forums.phpfreaks.com/topic/44370-solved-make-text-string-read-from-right-to-left-in-imagettftext-function/) – Ohgodwhy

+0

Ja, Sie haben Recht , aber ich denke nicht, dass dieser http://stackoverflow.com/questions/2444015/right-align-text-in-an-image-with-imagetttext-php mir helfen wird, aber ich werde es versuchen Danke für die schnelle Antwort :) –

+0

Eigentlich zeigt diese Lösung nicht in der Nähe meines Problems –

Antwort

6

Endlich habe ich eine Lösung gefunden einige andere Foren, das ist diese kleine Bibliothek word2uni und ist perfekt gearbeitet, ist die ganze Sache über die arabischen Buchstaben in Unicode symboles Umwandlung, praktisch, diese Umwandlung:

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

dazu:

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

und verwendet wird, dass die Bibliothek, die macht diese Umwandlung, so in meinem vorherigen Code würde es so arbeitet (nach der Bibliothek einschließlich):

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

der Link ist für Word2uni gebrochen. ich brauche es. Hilfe bitte. – hosein

+0

@ Hosein, kontaktieren Sie mich per E-Mail, ich kann es Ihnen senden –

+0

Ich kann Ihre E-Mail nicht in Stackoverflow finden. bitte geben Sie mir Ihre E-Mail – hosein

0

Sie diese Bibliothek überprüfen

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

hier ist ein Beispiel, wie es

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