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);
?>
Related
Image is not showing,not even the text.I'm not able to find the error.Is there any need of adding extra plugins or library files in it??
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('test.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'typesimp.TTF';
// Set Text to Be Printed On Image
$text = "Hello World!!";
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
Have you added the content type?
//Set the Content Type
header('Content-type: image/jpeg');
I want to create/generate an image with PHP, adding text into it, but without saving it into the FTP, I'd like to load it on the site by changing the content type to image/png, the same way I did it with ASP.NET:
Response.ContentType = "image/png";
rImage.Save(Response.OutputStream, ImageFormat.Png);
I found a function called file_put_contents, but I'm not so sure this is what I'm looking for. If you know how to do something like this, please tell me.
I'm trying this code, but it fails to load the image, and shows the browser's default error image.
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
Below example will regenerate an image with text over it:
<?php
header("Content-type: image/png"); // Set the header so that browser can output the image
$string = 'Print this text on Image'; // Text to be added on the image
$im = imagecreatefrompng("images/button1.png"); // Take base image
$orange = imagecolorallocate($im, 220, 210, 60); // Allocate a color for an image
imagestring($im, 3, 0, 0, $string, $orange); // Write the string at the top left
imagepng($im); // Create new image
imagedestroy($im); // destroy object
?>
Reference: http://php.net/manual/en/image.examples-png.php
The image get loaded to the browser fine but when I try writing some text onto it, the image breaks (like this: http://www.tradenepal.com.np/test.php ). When I comment out imagettftext(), the image does not load again. This happening on my localhost and I use WampServer Version 2.5. I have gone through so many comments on the inetrnet but I can't seem to know what the problem is. Any help would much be appreciated. Thank you. My code:
<?php
//Set content type
header('Content-type: image/jpeg');
// Create image from existing image
$jpgImage = imagecreatefromjpeg('file.jpg');
// Allocate color for text
$white = imagecolorallocate($jpgImage, 255, 255, 255);
// Set Path to Font File
$font = 'arialbd.ttf';
// Text to print to image
$text = 'Testing text output';
// Print Text On Image
imagettftext($jpgImage, 75, 0, 50, 400, $white, $font, $text);
// Send Image to Browser
imagejpeg($jpgImage);
// Clear Memory
imagedestroy($jpgImage);
?>
// Send Image to Browser
imagepng($jpg_image); <------ remove image type png
// Output the image
imagejpeg($jpg_image);
I've tested, it works.
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('file.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'arialbd.ttf';
// Set Text to Be Printed On Image
$text = "This is a sunset!";
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
why this works fine on wamp server:
<?php
//Let's generate a totally random string using md5
$md5_hash = md5(rand(0,999));
//We don't need a 32 character long string so we trim it down to 5
$security_code = substr($md5_hash, 15, 5);
//Set the session to store the security code
$_SESSION['security_code'] = $security_code;
//Set the image width and height
// $im = imagecreate(97, 18);
$im= #imagecreate(97, 18)
or die("Cannot Initialize new GD image stream");
//white background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 167, 167, 167);
//Add randomly generated string in white to the image
imagestring($im, 5, 27, 1, $security_code, $textcolor);
//Tell the browser what kind of file is come in
header("Content-Type: image/png");
//Output the newly created image in jpeg format
imagepng($im);
//Free up resources
imagedestroy($im);
?>
but this, doesn't work (just add html tags) ?
<html>
<body>
<?php
//Let's generate a totally random string using md5
$md5_hash = md5(rand(0,999));
//We don't need a 32 character long string so we trim it down to 5
$security_code = substr($md5_hash, 15, 5);
//Set the session to store the security code
$_SESSION['security_code'] = $security_code;
//Set the image width and height
// $im = imagecreate(97, 18);
$im= #imagecreate(97, 18)
or die("Cannot Initialize new GD image stream");
//white background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 167, 167, 167);
//Add randomly generated string in white to the image
imagestring($im, 5, 27, 1, $security_code, $textcolor);
//Tell the browser what kind of file is come in
header("Content-Type: image/png");
//Output the newly created image in jpeg format
imagepng($im);
//Free up resources
imagedestroy($im);
?>
</body>
</html>
Can you tell me why inside html tags php code doesn't work on WAMP Server, but only php code works fine?
okay, so im using this piece of code:
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('sunset.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'font.TTF';
// Set Text to Be Printed On Image
$text = "This is a sunset!";
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
and i have GD installed.
but the results show me this:
any idea why?