Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia Wish List | Report Bug | About Me ]

11.2     Images

This is NOT the latest copy of this book; click here for the latest version.

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.





<< 11.1.7 SVG   11.2.1 Creating new images: imagecreate(), imagedestroy(), imagecolorallocate() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is three plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow