PHP Graphics Overlaying Watermarks - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Graphics Overlaying Watermarks - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, June 23, 2019

PHP Graphics Overlaying Watermarks

PHP Graphics



Overlaying Watermarks

Problem

You want to overlay a watermark stamp on top of images.

Solution

If your watermark stamp has a transparent background, use ImageCopy() to use alpha channels:

       $image = ImageCreateFromPNG('/path/to/image.png');
       $stamp = ImageCreateFromPNG('/path/to/stamp.png');

       $margin = ['right' => 10, 'bottom' => 10]; // offset from the edge

       ImageCopy($image, $stamp,
              imagesx($image) - imagesx($stamp) - $margin['right'],

              imagesy($image) - imagesy($stamp) - $margin['bottom'],
              0, 0, imagesx($stamp), imagesy($stamp));

Otherwise, use ImageCopyMerge() with an opacity;

       $image = ImageCreateFromPNG('/path/to/image.png');
       $stamp = ImageCreateFromPNG('/path/to/stamp.png');

       $margin = ['right' => 10, 'bottom' => 10]; // offset from the edge
       $opacity = 50; // between 0 and 100%

       ImageCopyMerge($image, $stamp,
              imagesx($image) - imagesx($stamp) - $margin['right'],
              imagesy($image) - imagesy($stamp) - $margin['bottom'],
              0, 0, imagesx($stamp), imagesy($stamp),
              $opacity);

Discussion

When you overlay a watermark stamp on top of an image, you want the stamp to be clearly visible, but you would also like to allow the original image to show through. Also, to improve attractiveness, it’s nice to offset the watermark from the edge. In this example, there’s a 10-pixel border.

The ImageCopy() and ImageCopyMerge() functions let you take one image and place it on top of another. In this case, you copy the stamp on top of the base image.

The first two arguments are destination (your image) and the source (your stamp). Next are the x and y coordinates for where you’d like the stamp to be. In this case, you want to put the entire stamp in the lower-right corner, so the x coordinate is the width of the destination image minus the width of the stamp and the right offset; likewise for the y coordinate, but using heights instead of widths. To avoid hardcoding the image sizes into the script, use the ImageSX() and ImageSY() functions, because they dynamically compute those sizes.

When you know where the stamp is to be placed, you pass four coordinates for the size of the stamp. Here you pass 0, 0, ImageSX($stamp), and ImageSY($stamp) to copy the entire stamp. However, you could choose to copy just a portion of the image.

Finally, for ImageCopyMerge(), there’s an argument for the opacity. This sets the translucence of the copied image. Values range from 0 to 100. The smaller the number, the lighter the stamp’s appearance.

Putting this together in action generates Figure:

       $image = ImageCreateFromJPEG(__DIR__ . '/iguana.jpg');

       // Stamp
       $w = 400; $h = 75;
       $stamp = ImageCreateTrueColor($w, $h);
       ImageFilledRectangle($stamp, 0, 0, $w-1, $h-1, 0xFFFFFF);

       // Attribution text
       $color = 0x000000; // black
       ImageString($stamp, 4, 10, 10,
              'Galapagos Land Iguana by Nicolas de Camaret', $color);
       ImageString($stamp, 4, 10, 28,
              'http://flic.kr/ndecam/6215259398', $color);
       ImageString($stamp, 2, 10, 46,
              'Licence at http://creativecommons.org/licenses/by/2.0.', $color);

       // Add watermark
       $margin = ['right' => 10, 'bottom' => 10]; // offset from the edge
       $opacity = 50; // between 0 and 100%
       ImageCopyMerge($image, $stamp,
              imagesx($image) - imagesx($stamp) - $margin['right'],
              imagesy($image) - imagesy($stamp) - $margin['bottom'],
              0, 0, imagesx($stamp), imagesy($stamp),
              $opacity);

       // Send
       header('Content-type: image/png');
       ImagePNG($image);
       ImageDestroy($image);
       ImageDestroy($stamp);

Figure Iguana with a watermark

No comments:

Post a Comment

Post Top Ad