Page 559 - Beginning PHP 5.3
P. 559

Chapter 17:  Generating Images with PHP


                           Opening an Existing Image
                           As you ’ ve already seen, to create a new image from scratch you use the  imagecreate()  or
                           imagecreatetruecolor()  function. To create a new image based on an existing image, you use the
                            imagecreatefrom...  series of functions. The most common of these are  imagecreatefromjpeg() ,
                            imagecreatefromgif() , and  imagecreatefrompng() . A number of other functions enable you to
                          create new images in memory from existing image formats, but they aren ’ t as widely used as these three.

                           The   imagecreatefromjpeg()  function works in the same way as the  imagecreate()  function, except
                          instead of passing it the width and height of the new image as parameters, you only pass the filename of
                          the existing image as a string. The function returns an image resource with which you can work.

                            For example, the line
                             $myImage = imagecreatefromjpeg( “lucky.jpg” );

                         opens the JPEG file called  lucky.jpg  that is in the same directory as the script, and stores its contents in
                         memory. The image resource identifier   $myImage  points to the image data in memory. You can test this
                          by outputting the image data to the browser.





                       Try It Out     Display a JPEG

                         In this example you read an existing JPEG file into memory, then send it to the browser for displaying.
                         Save the following script as show_jpeg.php in your document root folder. Make sure that the
                         filename that you pass to the imagecreatefromjpeg() function is a JPEG file that exists in the same
                         folder.
                             <?php
                             $myImage = imagecreatefromjpeg( “lucky.jpg” );
                             header( “Content-type: image/jpeg” );
                             imagejpeg( $myImage );
                             imagedestroy( $myImage );
                             ?>
                         Now open the script’s URL in your Web browser. Figure 17-16 shows a sample output — you’ll have a
                         different image, of course.

















                                                                                                         521





                                                                                                      9/21/09   2:48:42 PM
          c17.indd   521
          c17.indd   521                                                                              9/21/09   2:48:42 PM
   554   555   556   557   558   559   560   561   562   563   564