Write text on image as display as image - php

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);
?>

Related

imagejpeg not working when used in Wordpress

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 )."'>";

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().

captcha displaying in a funny way

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'); )

'imagecreatefrompng' is outputting a crashed image

I’m having problems with gd library in PHP when I try to do a imagecratefrompng. I’m running a script where the user input a text and it is added to a pre-created image. Problem is, when I output the image the image show as broken.
Can anyone help pointing if something is wrong with my script/image?
The image is a PNG, 600x956, 220kb file size.
GD Library is enabled. PNG, JPEG, GIF support are enabled.
Here is the code.
// Text inputed by user
$text = $_POST['text'];
// Postion of text inputed by the user
$text_x = 50;
$text_y = 817;
// Color of the text
$text_color = imagecolorallocate($img, 0, 0, 0);
// Name of the file (That is in the same directory of the PHP file)
$nomeDaImagem = "Example";
$img = imagecreatefrompng($nomeDaImagem);
//Text is retrieved by Post method
imagestring($img, 3, $text_x, $text_y, $text, $text_color);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
There are a number of issues with your script:
You attempt to allocate the colour to the image, before you have actually created the image.
Your string to be written is in the variable $nome, but you are printing $text.
You don't check if $_POST['text'] exists, which may result in a Notice-level error.
You don't check if the file exists, which may result in a Warning-level error.
Here's an example of your code, fixed:
// Text inputed by user
$nome = isset($_POST['text']) ? $_POST['text'] : "<Nothing to write>";
// Postion of text inputed by the user
$text_x = 50;
$text_y = 817;
// Name of the file (That is in the same directory of the PHP file)
$nomeDaImagem = "Example";
$img = file_exists($nomeDaImagem)
? imagecreatefrompng($nomeDaImagem)
: imagecreate(imagefontwidth(3)*strlen($nome)+$text_x,imagefontheight(3)+$text_y);
// Color of the text
$text_color = imagecolorallocate($img, 0, 0, 0);
//Text is retrieved by Post method
imagestring($img, 3, $text_x, $text_y, $nome, $text_color);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
Read more:--
http://php.net/manual/en/function.imagecreatefrompng.php
http://www.php.net/manual/en/function.imagecreatefromstring.php
or try this
<?php
function LoadPNG($imgname)
{
/* Attempt to open */
$im = #imagecreatefrompng($imgname);
/* See if it failed */
if(!$im)
{
/* Create a blank image */
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/* Output an error message */
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
return $im;
}
header('Content-Type: image/png');
$img = LoadPNG('bogus.image');
imagepng($img);
imagedestroy($img);
?>

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