Watermark an image with PHP using imagecopy()

June 07, 2014 | Last tested: May 2015 | 1246 views | Comments

Watermark an image by putting a PNG image on top of a JPG image using the function imagecopy() in PHP.

For example.

1. Watermark a JPG image with a PNG image..

Resource images (used with permission):

  1. The waterwark PNG image, created with Photoshop. The text opacity is 100%. testLogo.png
  2. JPG image to be watermarked. create-a-dark-forest-scene-photo-manipulation.jpg Source link.

 

//set the source image (foreground) - the watermark image
$sourceImage = 'testLogo.png';

//set the destination image (background)
$destImage = 'create-a-dark-forest-scene-photo-manipulation.jpg';

//get the size of the source image, needed for imagecopy()
list($srcWidth, $srcHeight) = getimagesize($sourceImage);

//create a new image from the source image
$src = imagecreatefrompng($sourceImage);

//create a new image from the destination image
$dest = imagecreatefromjpeg($destImage);

//set the x and y positions of the source image on top of the destination image
$src_xPosition = 10; //10 pixels from the left
$src_yPosition = 10; //10 pixels from the top

//set the x and y positions of the source image to be copied to the destination image
$src_cropXposition = 0; //do not crop at the side
$src_cropYposition = 0; //do not crop on the top
						
//merge the source and destination images
imagecopy($dest,$src,$src_xPosition,$src_yPosition,$src_cropXposition,$src_cropYposition,$srcWidth,$srcHeight);

//output the merged images to a file
/*
 * '100' is an optional parameter,
 * it represents the quality of the image to be created,
 * if not set, the default is about '75'
 */
imagejpeg($dest,'watermark-an-image-with-php-using-imagecopy-01.jpg',100);

//destroy the source image
imagedestroy($src);

//destroy the destination image
imagedestroy($dest);

Result.

Please mouse over here to view the image.

 

2. Use a PNG image with an opacity of 50% as a watermark for a JPG image..

The opacity of the same PNG image used on the example above was manually reduced to 50%. This can be done with Photoshop.

 

This PHP script is almost the same as the example above, the only difference is the PNG image used, and the new image created.

  1. PNG file used - testLogo_50-opacity.png
  2. New filename - watermark-an-image-with-php-using-imagecopy-02.jpg

 

//set the source image (foreground) - the watermark image
$sourceImage = 'testLogo_50-opacity.png';

//set the destination image (background)
$destImage = 'create-a-dark-forest-scene-photo-manipulation.jpg';

//get the size of the source image, needed for imagecopy()
list($srcWidth, $srcHeight) = getimagesize($sourceImage);

//create a new image from the source image
$src = imagecreatefrompng($sourceImage);

//create a new image from the destination image
$dest = imagecreatefromjpeg($destImage);

//set the x and y positions of the source image on top of the destination image
$src_xPosition = 10; //10 pixels from the left
$src_yPosition = 10; //10 pixels from the top

//set the x and y positions of the source image to be copied to the destination image
$src_cropXposition = 0; //do not crop at the side
$src_cropYposition = 0; //do not crop on the top
						
//merge the source and destination images
imagecopy($dest,$src,$src_xPosition,$src_yPosition,$src_cropXposition,$src_cropYposition,$srcWidth,$srcHeight);

//output the merged images to a file
/*
 * '100' is an optional parameter,
 * it represents the quality of the image to be created,
 * if not set, the default is about '75'
 */
imagejpeg($dest,'watermark-an-image-with-php-using-imagecopy-02.jpg',100);

//destroy the source image
imagedestroy($src);

//destroy the destination image
imagedestroy($dest);

Result.

Please mouse over here to view the image.