2014-06-07 13 views
5

Vorrei creare un'immagine con del testo, i linguaggi LTR sembrano funzionare bene, ma quando lo si prova in arabo, semplicemente, non ha funzionato , vedere il muggito screenshot dove traggo 3 stringhe di testo con il codice testo presentatoScrivi testo RTL (arabo) per immagini con PHP

enter image description here

Ecco il mio codice di prova (con alcuni commenti):

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

Hai provato la [soluzione qui] (http://www.php.net //manual/en/function.imagettftext.php#91028) sto assumendo? O [questo forse?] (Http://stackoverflow.com/questions/2444015/right-align-text-in-an-image-with-imagettftext-php) .. forse non l'hai fatto, forse hai solo provato [questo?] (http://forums.phpfreaks.com/topic/44370-solved-make-text-string-read-from-right-to-left-in-imagettftext-function/) – Ohgodwhy

+0

Sì hai ragione , ma non penso che questo http://stackoverflow.com/questions/2444015/right-align-text-in-an-image-with-imagettftext-php mi aiuterà, ma ci provo , grazie per la rapida risposta :) –

+0

In realtà quella soluzione non punta vicino al mio problema –

risposta

6

Finalmente ho trovato una soluzione in alcuni altri forum, che è questa piccola libreria word2uni ed è funzionato alla perfezione, il tutto è sulla conversione delle lettere arabe in symboles Unicode, in pratica, la conversione di questo:

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

a questo:

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

e l'utilizzo, quella biblioteca, che sta facendo questa conversione, così nel mio codice precedente che avrebbe funzionato così (dopo compresa la biblioteca):

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

il collegamento viene interrotto per word2uni. ne ho bisogno. aiuto per favore. – hosein

+0

@hosein, contattami via email, posso inviarcelo –

+0

non riesco a trovare la tua email in StackOverflow. per favore dammi la tua email – hosein

0

è possibile controllare questa libreria

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

ecco un esempio come usarlo

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