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 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.

« Previous: Introduction & Reading Images
Next: 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