[ Team LiB ] Previous Section Next Section

Creating and Outputting Images

Before you can begin to work with an image, you must acquire an image resource. You can do this using the imagecreate() function. imagecreate() requires two arguments, one for the image's height and another for its width. It returns an image resource, which you will use with most of the functions we cover in this hour. You should be familiar with resources from your work with files and databases. The image resource returned by imagecreate() is a required argument for most of the functions in this book:

$image = imagecreate( 200, 200 );

Now that you have an image resource, you can allocate a color.


If you want to work with an existing image rather than create a new one, PHP provides a range of functions to open files of different types. You can open and work with a JPEG file, for example, by passing its path to imagecreatefromjpeg(). You can also open and work with a PNG file by passing a path to imagecreatefrompng(). You can then work with the image resource returned by these functions as we do in the examples in this hour. You will need to use gd_info() to check that GD is set up to work with the format you want to read.

You can find these and other variations on imagecreate() at the Image Functions section of the PHP manual, at http://www.php.net/gd.

    [ Team LiB ] Previous Section Next Section