Image manipulation with PHP & the GD library, Part 1
(Page 3 out of 4)Displaying and saving images
After you've opened an image, and done some manipulation, you'll probably want to display in the browser. To do this, there are several functions you can use, such as imagegif() and imagejpeg().
The example below demonstrates this:
// Load open
$image = open_image('flower.jpg');
if ($image === false) { die ('Unable to open image'); }
// ... Do some manipulation here ...
// Display image
imagejpeg($image);
function open_image ($file) {
// ... edited for brevity
}
?>
If you try to run the above code (or view the live demo) you'll be in for a surprise: it doesn't display the image, but instead it returns all kinds of garbage filled with weird signs.
That's because we haven't sent the correct Content-Type header to the browser. The standard content-type is 'text/html', so that's what the browser expects when you don't send your own custom header.
Obviously that's not what we want, so we'll have to send the correct Content-Type header, like this:
// Load open
$image = open_image('flower.jpg');
if ($image === false) { die ('Unable to open image'); }
// ... Do some manipulation here ...
// Send header
header ('Content-Type: image/jpeg');
// Display image
imagejpeg($image);
function open_image ($file) {
// ... edited for brevity
}
?>
If you now run the above example, you will see the JPEG image like any normal image.
It's also possible to directly save the image to the hard disk instead of displaying it in your browser. The imagejpeg() function (or any other image* function) takes a second filename argument to specify where the image should be saved, for example:
// Load open
$image = open_image('flower.jpg');
if ($image === false) { die ('Unable to open image'); }
// ... Do some manipulation here ...
// Display image
imagejpeg($image, 'my_image.jpg');
function open_image ($file) {
// ... edited for brevity
}
?>
A new JPEG image will be now created in the current working directory, called 'my_image.jpg'. Since we're not displaying the image, we don't need the Content-Type header anymore.
Let's have a look at a practical use of the GD library: converting images.
August 18th, 2006 at 9:31 pm
Dude, this is a great article as I soon plan to get cracking on PHP for some upcoming web projects. Thank you for taking the time to write this. It was “delicious.” :-)
August 19th, 2006 at 2:43 am
there is a better solution to your ‘hit-or-miss’ method when you talk about a file having the wrong extension. the php method getImageSize() returns array of information about an image, which is [width, height, imageType], where image type would be ‘JPG’, ‘GIF’, ‘PNG’, etc.
http://us3.php.net/getimagesize
August 22nd, 2006 at 3:31 am
If the image files you are trying to open had been uploaded by a user, the file type would be in $_FILES[’imagefile’][’type’] where imagefile is the name of the form element which uploaded the image. This way you could use a function more like the one you showed which checked the extension. For example
switch($_FILES[’imagefile’][’type’]){
case ‘image/jpeg’:
$im = @imagecreatefromjpeg($file);
break;
case ‘image/gif’:
//etc. etc.