Using Malayalam language word in imagettftext() - php

I am trying to convert Malayalam(Indian language) words to image using php imagettftext() function. I am using Malayalam font as well. Here is a block of code I am using.
// malayalam word
$utf8str = "കറുപ്പ്";
// buffer output in case there are errors
ob_start();
// create blank image
$im = imagecreatetruecolor(400,40);
$white = imagecolorallocate($im,255,255,255);
imagefilledrectangle($im,0,0,imagesx($im),imagesy($im),$white);
// write the text to image
$font = "anjali.ttf";
$color = imagecolorallocatealpha($im, 50, 50, 50, 0); // dark gray
$size = 20;
$angle = 0;
$x = 5;
$y = 25;
imagettftext($im, $size, $angle, $x, $y , $color, $font, $utf8str);
// display the image, if no errors
$err = ob_get_clean();
if( !$err ) {
header("Content-type: image/png");
imagepng($im);
}
With this code, I get the following output
I have tried many Malayalam fonts and codes from stackoverflow. Can anybody help me with this?

After a long research, I came to know that existing malayalam fonts does not support all the characters. We will have to wait for new fonts to come up that support all the characters.

Related

Gujarati Letters are not displaying properly while using imagefttext() function, PHP

Want to display the half letters like "અર્થ" and "અસત્ય" in imagefttext function PHP but the result something different. Like this.
Or anyone can help me the alternate way?
<?php
header('Content-type: text/html; charset=utf-8');
#Declare fixed values
$textY = 190;
$textX = 130;
$textY2 = 280;
$textX2 = 130;
$textFont = './fonts/notoSans/NotoSansGujarati-Bold.ttf';
// $textFont = './fonts/Shrikhand-Regular.ttf';
$textSize = 54;
$imagesFolder = './images';
$text = "અર્થ";
$text2 = "અસત્ય";
//header("Content-Type: image/png");
header("Content-Type: image/png");
//Creating background image
$im = imagecreatefrompng('https://i.stack.imgur.com/A9Oll.png');
//$im = imagecreatetruecolor(512,512);
// Create the clours to be used.
$yellow = imagecolorallocate( $im, 255, 255, 0 );
imagefttext( $im, $textSize, 0, $textX, $textY, $yellow, $textFont, $text );
imagefttext( $im, $textSize, 0, $textX2, $textY2, $yellow, $textFont, $text2 );
$imageFile = $imagesFolder.'/'.rand(0,500).'.png';
imagepng( $im, $imageFile );
// Unload resources.
imagedestroy( $im );
?>
=> Edits 0.1:
Issue is getting displaying the half letters in the image.
- Output I needed (I did that in photoshop) : desired Output image
- But I'm getting through this code : image getting from code
Looks like this is not possible with Unicode, and has been a problem for a long time. From a PHP developer on a 10 year old bug:
Thanks, now I got it. The problem isn't particularly related to
Bengali, but rather to combining characters in general, which are
not supported by imagettftext().
I attempted this with the Imagick extension, but had the same output.
A possible workaround is to find a font that uses another encoding such as ISCII, and then convert the text to that encoding using mb_convert_encoding() or similar function.
Or perhaps have the image generated by a command-line tool run with exec().

imagettftext and arabic language: how can I write in RTL mode?

I need to write in RTL mode to imagettftext, 'caus arabic language is in this way: and I don't mean to revert the letters, I mean a css-like RTL (direction:rtl), so aligned-flag on the right... how can I?
My easy code:
require('static/I18N/Arabic.php');
$Arabic = new I18N_Arabic('Glyphs');
$font="static/ArabicModern-Light.ttf";
$testo=$Arabic->utf8Glyphs($testo);
imagettftext($im, 26, 0, 560, 345, $textcolor, $font, "\"".$testo."\"");
Thanks!
After eventually finding and downloading Ar-PHP 4.0 I've updated this answer to use Arabic. Since I don't understand Arabic I've used the text 'العَرَبِيَّة' from the Arabic Wikipedia page.
Source:
<?php
require_once('./Arabic.php');
$iw = 300; // image width.
$ih = 150; // image height.
$size = 40;
$angle = 0;
$font = 'arial';
$text = 'العَرَبِيَّة';
$obj = new I18N_Arabic('Glyphs');
$text = $obj->utf8Glyphs($text);
$box = imagettfbbox($size, $angle, $font, $text);
$tw = abs($box[6] - $box[4]); // text width.
$th = abs($box[7] - $box[1]); // text height.
$im = imagecreatetruecolor($iw, $ih);
imagefill($im, 0, 0, 0x00c0c0c0); // set a grey background.
// left aligned.
imagettftext($im, $size, $angle, 0, $th, 0x00000000, $font, $text);
// centre aligned.
imagettftext($im, $size, $angle, ($iw / 2) - ($tw / 2), $th * 2, 0x00000000, $font, $text);
// right aligned.
imagettftext($im, $size, $angle, $iw - $tw, $th * 3, 0x00000000, $font, $text);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
Result (generated with PHP 7.1):
Reference image from Wikipedia:
Edit: Below is a comparison of the first five lines from the text you linked to in your comment, run through the code I wrote above (obviously with some minor changes to handle multiple lines):
Your original image (cropped) on the left, my generated image on the right.
Aside from not aligning correctly it's a match.

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.
You'll need an additional library to perform Arabic glyph joining. Check out AR-PHP.

