Page 549 - Beginning PHP 5.3
P. 549

Chapter 17:  Generating Images with PHP


                               A true color image created with  imagecreatetruecolor()  can hold as many different colors as you
                             can possibly create  —  more than 16 million  —  so it doesn ’ t suffer from the palette limitation.
                           To work around this problem, you can use the   imagecolorresolve()  function, which always returns a
                         valid color identifier. The   imagecolorresolve()  function takes the same parameters as the
                            imagecolorallocate()  function, but unlike  imagecolorallocate()  —  which simply tries to allocate

                          the requested color to the image palette  —  the   imagecolorresolve()  function first looks to see if the
                          color that you are requesting already exists in the palette. If it does, the function simply returns the color
                          index for that color. If it doesn ’ t exist, the function tries to add the color that you requested to the palette.
                          If successful, it returns the color identifier within the palette. If it fails, it looks at all of the existing
                          colors in the palette, and returns the color identifier of the color in the palette that is the closest to
                          the color that you want.

                            You can create as many colors as you like for each image that you work with (up to the palette limitation
                          of 256 colors for palette - based images). The first color that you allocate to a palette - based image (one
                          created with the   imagecreate()  function) is used as the background color for that image. True - color
                         images created using the   imagecreatetruecolor()  function are created with a black background; it is
                          then up to you to color it as you need to.


                           Outputting Images
                           Once you have an image in memory, how do you output it? You simply call one of three functions:

                            ❑       imagejpeg()  outputs the image in JPEG format
                            ❑       imagegif()  outputs the image in GIF format
                            ❑       imagepng()  outputs the image in PNG format

                               PHP can output images in other formats too, but these are the three you ’ re most likely to use day - to - day.

                            Each function takes the image resource of the image to output, and an optional filename to save the
                          image file to. For example, here ’ s how to save an image as a JPEG file:

                             imagejpeg( $myImage, “myimage.jpeg” );

                           If you want to send the image straight to the browser instead, omit the filename argument, or set it to
                           null . You also need to send an appropriate HTTP header so that the browser knows how to handle the
                         image. For example, to display the image as a JPEG use:

                             header( “Content-type: image/jpeg” );
                             imagejpeg( $myImage );

                           To display it as a GIF use:

                             header( “Content-type: image/gif” );

                             imagegif( $myImage );
                           Finally, to display an image in PNG format use:
                             header( “Content-type: image/png” );


                             imagepng( $myImage );



                                                                                                         511

                                                                                                      9/21/09   2:48:37 PM
          c17.indd   511
          c17.indd   511                                                                              9/21/09   2:48:37 PM
   544   545   546   547   548   549   550   551   552   553   554