How to add watermark to images using php is a short tutorial to show you how to achieve what you see in the below image.

See how this was achieved:
How to add watermark to images using php
- Creates the watermark image using this function from gd library
$watermark = imagecreatefrompng('image_to_be_used_as_watermark.image_extension');
- GET image_to_be_used_as_watermark width
$watermarkX = imagesx($watermark);
- GET image_to_be_used_as_watermark.png height
$watermarkY = imagesy($watermark);
- Get image extension.
$destpath = 'Path_to_image/';
$filename = 'image_to_put_watermark_on.image_extension';
$ext = getExtension($destpath . $filename);
- Creates the new image using the following function from gd library
if (!strcmp("jpg", $ext) || !strcmp("jpeg", $ext)) {
$img = imagecreatefromjpeg($destpath . $filename);
}
if (!strcmp("png", $ext)) {
$img = imagecreatefrompng($destpath . $filename);
}
- Gets the dimensions of the image
$old_x = imageSX($img);
$old_y = imageSY($img);
- Create a new image($destimg) with the original dimmensions
$dst_img = ImageCreateTrueColor($old_x, $old_y);
- SET margin for watermark to position it on the destination image the way you like it – i suggest you play around with these figures to get a suitable position for your watermark.
$margin_right = 50;
$margin_bottom = 100;
- Copy image onto destination image created
imagecopy($dst_img, $img, 0, 0, 0, 0, $old_x, $old_y);
- Copy watermark image onto destination image created
imagecopy($dst_img, $watermark, imageSX($dst_img) - $old_x + 70, imageSY($dst_img) - $old_y / 2, 0, 0, $watermarkX, $watermarkY);
- output the created image to Uploads folder with same filename.
if (!strcmp("png", $ext)) {
imagepng($dst_img, 'Uploads/' . $filename);
} else {
imagejpeg($dst_img, 'Uploads/' . $filename);
}
- Destroys watermared and destination images.
imagedestroy($dst_img); //destroy destination image
imagedestroy($img); //destroy destination image
imagedestroy($watermark); //destroy second source image
Putting all together…
// This function reads the extension of the file.
function getExtension($str) {
$i = strrpos($str, ".");
if (!$i) {
return "";
}
$l = strlen($str) - $i;
$ext = substr($str, $i + 1, $l);
return $ext;
}
//Creates the watermark image using this function from gd library
$watermark = imagecreatefrompng('image_to_be_used_as_watermark.image_extension');
//GET image_to_be_used_as_watermark width
$watermarkX = imagesx($watermark);
//GET image_to_be_used_as_watermark.png height
$watermarkY = imagesy($watermark);
//Get image extension.
$destpath = 'Path_to_image/';
$filename = 'image_to_put_watermark_on.image_extension';
$ext = getExtension($destpath . $filename);
//Creates the new image using the following function from gd library
if (!strcmp("jpg", $ext) || !strcmp("jpeg", $ext)) {
$img = imagecreatefromjpeg($destpath . $filename);
}
if (!strcmp("png", $ext)) {
$img = imagecreatefrompng($destpath . $filename);
}
//Gets the dimensions of the image
$old_x = imageSX($img);
$old_y = imageSY($img);
//Create a new image($destimg) with the original dimmensions
imagecopy($dst_img, $img, 0, 0, 0, 0, $old_x, $old_y);
//Copy watermark image onto destination image created
imagecopy($dst_img, $watermark, imageSX($dst_img) - $old_x + 70, imageSY($dst_img) - $old_y / 2, 0, 0, $watermarkX, $watermarkY);
//output the created image to Uploads folder with same filename.
if (!strcmp("png", $ext)) {
imagepng($dst_img, 'Uploads/' . $filename);
} else {
imagejpeg($dst_img, 'Uploads/' . $filename);
}
//Destroys watermared and destination images.
imagedestroy($dst_img); //destroy destination image
imagedestroy($img); //destroy destination image
imagedestroy($watermark); //destroy second source image