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 3 out of 4)

Displaying and saving images

After you've opened an image, and done some manipulation, you'll probably want to display in the browser. To do this, there are several functions you can use, such as imagegif() and imagejpeg().

The example below demonstrates this:

// Load open
$image = open_image('flower.jpg');
if ($image === false) { die ('Unable to open image'); }

// ... Do some manipulation here ...

// Display image
imagejpeg($image);

function open_image ($file) {
        // ... edited for brevity
}

?>

(View Live Demo)

If you try to run the above code (or view the live demo) you'll be in for a surprise: it doesn't display the image, but instead it returns all kinds of garbage filled with weird signs.

That's because we haven't sent the correct Content-Type header to the browser. The standard content-type is 'text/html', so that's what the browser expects when you don't send your own custom header.

Obviously that's not what we want, so we'll have to send the correct Content-Type header, like this:

// Load open
$image = open_image('flower.jpg');
if ($image === false) { die ('Unable to open image'); }

// ... Do some manipulation here ...

// Send header
header ('Content-Type: image/jpeg');

// Display image
imagejpeg($image);

function open_image ($file) {
        // ... edited for brevity
}

?>

(View Live Demo)

If you now run the above example, you will see the JPEG image like any normal image.

It's also possible to directly save the image to the hard disk instead of displaying it in your browser. The imagejpeg() function (or any other image* function) takes a second filename argument to specify where the image should be saved, for example:

// Load open
$image = open_image('flower.jpg');
if ($image === false) { die ('Unable to open image'); }

// ... Do some manipulation here ...

// Display image
imagejpeg($image, 'my_image.jpg');

function open_image ($file) {
        // ... edited for brevity
}

?>

A new JPEG image will be now created in the current working directory, called 'my_image.jpg'. Since we're not displaying the image, we don't need the Content-Type header anymore.

Let's have a look at a practical use of the GD library: converting images.

« Previous: Opening any kind of image
Next: Image Converter & Conclusion »



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