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

Unit 12: Images



                   imagecolordeallocate($line_color);                                             Notes
                   imagecolordeallocate($text_color);

                   imagecolordeallocate($background);
                   imagedestroy($my_img);

              ?>
            The above code creates a 200x80 PNG image with a blue background and yellow text. It can
            be called from within your web page simply by referencing the php file. For example, if the
            PHP file that contains the above code is called myimage.php, then the HTML code to invoke
            it can simply be:

            <img src=”myimpage.php” alt=”Image created by a PHP script” width=”200” height=”80”>
            Explanation of the Code

               •  Creating the Image
                 The first thing the code does is to call the imagecreate() function with the dimensions of
                 the image, namely its width and height in that order. This function returns a resource
                 identifier for the image which we save in$my_img. The identifier is needed for all our
                 operations on the image.

                          If the function fails for some reason, it will return FALSE. If you want your
                          code to be robust, you should test for this.


               •  Using Colours in PHP
                 Before you can use any sort of colours in your image at all, you will need to allocate the
                 colour. Colours are represented by three digits, known as the RGB value. The first digit
                 denotes  the red  component,  the second  the green and  the third blue, hence RGB, for
                 Red-Green-Blue. These are the same colour values that you use for your web page as well
                 as numerous other computer applications.

                 Colours are allocated using the imagecolorallocate() function. This function will automatically
                 fill the background of the image with the colour the first time you call it, as well as return
                 an identifier for that particular colour. Subsequent calls to imagecolorallocate() will simply
                 create a colour identifier for your colour, without affecting your image background.

                 As you can see from the above code, my script allocates a blue identifier for the image,
                 and in so doing, causes imagecolorallocate() to set the background to blue automatically.
                 It also allocates a colour identifier for yellow and one for a shade of green. The latter two
                 identifiers will be used later to write text and draw a line.

                 imagecolorallocate() returns FALSE if the function fails for any reason.

               •  Writing Text to the Image
                 To write text to your image, you will need to use the imagestring() function. This function
                 uses a set of built-in fonts to do the writing. The fonts have various sizes, ranging from 1
                 to 5, where 1 is the smallest font size and 5 the largest. The size of the font is specified in
                 the second parameter to the function. The third and fourth parameters to imagestring()
                 specify the x,y coordinate for the top left hand corner of the text. In the case of the example
                 above, my text will begin 25 pixels from the top edge of the image, and 30 pixels from the
                 left.



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