Page 561 - Beginning PHP 5.3
P. 561

Chapter 17:  Generating Images with PHP


                            ❑   Annotating the image with some descriptive text or a caption
                            ❑   Copying a portion of another image into it to use as a watermark for copyright purposes

                          In the following sections you take a look at how to apply a watermark to an image, and also how to
                          create thumbnails of your images.




                           Applying a Watermark

                           If you are working on a Web site that displays original art or photographs, you may want to protect your
                         or your clients ’  intellectual property from being stolen. A common way of doing this is to apply a
                         watermark to the image to discourage other people from using it as their own. Here ’ s how to do it.
                           First, create a simple copyright image (such as Figure 17 - 17) in an image editor such as Photoshop. To do
                         this, add some black text to a white background and save the image as a PNG file with an eight - color
                         palette. (You see why this is done later in the section.)





                                           Figure 17-17

                           Copying the Watermark into the Image
                          Here ’ s the script to create the watermarked image. Save this file as  watermark.php . Figure 17 - 18 shows

                         an example result.
                               < ?php
                             $myImage = imagecreatefromjpeg( “lucky.jpg” );
                             $myCopyright = imagecreatefrompng( “copyright.png” );

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

                             $destX = ($destWidth - $srcWidth) / 2;
                             $destY = ($destHeight - $srcHeight) / 2;

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

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





                                                                                                         523





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