jeudi 16 avril 2015

How to convert UTF-8 URDU text to image on the fly in Php?

اردو کے فونٹ کے مسائل


میرے ویب پیچ کے فونٹ ، براوزر کی وجہ سے بدل جاتے ہیں، میں اپنے صفحے کو امیج بنا کر براوزر کو دینا چایتا ہوں۔


I have tried a number of tutorials / examples and searches. I could not find solution.



  • A Tip: ( Hope it will make someone's life easier.)


A big trouble , I resolved in image creation was use of Windows Share-Point to save my .php files. All files i was saving , had something UTF BOM .


When I saved same file using simple Notepad in UTF-8 encoding , my image creation in PHP started.



  • My Problem.


I have a sentence in URDU ( a language Spoken by a billion in South Asia ), say a string - saved in UTF-8 , perfectly being saved in database and on shown at web browsers.


Unlike English, Urdu letters join to make words, While joining , their apparent shape is cropped - size or style.


I am working on this code



<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
//$SourceFile is source of the image file to be watermarked
//$WaterMarkText is the text of the watermark
//$DestinationFile is the destination location where the watermarked images will be placed
//Delete if destinaton file already exists
@unlink($DestinationFile);

//This is the vertical center of the image
$top = getimagesize($SourceFile);
$top = $top[1]/2;
list($width, $height) = getimagesize($SourceFile);

$image_p = imagecreatetruecolor($width, $height);

$image = imagecreatefromjpeg($SourceFile);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);

//Path to the font file on the server. Do not miss to upload the font file
$font = 'arial.ttf';

//Font sie
$font_size = 36;

//Give a white shadow
$white = imagecolorallocate($image_p, 255, 255, 155);
imagettftext($image_p, $font_size, 0, 10, $top, $white, $font, $WaterMarkText);

//Print in black color
$black = imagecolorallocate($image_p, 0, 0, 0);
imagettftext($image_p, $font_size, 0, 8, $top-1, $black, $font, $WaterMarkText);

if ($DestinationFile<>'') {

imagejpeg ($image_p, $DestinationFile, 100);

} else {

header('Content-Type: image/jpeg');

imagejpeg($image_p, null, 100);

};

imagedestroy($image);

imagedestroy($image_p);

};

?>

<?php
/*
// The text to draw
require('../../../I18N/Arabic.php'); // It converts the left side language to right side
$Arabic = new I18N_Arabic('Glyphs'); //
$font = './DroidNaskh-Bold.ttf';
$text = "جب درخشاں ہوں ستاروں کے چراغ";

$text = $Arabic->utf8Glyphs('جب درخشاں ہوں ستاروں کے چراغ ');
It gave me ﻍﺍﺭچ ےک ںﻭﺭﺎﺘﺳںﻭہ ںﺎﺸﺧﺭﺩ ﺐﺟ , reversing the string ,
*/

$text = "ﻍﺍﺭچ ےک ںﻭﺭﺎﺘﺳںﻭہ ںﺎﺸﺧﺭﺩ ﺐﺟ"; // Problem in joining of Urdu -

// Text
// Sequence of characters reversed in string using utf8Glyphs( ) ;

$SourceFile = 'nature (28).jpg';//Source image
$DestinationFile = 'watermarked/sky.jpg';//Destination path
//Call the function to watermark the image
watermarkImage ($SourceFile, $text, $DestinationFile);

//Display watermarked image if desired
if(file_exists($DestinationFile)){
echo "<img src=\"watermarked/sky.jpg\">";
echo "<p>The image has been watermarked at '".$DestinationFile."'</p>";
}
?>


My image creation on the FLY for other then UTF-8 is working fine.


Kind regards.


Aucun commentaire:

Enregistrer un commentaire