Image manipulation with PHP & the GD library, Part 1
(Page 4 out of 4)Converting Images
Now that we can load any kind of image, and display it, we are able to write a simple image converter.
Let's start off with a simple upload form:
if ($_POST) {
// handle the upload
// ...
die;
}
// Display the upload form:
?>
<html>
<head>
<title>Image Converter</title>
</head>
<body>
<form method="POST" enctype="multipart/form-data">
<table>
<tr>
<th style="text-align:right;">Image:</th>
<td><input type="file" name="image"></td>
</tr>
<tr>
<th>Convert Into: </th>
<td>
<select name="type">
<option value="jpg">JPEG</option>
<option value="gif">GIF</option>
<option value="png">PNG</option>
<option value="wbmp">WBMP</option>
</select>
</td>
<tr><td colspan="2"><input type="submit" value="Convert Image" /></td></tr>
</form>
</body>
</html>
Now that we've got a simple upload form, we'll have to first write some validation code (to make sure that a file has been uploaded), and then write the code for the image conversion. The following will do for the validation:
if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
die ('<strong>Invalid image uploaded. Please go back and try again.</strong>');
}
if (empty($_POST['type'])) {
die ('<strong>Invalid image type selected. Please go back and try again.</strong>');
}
$type = $_POST['type'];
$imagepath = $_FILES['image']['tmp_name'];
All we need to do still is open the uploaded image (with our open_image() function), and then use the right image* function to display the image. See the example below:
$image = open_image($imagepath);
if ($image == false) {
die ('<strong>You uploaded an invalid image. Please go back and try again.</strong>');
}
// Display image
switch($type) {
case 'jpg':
header ('Content-Type: image/jpeg');
imagejpeg($image);
break;
case 'gif':
header ('Content-Type: image/gif');
imagegif($image);
break;
case 'png':
header ('Content-Type: image/png');
imagepng($image);
break;
case 'wbmp':
header ('Content-Type: image/vnd.wap.wbmp');
imagewbmp($image);
break;
default:
die ('<strong>You selected an invalid image type. Please go back and try again.</strong>');
}
That's all there is to it, and our converter script can now be used to convert images from one format into another.
Click here to view the converter in action, and try it for yourself. The full source code can be downloaded by clicking here.
Conclusion
In this first part of an article series on image manipulation with PHP and the GD library I've taken you through all the basics necessary when manipulating images: loading, displaying and saving images.
In the next parts of this series we will take a look at resizing images, creating thumbnails on the fly, creating a CAPTCHA, creating images from scratch, manipulating images, and more, so stay tuned.
If you have any questions or comments, feel free to drop 'em in the comments below. Don't forget to join us at PHPit Forums to talk more PHP.
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.