Interactive GraphicsOn the Server


This chapter is dedicated to creating graphics interactivelyon the server. Here, we're going to see how to use the GD graphics module with PHP to draw graphics on the server at run time and send them back to the browser. You can draw lines, rectangles, ellipses, and text, modify existing images, and more, sending the results back in a variety of formats: JPEG, GIF, PNG, and so forth. Think of what you can do with this technologyyou can draw real-time stock reports, add time and date to images, create weather maps, draw graphs or hotel occupancy charts, and any number of other things.

To make this happen, you have to enable support for the GD module in PHP. To enable that support, configure PHP using --with-gd[=DIR], where DIR is the GD base install directory. To use the bundled version of the GD library (recommended), use the configure option: --with-gd. That's all it takes. In Windows, you can find the needed DLL, php_gd2.dll, in the PHP ext directory. Copy it to the main PHP directory, uncomment the php_gd2.dll line in php.ini, and restart PHP:

 ;extension=php_fdf.dll extension=php_gd2.dll ;extension=php_gettext.dll         .         .         . 

Using GD, we'll be able to use functions such as imagecreate to create images and imagerectangle to draw in them, like this:

 $image_height = 100; $image_width = 300; $image = imagecreate($image_width, $image_height); $rect_color = imagecolorallocate($image, 128, 128, 128); imagerectangle($image, 0, 0, 50, 50, $rect_color); 

OK, we're set; let's start creating graphics! For reference, you can find the GD manual at http://us4.php.net/manual/en/ref.image.php.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net