05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The series of requests sent by the web browser for this page looks something like this:<br />

GET /page.html HTTP/1.0<br />

GET /image1.jpg HTTP/1.0<br />

GET /image2.jpg HTTP/1.0<br />

The web server sends back a response to each of these requests. The Content-Type<br />

headers in these responses look like this:<br />

Content-Type: text/html<br />

Content-Type: image/jpeg<br />

Content-Type: image/jpeg<br />

To embed a <strong>PHP</strong>-generated image in an HTML page, pretend that the <strong>PHP</strong> script that<br />

generates the image is actually the image. Thus, if we have image1.php and image2.<br />

php scripts that create images, we can modify the previous HTML to look like this:<br />

<br />

<br />

Example Page<br />

<br />

<br />

This page contains two images.<br />

<br />

<br />

<br />

<br />

Instead of referring to real images on your web server, the img tags nowrefer to the<br />

<strong>PHP</strong> scripts that generate the images.<br />

Furthermore, you can pass variables to these scripts, so instead of having separate<br />

scripts to generate the two images, you could write your img tags like this:<br />

<br />

<br />

Then, inside image.php, you can access $_GET['num'] (or $num, ifregister_globals is<br />

on) to generate the appropriate image.<br />

The GD Extension<br />

Before you can start generating images with <strong>PHP</strong>, you need to check that you actually<br />

have image-generation capabilities in your <strong>PHP</strong> installation. In this chapter we’ll<br />

discuss using the GD extension, which allows <strong>PHP</strong> to use the open source GD graphics<br />

library available from http://www.boutell.com/gd/.<br />

Load the familiar phpinfo( ) page and look for a section entitled “GD”. You should<br />

see something similar to the following.<br />

gd<br />

GD Support enabled<br />

GD Version 2.0 or higher<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

The GD Extension | 215

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!