Images

To start the journey into the world of media, we will begin by drawing some simple shapes. For image manipulation purposes, PHP its own copy of the popular GD library. Some time ago, you used to have to get your own copy of GD, and just hope it was compatible with your PHP version - this is no longer the case. As PHP has its own bundled version of GD, it should always work fine with the version of PHP it was supplied with.

An important PHP function when working with images is header(). Header() outputs a HTTP header of your choice, and in this situation we will be sending the content-type header, which tells web browsers what kind of content they can expect through the connection. Popular content types include text/plain for plain text documents, text/html for most web pages, and image/*, where the * is PNG, JPEG, GIF, or other picture formats.

As header() sends HTTP headers, it must be used before you send any content through. This is a core HTTP rule - no headers can be sent after content. This is the same thing that stops you from using cookies after you have sent content. The header() function is covered in more detail in the Networks section, but for now we will just be working with this one aspect of it.

Creating a new image is done with the imagecreate() function, which has two parameters: the height and width of the image you wish to create. Imagecreate() will return false if it failed to create an image, which is usually the result of a lack of memory, otherwise it will return the image as a resource for you to use in other image functions.

Images are special in PHP in that they are one of the few resources that are not automatically cleaned up when your script ends, so imagecreate() is complemented by the imagedestroy() function, which takes an image resource as its only parameter, and frees up the memory assigned to the image. As image memory is not automatically cleaned up for you, you need to be especially careful to call image_destroy() on all your image resources once you are finished with them.

Author's Note: Warning! You must never forget to call imagedestroy() otherwise your web server will slowly chew up more and more system resources until your server all but locks up. Always keep your scripts in order - don't forget to clean up after yourself!

Once you have your image resource, it is yours to play with all you want. PHP provides a wide array of functions for you to use to manipulate the image, and, when you are done, you just choose your output format and the picture is finished.

To output the picture, you call one of several functions. If you want to convert it to PNG format, you call imagepng(). This function takes two parameters, which are the image resource to use, and a filename to save the picture as, which is optional. If you don't provide the second parameter, imagepng() sends the PNG-formatted picture straight to output, which is usually a visitor to your site.

To choose JPEG, you call the imagejpeg() function, which takes three parameters - the same two as imagepng() plus the quality you wish to use for the picture. The quality, a number between 0 (lowest quality, smallest file) and 100 (highest quality, largest file) is optional, as is the filename parameter. If you want to set the quality without specifying a filename, just provide an empty string ('') as the filename.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Creating new images >>

Previous chapter: SVG

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.