13.09.2016 Views

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

Create successful ePaper yourself

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

Creating Images<br />

489<br />

It takes as parameters the image identifier, the font, the x <strong>and</strong> y coordinates to start writing<br />

the text, the text to write, <strong>and</strong> the color.<br />

The font is a number between 1 <strong>and</strong> 5.These numbers represent a set of built-in<br />

fonts in latin2 encoding, with higher numbers corresponding to larger fonts. As an alternative<br />

to these fonts, you can use TrueType fonts or PostScript Type 1 fonts. Each of<br />

these font sets has a corresponding function set.We use the TrueType functions in the<br />

next example.<br />

A good reason for using one of the alternative font function sets is that the text written<br />

by imagestring() <strong>and</strong> associated functions, such as imagechar() (write a character<br />

to the image) is aliased.The TrueType <strong>and</strong> PostScript functions produce antialiased text.<br />

If you’re not sure what the difference is, look at Figure 22.2.Where curves or angled<br />

lines appear in the letters, the aliased text appears jagged.The curve or angle is achieved<br />

by using a “staircase” effect. In the antialiased image, when curves or angles appear in the<br />

text, pixels in colors between the background <strong>and</strong> the text color are used to smooth the<br />

text’s appearance.<br />

Figure 22.2 Normal text appears jagged, especially in a large font size.<br />

Antialiasing smoothes the curves <strong>and</strong> corners of the letters.<br />

Outputting the Final Graphic<br />

You can output an image either directly to the browser or to a file.<br />

In this example, you output the image to the browser.This is a two-stage process.<br />

First, you need to tell the web browser that you are outputting an image rather than text<br />

or HTML.You do this by using the Header() function to specify the MIME type of the<br />

image:<br />

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

Normally, when you retrieve a file in your browser, the MIME type is the first thing the<br />

web server sends. For an HTML or <strong>PHP</strong> page (post execution), the first thing sent is<br />

Content-type: text/html<br />

This tells the browser how to interpret the data that follows.<br />

In this case, you want to tell the browser that you are sending an image instead of the<br />

usual HTML output.You can do this by using the Header() function, which we have<br />

not yet discussed.

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

Saved successfully!

Ooh no, something went wrong!