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().
Related
Good day. Why is that when I try to run my code on an external page, it works perfectly. But when I used wordpress to add it on my page, it gives me strange errors. Why is that and how do I fix that?
code:
<?php
// (A) OPEN IMAGE
$img = imagecreatefromjpeg('https://images.unsplash.com/photo-1550684376-efcbd6e3f031?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80');
// (B) WRITE TEXT
$white = imagecolorallocate($img, 255, 255, 255);
$txt = "sad";
$font = realpath('arial.ttf');
//(IMAGE, FONT SIZE, TILT ANGLE, X, Y, COLOR, FONT, TEXT)
imagettftext($img, 12, 0, 253, 234, $white, $font, $txt);
// (C) OUTPUT IMAGE
header('Content-Type: image/jpeg');
imagejpeg($img);
// OR SAVE TO A FILE
// THE LAST PARAMETER IS THE QUALITY FROM 0 to 100
imagejpeg($img, "test.jpg", 100);
?>
Here's the result I got when trying it on my wordpress page:
Image here
The issue is that you are running this in a shortcode and change the header of the output, that you already created, namely the page. It depends on what you want to do, if you want to display the image in the site:
ob_start();
imagejpeg( $img, NULL, 100 );
imagedestroy( $img );
$i = ob_get_clean();
echo "<img src='data:image/jpeg;base64," . base64_encode( $i )."'>";
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.
I have a captcha Code which works perfectly but I really dont know why it displays funny.
Can anyone help me figure out what might be the cause of this? Here is the code:
<?php
session_start();
$text = rand(10000,99999);
$_SESSION["vercode"] = $text;
$height = 25;
$width = 65;
$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;
imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, null, 80);
?>
This is what is displays:
kX’OýÓx£Ç> ñ=¨µÖ/b{ct÷¯½¬6É-ÀiH¡ä csdòyäæcÆ^"Ö|7¥è¦ss¤i™û%³‘¶>02q–Ú2q;A!p+Ÿ¢€:¿øX>'þÂþÈþÓÿDûöo™öx¾Ñö]Û¼?o›ågø7mÇËŒqF‡ñÄú•Ÿ¦j~M½·öWkx¤šÓÎ]²ù²™!Ü:ìeä“Ô“\¥QEQEÿÙ
You're missing the header. Add the following after session_start():
header('Content-Type: image/jpeg');
The output will be something like:
When you're creating an image like this, you need to tell the browser what type of content you're wanting to display. Hence the reason:
header("Content-type: image/jpeg");
is needed. Read the Wiki article on MIME if this confuses you. You'll also find that you'll need to specify the content type whenever you're generating other file types as well (for example: PDF files generated on the fly with PHP will require header('Content-type: application/pdf'); )
I have one image in php like this
Now I want to write Number on white textfield at a run time. And then I want to display it as a single image. How I can do this ?
I tried to search over and found different result but none of them worked for me.
I tried http://blog.doh.ms/2008/02/12/adding-text-to-images-in-real-time-with-php/
And this http://php.about.com/od/phpfunctions/g/imagestring_php.htm
It displays something like this that I didn't understand.
"‰PNG IHDR‚2ª‘ËPLTEÿA£vIDAT(‘c`Ø?óþaà1#a“æmcŠ06 ‹cˆ$ngï1ÀPc 6çï†Ä=gŽ¥å²1ó60$n¸
‘cØÀØp.R!‚¡Fhα sl€v5oøð¡Æ&ê •#ÓÀI›Å1U•/LIEND®B`‚"
I am not sure why this is happening . Am I missing something ?
UPDATE
I did this
php opened
$string = "BSThakrar";
$image = ImageCreateFromJPEG("images/temp.jpg");
$cor = imagecolorallocate($image, 0, 0, 0);
imagestring($image,5,126,22,urldecode($string),$cor);
header('Content-type: image/jpeg');
imagejpeg($image,NULL,100);
php closed.
and it give this output
<?php
$string = "BSThakrar";
$image = imageCreateFromJpeg("image.jpeg");
$cor = imagecolorallocate($image, 0, 0, 0);
imagestring($image,5,126,22,urldecode($string),$cor);
header('Content-type: image/jpeg');
imagejpeg($image,NULL,100);
?>
It has to be a jpg image instead of a png you where trying, also $string can be replaced by the input you want it to have.
if you want to use a png, change to this:
<?php
$string = "BSThakrar";
$image = imageCreateFromPng("image.png");
$cor = imagecolorallocate($image, 0, 0, 0);
imagestring($image,5,126,22,urldecode($string),$cor);
header('Content-type: image/png');
imagepng($image,NULL);
?>
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.