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

Unit 11: Graphics



            $g = ($col & 0x0000FF00) >> 8;                                                        Notes
            $b = ($col & 0x000000FF);



            Direct manipulation of true colour values like this is rarely necessary. One application is to
            generate a colour-testing image that shows the pure shades of red, green, and blue. For example:

            $im = ImageCreateTrueColor(256,60);
            for($x=0; $x<256; $x++) {
                ImageLine($im, $x, 0, $x, 19, $x);

                ImageLine($im, 255-$x, 20, 255-$x, 39, $x<<8);
                ImageLine($im, $x, 40, $x, 59, $x<<16);
            }

            ImagePNG($im);
            Figure 11.14 shows the output of the colour-testing program.

                                      Figure 11.14: The Colour Test








            Obviously it will be much more colourful than what we can show you here in black and white,
            so try this example for yourself. In this particular example it is much easier to simply calculate
            the pixel colour than to call ImageColorResolveAlpha( ) for every colour.

            11.8.4 Text Representation of an Image
            An interesting use of the ImageColorAt( ) function is to loop through each pixel in an image
            and check the color, and then do something with that colour data. Example displays a # character
            in the appropriate colour for each pixel.

                          Converting an image to text.
            <html><body bgcolor=#000000><tt>

            <?php
             $im = imagecreatefromjpeg(‘php-el.jpg’);
             $dx = imagesx($im);
             $dy = imagesy($im);
             for($y = 0; $y < $dy; $y++) {

                 for($x=0; $x < $dx; $x++) {
                     $col = imagecolorat($im, $x, $y);
                     $rgb = imagecolorsforindex($im,$col);
                     printf(‘<font color=#%02x%02x%02x>#</font>’,

                             $rgb[‘red’],$rgb[‘green’],$rgb[‘blue’]);



                                             LOVELY PROFESSIONAL UNIVERSITY                                   277
   278   279   280   281   282   283   284   285   286   287   288