Section 16.1. Getting Started


16.1. Getting Started

An important PHP function when working with images is header( ). This outputs a HTTP header of your choice; 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 MIME types for other picture formats.

As header( ) sends HTTP headers, it must be used before you send any content through. This is a core HTTP ruleno 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 Chapter 20, but for now, we will just work 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. This 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. To free up this image's memory, pass that resource into imagedestroy( ) as its only parameter.

Once you have your image resource, it is yours to play with all you want. PHP provides a selection of functions for you to use to manipulate the image. 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 (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 parametersthe 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.

The most basic image script looks like this:

     $image = imagecreate(400,300);     // do stuff to the image     imagejpeg($image, '', 75);     imagedestroy($image); 

Save that as picture1.php. As most of your pictures will probably be referenced from a web page, we will also make a companion web page. Save this as phppicture.html:

     <html>     <title>PHP Art</title>     <body>     PHP woz 'ere:     <img src="/books/1/302/1/html/2/picture1.php" />     </body>     </html> 

Open up your web browser and load in phppicture.htmlyou should see a large black box for the image, as shown in Figure 16-1.

Be sure not to have anything outside the PHP code block, not even an empty line or a space. Everything outside the PHP block is sent to the browser as part of the picture, and even having a single space character at the end of the file will cause problems.


The next step is to add a little color in place of the "do stuff to the image" comment, so we need imagecolorallocate( ) (note that you must use U.S. spellings for these function names). This new function takes four parameters: the image resource you are choosing a color for, then three integers between 0 and 255one each for the red value, then green value, and the blue value of the color. You can also specify these colors in hexadecimal format (e.g., 0xff) rather than decimal.

Figure 16-1. Our first picture using PHP is a big square colored entirely blacknot exactly a stunner, but a good start


The first color you allocate is automatically used as the background color for your image, so this next piece of code is a minor modification of the last script to include color information:

     $image = imagecreate(400,300);     $gold = imagecolorallocate($image, 255, 240, 00);     imagepng($image);     imagedestroy($image); 

Save that over picture1.php, and refresh phppicture.htmlyou should see the black square replaced by a yellow square.

Don't worry about deallocating colors, as they are just numbers and not resources, meaning they don't use up any special memory. If you really want to deallocate a color (perhaps if you're working with a paletted image), use the imagecolordeallocate( ) function.




PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net