ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

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:
?>

       
                Image Converter
       

       
               

"POST" enctype="multipart/form-data">
               
                       
                               
                               
                       

                       


                               
                               
                       
               
       

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:

// No image?
if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
        die ('Invalid image uploaded.  Please go back and try again.');
}

if (empty($_POST['type'])) {
        die ('Invalid image type selected. Please go back and try again.');
}

$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:

// Load image
$image = open_image($imagepath);

if ($image == false) {
        die ('You uploaded an invalid image. Please go back and try again.');
}

// 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 ('You selected an invalid image type. Please go back and try again.');
}

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.

« Previous: Displaying and saving images



3 Responses to “Image manipulation with PHP & the GD library, Part 1”

  1. Max The IT pro Says:

    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.” :-)

  2. flashape Says:

    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

  3. Strandburglar Says:

    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.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Reading Images
  2. Opening any kind of image
  3. Displaying and saving images
  4. Image Converter & Conclusion
Bookmark Article
Download Article
PDF
Download this article as a PDF file

"text-align:right;">Image:"file" name="image">
Convert Into:
                                       
                               
"2">"submit" value="Convert Image" />