Page 562 - Beginning PHP 5.3
P. 562

Part III: Using PHP in Practice
                   First, you open the original image that you want to watermark:

                      < ?php
                    $myImage = imagecreatefromjpeg( “lucky.jpg” );

                   Then you open your copyright image. Because it ’ s a PNG file, use the  imagecreatefrompng()  function
                to open the file:

                    $myCopyright = imagecreatefrompng( “copyright.png” );

                   To position your copyright notice in the center of the image you have to know the dimensions of each of
                 your images. The function   imagesx()  returns the width of an image, and the function  imagesy()
                 returns an image ’ s height. Both functions take the image resource of the image that you would like to
                 get the width or height of. The script gets the width and height of both the original image and the
                 copyright notice:

                    $destWidth = imagesx( $myImage );
                    $destHeight = imagesy( $myImage );
                    $srcWidth = imagesx( $myCopyright );
                    $srcHeight = imagesy( $myCopyright );

                   You now need to work out the top - left corner position where the copyright notice needs to be placed.
                 To work out the  x  position of the corner, you subtract the width of the copyright notice from the width
                 of the image to be watermarked, and then divide the difference by two. To get the  y  position, you
                 perform the same calculation using the image heights:

                    $destX = ($destWidth - $srcWidth) / 2;

                    $destY = ($destHeight - $srcHeight) / 2;
                   Once you know where you need to put the copyright notice, you can go ahead and copy it into the
                 image to be watermarked. The function to do this is   imagecopy() :

                    imagecopy( $myImage, $myCopyright, $destX, $destY, 0, 0, $srcWidth,
                    $srcHeight );

                    imagecopy()  takes eight parameters, as follows:
                   ❑       The first parameter is the image into which the data is to be copied  —  the image that you want
                       to watermark
                   ❑       The second parameter is the image from where the data is being copied  —  the copyright image
                   ❑       The third and fourth parameters are the  x  and  y  coordinates of the position in the destination
                       image where the image data is to be copied. They mark the top - left corner of the block of data
                       that is being copied across
                   ❑       The next two parameters are  x  and  y  coordinates indicating the top - left corner of the block to
                       copy in the source image
                   ❑       The final two parameters indicate the width and height of the block to copy






              524





                                                                                                      9/21/09   2:48:43 PM
          c17.indd   524                                                                              9/21/09   2:48:43 PM
          c17.indd   524
   557   558   559   560   561   562   563   564   565   566   567