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 />

487<br />

Figure 22.1<br />

The script draws a blue background <strong>and</strong> then adds a line <strong>and</strong> a<br />

text label for the image.<br />

Now let’s walk through the steps of creating this image one by one.<br />

Creating a Canvas Image<br />

To begin building or changing an image in <strong>PHP</strong>, you need to create an image identifier.<br />

There are two basic ways to do this. One is to create a blank canvas, which you can do with<br />

a call to the imagecreatetruecolor() function, as done in this script with the following:<br />

$im = imagecreatetruecolor($width, $height);<br />

You need to pass two parameters to ImageCreateTrueColor().The first is the width of<br />

the new image, <strong>and</strong> the second is the height of the new image.The function will return<br />

an identifier for the new image.These identifiers work a lot like file h<strong>and</strong>les.<br />

An alternative way is to read in an existing image file that you can then filter, resize, or<br />

add to.You can do this with one of the functions imagecreatefrompng(), imagecreatefromjpeg(),<br />

or imagecreatefromgif(), depending on the file format you are reading in.<br />

Each of these functions takes the filename as a parameter, as in this example:<br />

$im = imagecreatefrompng(‘baseimage.png’);<br />

An example is shown later in this chapter using existing images to create buttons on<br />

the fly.<br />

Drawing or Printing Text on the Image<br />

Drawing or printing text on the image really involves two stages. First, you must select<br />

the colors in which you want to draw. As you probably already know, colors to be displayed<br />

on a computer monitor are made up of different amounts of red, green, <strong>and</strong> blue<br />

light. Image formats use a color palette that consists of a specified subset of all the possible<br />

combinations of the three colors.To use a color to draw in an image, you need to

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

Saved successfully!

Ooh no, something went wrong!