I have a php script that is supposed to create and display an image.
The image is displayed properly on the webpage when using this code:
header('Content-Type: image/gif');
readfile('picture.gif');
//rest of code creating the image
imagegif($im,'picture.gif');
But, the image is not displayed when using this code:
header('Content-Type: image/gif');
//rest of code creating the image
imagegif($im);
In the php documentation for imagegif, this code is given as an example:
<?php
// Create a new image instance
$im = imagecreatetruecolor(100, 100);
// Make the background white
imagefilledrectangle($im, 0, 0, 99, 99, 0xFFFFFF);
// Draw a text string on the image
imagestring($im, 3, 40, 20, 'GD Library', 0xFFBA00);
// Output the image to browser
header('Content-Type: image/gif');
imagegif($im);
imagedestroy($im);
?>
What am I doing wrong?
Related
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
At the moment I am generating a barcode using Shay Anderson's class (http://www.shayanderson.com/php/php-barcode-generator-class-code-39.htm) and I am able to successfully display the generated barcode in the browser as follows:
$bc = new PrintBarcode('DARP CODE');
$bc->drawVoucher();
$src = $bc->getVoucher();
// Output and free from memory
header('Content-Type: image/jpeg');
imagejpeg($src);
but now I am trying to modify my script to overlay the barcode on top of another image to create a voucher but I can't seem to get it to work, I just get the broken image icon in Chrome and the following warning in the console:
Resource interpreted as Document but transferred with MIME type image/jpeg
$bc = new PrintBarcode('DARP CODE');
$bc->drawVoucher();
$src = $bc->getVoucher();
header('Content-Type: image/jpeg');
// create actual voucher with barcode overlayed on voucher background
$bg = imagecreatefromjpeg('images/voucher.jpg');
imagecopymerge($bg, $src, 0, 0, 0, 0, imagesx($bg), imagesy($bg), 75);
imagejpeg($bg, null, 100);
imagedestroy($bg);
Error reporting is on and I'm getting no wanrings, notices or fatal errors. Any help appreciated.
The only thing I can think of is that from the docs of the barcode class, it generates the barcode as a gif so not sure if I am missing a few steps.
Turns out the problem was to do with the image I was using as the basis of the merge wasn't quite right so I re-converted it from a png to jpg properly (first time I downloaded the png I simply did a save as all files to jpeg) using photoshop and it was fine, to clarify, here's the code:
$bc = new PrintBarcode('DARP CODE');
$bc->drawVoucher();
$src = $bc->getVoucher();
header('Content-Type: image/jpeg');
$bg = imagecreatefromjpeg('images/voucher.jpg');
imagecopymerge($bg, $src, 40, 380, 0, 0, imagesx($bg), imagesy($bg), 100);
imagejpeg($bg, null, 100);
imagedestroy($src);
imagedestroy($bg);
Folks,I've some 288 points with their X,Y co-ordinates and a value assigned to them. I need to show this figuratively. I tried gd and imagettftext but simple code to draw a blank image isn't working even when I've installed and configured gd.
header('Content-Type: image/png');
$image = imagecreatetruecolor(400, 300);
// Allocate a color for the polygon
$col_poly = imagecolorallocate($image, 255, 255, 255);
// Draw the polygon
imagepolygon($image, array(
0, 0,
100, 200,
300, 200
),
3,
$col_poly);
// Output the picture to the browser
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
The output in the browser is
Try to set encoding of php file with your code to UTF-8 without BOM (e.g. in Notepad++).
Your code works right and image shows. Most probably your GD library is not working as expected. Try this
$image = imagecreatetruecolor(400, 300) or die('Cannot Initialize new GD image stream');
$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
I have already draw a black image i want draw a file suppose 3.png on it ..
How to do that ?
You have to load an image you want to draw and then use imagecopy() to draw it:
// the part you already have; creates 538x616 px black image
$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
// load image from file and draw it onto black image;
// for loading PNG, use imagecreatefrompng()
$overlayImage = imagecreatefromjpeg('macro_photo_1.jpg');
imagecopy($image, $overlayImage, 10, 10, 0, 0, imagesx($overlayImage), imagesy($overlayImage));
// send image to the browser
header("Content-Type: image/png");
imagepng($image);
exit;
I would also advise to go through the list of GD and Image functions to see what (and how) can be done with images in PHP.
imagepng($image);
And if you haven't already you need:
header('Content-Type: image/png');
Before you call imagepng to let the browser know it's looking at an image not an HTML page.
Saroj, http://www.php.net/manual/en/function.imagecreate.php and from same page, here is the example snippet
<?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);
?>
HTH!
I am using PHP imagejpeg to create a simple image like this..
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($im);
// Free up memory
imagedestroy($im);
This is working, but i would like it to automatically trigger a download inside the browser instead of display the image.
How can I do this, do I need to set a header?
All you have to do is set an extra header:
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename=file-name.jpg'); // This will tell the browser to download it
// Output the image
imagejpeg($im);
// Free up memory
imagedestroy($im);
Here is the documentation
I believe you need at least 3 headers:
header("Content-Type: image/jpeg"); // Content type
header("Content-Disposition: attachment; filename=file-name.jpg"); // Content deposition
header("Content-Length: $fileSize"); // YOUR FILE SIZE
otherwise, the download will not work in firefox.