Image manipulation with PHP & the GD library, Part 1
(Page 2 out of 4)Opening any kind of image
Our universal function for opening any kind of image will have to be able to determine what kind of image we're dealing with and then use the correct function.
We could try to detect the extension of the file we're opening, and then use the correct function, like this:
$image = open_image('flower.jpg');
if ($image === false) {
die ('Unable to open image');
}
echo 'Opened image';
function open_image ($file) {
// Get extension
$extension = strrchr($file, '.');
$extension = strtolower($extension);
switch($extension) {
case '.jpg':
case '.jpeg':
$im = @imagecreatefromjpeg($file);
break;
case '.gif':
$im = @imagecreatefromgif($file);
break;
// ... etc
default:
$im = false;
break;
}
return $im;
}
?>
But this solution comes with another problem: what if the file has an incorrect extension? A JPG image could be called 'my_photo.gif' or a GIF image could be called 'flower.txt'. Using the extension is very unreliable, and isn't really foolproof. That's why we need a different option.
The best way is to employ a "hit or miss" solution. We simply use all the different imagecreatefrom functions on the file, and (eventually) we will get to the right function, and then be able to open the image. See the example below:
$image = open_image('flower.jpg');
if ($image === false) {
die ('Unable to open image');
}
echo 'Opened image';
function open_image ($file) {
# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }
# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }
# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }
# GD File:
$im = @imagecreatefromgd($file);
if ($im !== false) { return $im; }
# GD2 File:
$im = @imagecreatefromgd2($file);
if ($im !== false) { return $im; }
# WBMP:
$im = @imagecreatefromwbmp($file);
if ($im !== false) { return $im; }
# XBM:
$im = @imagecreatefromxbm($file);
if ($im !== false) { return $im; }
# XPM:
$im = @imagecreatefromxpm($file);
if ($im !== false) { return $im; }
# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($file));
if ($im !== false) { return $im; }
return false;
}
?>
As you can see, we simple go through all the imagecreatefrom functions until we hit the one that works, and this technique doesn't rely on the extension of the file.
Now that we're able to load any kind of image, let's see what we can do with it.
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.