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

Unit 11: Graphics



            PHP also supports GD 2.x, which, as of this writing, is still in beta. Despite its beta status, the   Notes
            new version is relatively stable and has many new features. In particular, Version 2.x allows true-
            colour images, which lets GD read in PNGs and JPEGs with almost no loss in quality. Also, GD
            2.x supports PNG alpha channels, which allow you to specify a transparency level for each pixel.
                          Both versions of GD are available for download from the official GD site at
                          http://www.boutell.com/gd/. The GD section of the online PHP Manual
                          athttp://www.php.net/image also lists the location of the additional libraries
                          necessary to provide support for JPEGs and Type 1 fonts.
            There are two easy ways to see which version, if any, of GD is installed on your server and
            how it is configured. One way is to call phpinfo( ) . You should see with-gd at the top under
            “Configure Command”; further down the page there is also a section titled “gd” that has more
            information about which version of GD is installed and what features are enabled. The other
            option is to check the return value of function_exists(‘imagecreate’). If it returns true, GD
            is installed. The imagetypes( ) function returns a bit field indicating which graphics formats
            are available. The basic image generation process has three steps: creating the image, adding
            graphics and text to the canvas, and displaying or saving the image.




            $image = ImageCreate(200, 50);
            $background_color = ImageColorAllocate($image, 255, 255, 255); // white
            $gray = ImageColorAllocate($image, 204, 204, 204); // gray
            ImageFilledRectangle($image, 50, 10, 150, 40, $gray);
            header(‘Content-type: image/png’);
            ImagePNG($image);
            The output of this  code,  which prints a gray rectangle on a white background, is shown  in
            Figure 11.1.
                           Figure 11.1: A Gray Rectangle on a White Background














            To begin, you create an image canvas. The ImageCreate( ) function does not return an actual
            image. Instead, it provides you with a handle to an image; it is not an actual graphic until you
            specifically tell PHP to write the image out. Using ImageCreate( ), you can juggle multiple
            images at the same time.
            The parameters passed to ImageCreate( ) are the width and height of the graphic in pixels. In
            this case, it is 200 pixels across and 50 pixels high. Instead of creating a new image, you can also
            edit existing images. To open a graphic, call ImageCreateFromPNG( ) or a similarly named
            function to open a different file format. The filename is the only argument, and files can live
            locally or on remote servers:
            // open a PNG from the local machine $graph = ImageCreateFromPNG(‘/path/to/graph.png’);
            // open a JPEG from a remote server $icon = ImageCreateFromJPEG(‘http://www.example.
            com/images/icon.jpeg’);


                                             LOVELY PROFESSIONAL UNIVERSITY                                   259
   260   261   262   263   264   265   266   267   268   269   270