My captcha image is not being loaded. There is internal server error 505

I have a php file for captcha. But image is not there in web page. I cant find out what is the prob. Please help me. Here is my code.
<?php
ob_start();
session_start();
// Set the content-type
header('Content-Type: image/jpeg');
mimetypes.add_type('application/font-ttf', '.ttf', True)
// Create the image
$im = imagecreatefromjpeg('bg.jpg');
// Create some colors
$R = rand(0,100);
$G = rand(0,100);
$B = rand(0,100);
$cc = imagecolorallocate($im, $R, $G, $B);
// The text to draw
$text = rand(100,10000);
$_SESSION['text'] = $text;
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, rand(40,45), rand(0,1), rand(10,70), rand(38,50), $cc, $font, $text);
$NumberOfLines=15;
imagecolorallocate($im, 15, 142, 210);
$cc=0;
while($cc < $NumberOfLines){
// set random color:::
//assign random rgb values
$c1 = mt_rand(50,200); //r(ed)
$c2 = mt_rand(50,200); //g(reen)
$c3 = mt_rand(50,200); //b(lue)
//test if we have used up palette
if(imagecolorstotal($im)>=255) {
//palette used up; pick closest assigned color
$color = imagecolorclosest($im, $c1, $c2, $c3);
} else {
//palette NOT used up; assign new color
$color = imagecolorallocate($im, $c1, $c2, $c3);
}
// done...
$startH =rand(3,200);
$startTOP = rand(0,8);
$stopH=rand(3,200);
$stopTOP =50;
imageline($im, $startH, $startTOP, $stopH, $stopTOP, $color);
$cc++;
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagejpeg($im);
imagedestroy($im);
?>
Name of this script file is img.php And it is set as src of img
tag like
img src='img.php'
Here arial.ttf file is in the same folder where this php file resides. Please help me for this. This captcha image is not being loaded.
First remove the ob_start() command. It does not make much sense (at least to me).
Then uncomment the header(..) line so that you see error messages in the browser.
I had to do the following things to get the code up and running:
I removed the mimetypes line (is this PHP at all? If yes, a semicolon is missing)
the script did not find the font - I had to use absolute path and set the permissions right
When you are done with debugging and see fancy chars in your browser window, add the header(..) line again.

Generating preview images for DOC, TXT and RTF files

I've been trying to find a method of achieving this for awhile now with no luck.
Unfortunately none of these formats are supported by ImageMagicK
Thanks
Customize the code to fit into your requirements.
I've attached a sample image generated from a text file that contains "Generating preview images for TXT files" sentence by the following code:
<?php
Header ("Content-type: image/gif");
$txtfile = "test.txt";
$testarr = array();
if(!file_exists($txtfile)){
$string = "File not found.";
}
else{
$testarr = file($txtfile);
srand ((float) microtime() * 10000000);
$string = '-'.$testarr[array_rand($testarr)];
$string = substr($string,0,strlen($string)-2);
}
$font = 4;
$width = ImageFontWidth($font)* strlen($string);
$height = ImageFontHeight($font);
$im = ImageCreate($width,$height);
$x=imagesx($im)-$width ;
$y=imagesy($im)-$height;
$background_color = imagecolorallocate ($im, 242, 242, 242); // white colored background
$text_color = imagecolorallocate ($im, 0, 0,0); // black colored text
$trans_color = $background_color; // transparent
imagecolortransparent($im, $trans_color);
imagestring ($im, $font, $x, $y, $string, $text_color);
imagegif($im);
ImageDestroy($im);
?>
Links would be useful :
http://visionmasterdesigns.com/tutorial-convert-text-into-transparent-png-image-using-php/
http://www.phpro.org/examples/Text-to-Image-with-GD.html
I think you are going to need to do this on a machine with a GUI/Window environment by opening the files and then taking screenshots of them like http://litmus.com/ or https://browserlab.adobe.com/en-us/index.html
There is a program written do this for PDFs from HTML using WebKits rendering engine: http://code.google.com/p/wkhtmltopdf/
My suggestion is to convert all those documents to pdf and then print that pdf to image.
For Doc to PDF, you could try LiveDocx.

Categories