Page 203 - Open Soource Technologies 304.indd
P. 203

Open Source Technologies



                   Notes         Introduction


                                 PHP makes it very easy to do many things needed on a website, among which is to create an
                                 image. The ability to generate an image in PHP can be useful if you want to do things like create
                                 CAPTCHA images, or even design a banner or logo on the fly the way some free blogging
                                 software do.
                                 By the end of this tutorial, you will be able to create or modify an existing image using PHP, set
                                 the colours of its background, the text and the lines you draw, write text to that image, draw
                                 a line and set its thickness. You will also be able to either send that image to a web browser or
                                 to save it as a file. Armed with this basic foundation, you will also be able to knowledgeably
                                 explore further and perform more complex operations on images using PHP if you wish.

                                 12.1 Image Creation Process


                                 12.1.1 Basic PHP Knowledge

                                 Assume that you already have some basic knowledge of how to write PHP programs. If not,
                                 you may wish to check out my basic PHP tutorial, How to Program in PHP.

                                 12.1.2 Your PHP Must Have Been Compiled with the GD Library

                                 For any of the functions listed here to be available, your PHP interpreter must have been
                                 compiled with the GD library. If you are using the PHP provided by a commercial web host,
                                 this is probably already provided as a standard feature.
                                 12.1.3 Free Type Must Be Compiled for True Type Font Support

                                 If you want True Type font support, your PHP must have FreeType support compiled into it
                                 as well.

                                 12.1.4 Creating an Image from Scratch Using PHP
                                 The easiest way to understand how to create an image is by looking at some sample code.


                                    <?php
                                         $my_img = imagecreate(200, 80);
                                         $background = imagecolorallocate($my_img, 0, 0, 255);

                                         $text_colour = imagecolorallocate($my_img, 255, 255, 0);

                                         $line_colour = imagecolorallocate($my_img, 128, 255, 0);
                                         imagestring($my_img, 4, 30, 25, “thesitewizard.com”,

                                         $text_colour);
                                         imagesetthickness ($my_img, 5);

                                         imageline($my_img, 30, 45, 165, 45, $line_colour);

                                         header(“Content-type: image/png”);
                                         imagepng($my_img);



        198                               LOVELY PROFESSIONAL UNIVERSITY
   198   199   200   201   202   203   204   205   206   207   208