13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

490 Chapter 22 Generating Images<br />

This function sends raw HTTP header strings. Another typical application of this<br />

function is to do HTTP redirects.They tell the browser to load a different page instead<br />

of the one requested.They are typically used when a page has been moved. For example,<br />

Header (‘Location: http://www.domain.com/new_home_page.html ‘);<br />

An important point to note when using the Header() function is that it cannot be executed<br />

if content has already been sent for the page. <strong>PHP</strong> will send an HTTP header<br />

automatically for you as soon as you output anything to the browser. Hence, if you have<br />

any echo statements, or even any whitespace before your opening <strong>PHP</strong> tag, the headers<br />

will be sent, <strong>and</strong> you will get a warning message from <strong>PHP</strong> when you try to call<br />

Header(). However, you can send multiple HTTP headers with multiple calls to the<br />

Header() function in the same script, although they must all appear before any output is<br />

sent to the browser.<br />

After you have sent the header data, you output the image data with a call to<br />

imagepng ($im);<br />

This call sends the output to the browser in PNG format. If you wanted it sent in a different<br />

format, you could call imagejpeg()—if JPEG support is enabled.You would also<br />

need to send the corresponding header first, as shown here:<br />

Header (‘Content-type: image/jpeg’);<br />

The second option you can use, as an alternative to all the previous ones, is to write the<br />

image to a file instead of to the browser.You can do this by adding the optional second<br />

parameter to imagepng() (or a similar function for the other supported formats):<br />

imagepng($im, $filename);<br />

Remember that all the usual rules about writing to a file from <strong>PHP</strong> apply (for example,<br />

having permissions set up correctly).<br />

Cleaning Up<br />

When you’re done with an image, you should return the resources you have been<br />

using to the server by destroying the image identifier.You can do this with a call to<br />

imagedestroy():<br />

imagedestroy($im);<br />

Using Automatically Generated Images in Other<br />

Pages<br />

Because a header can be sent only once, <strong>and</strong> this is the only way to tell the browser that<br />

you are sending image data, it is slightly tricky to embed any images you create on the<br />

fly in a regular page.Three ways you can do it are as follows:<br />

n<br />

You can have an entire page consist of the image output, as we did in the previous<br />

example.

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

Saved successfully!

Ooh no, something went wrong!