Image manipulation with PHP & the GD library, Part 1
(Page 1 out of 4)Abstract
In this new article series you will learn about PHP's inbuilt image functions. In the first part you will see how to open images, display or save them, and how to write a simple image converter.
Introduction
Image manipulation can be a lot of fun, especially when it's all done for you by a script. PHP comes with several inbuilt image functions which can be used to do almost anything with images, and that's exactly what we'll be looking at.
In this new article series I will take you through most of the common image functions and tasks, and demonstrate everything with live examples. In the first part we will have a look at the basics, and learn how to open images and then display them or save them. We will also see how to write a simple image converter (e.g. JPEG to GIF).
Before we start, make sure that you have the latest version of the GD library installed, because the image functions require the GD library. If you're unsure whether you have installed the GD library or which version you're using, try the gd_info() function, which returns an associative array of information on the GD library you have installed. Refer to the manual for instructions on installing the GD library.
Let's get started!
Reading Images
The first step in using the image functions is creating a new image or opening an existing image. We'll have a look at creating new images in the next part, so let's start with opening an existing image.
This is really simple, and only requires a few lines of code:
$im = @imagecreatefromjpeg('flower.jpg');
if ($im === false) {
die ('Unable to open image');
}
// $im is now an 'Image Resource', which can be used
// with the other image functions
echo 'Opened image';
?>
As you can see in the above example, the imagecreatefromjpeg() function is used to open a JPEG image. If everything goes as planned, $im will now contain an image resource, which you can use with the other image functions. If something went wrong (e.g. the image is invalid or doesn't exist), false will be returned.
If your image is not a JPEG, but something else (e.g. a GIF) you will have to use another function, for example:
$im = @imagecreatefromgif('flower.gif');
if ($im === false) {
die ('Unable to open image');
}
// $im is now 'Image Resource', which can be used
// with the other image functions
echo 'Opened image';
?>
But there's one problem; what if we don't know what kind of image we're opening? In that case we need some sort of universal function that can open any kind of image. Unfortunately, such a function doesn't exist, but we can always write our own.
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.