Load the base image and watermark separately, then copy the watermark at a calculated position. PNG watermarks are useful when transparency is required.
<?php
$base = imagecreatefromjpeg('photo.jpg');
$watermark = imagecreatefrompng('watermark.png');
$x = imagesx($base) - imagesx($watermark) - 20;
$y = imagesy($base) - imagesy($watermark) - 20;
imagealphablending($base, true);
imagecopy($base, $watermark, $x, $y, 0, 0, imagesx($watermark), imagesy($watermark));
imagejpeg($base, 'watermarked.jpg', 90);
imagedestroy($watermark);
imagedestroy($base);
?>Current example first; the original tutorial examples below are retained and cleaned up for continued learning.
$file_name="f1.jpg"; // File name on which water mark is to be added
$add="source/$file_name"; // Source directory location
$add2="destination/$file_name"; // Destination directory location
$water_img="wtt7.gif"; // Water Image file
You can see we have kept the water marked logo in root where the script is running. You can change all the above settings as per your requirement. Now we will read both the image sizes. Note that the array first element will store the width of the image and second element will store the height of the image. We will read dimensions for both our main image and water mark log image. Here it is
$img_ar=GetImageSize ($add); // reading source image size
$img_wt_ar=GetImageSize ($water_img); // reading water image size
Note that the above two variables are arrays. Now let us decide on the location on our main image where our water mark will be added. Note that left top corner is our 0 , 0 location. Let us fix this at 300 x 300 to keep the mark at center for a 600 x 600 image. You can change these values as per your requirement.
$location_height=300; // Location of water mark
$location_width=300; // Location of water mark
Now here is our main script which creates the water marked image
$im=ImageCreateFromJpeg($add); // for main image
$water_img1=ImageCreateFromgif($water_img); // for water image
$newimage=imagecreatetruecolor($img_ar[0],$img_ar[1]);//
imageCopyResized($newimage,$im,0,0,0,0,$img_ar[0],$img_ar[1],
$img_ar[0],$img_ar[1]);
$t=ImageCopy($newimage,$water_img1,$location_width,$location_height,
0,0,$img_wt_ar[0],$img_wt_ar[1]);
Take care about the write permission at destination directory. This script is in use at India travel picture gallery.
$file_name="f1.jpg"; // File name on which water mark is to be added
$add="source/$file_name"; // Source directory location
$add2="destination/$file_name"; // Destination directory location
$water_img="wtt7.gif"; // Water Image file
///////////////////////////Settings for the script over////////////
$img_ar=GetImageSize ($add); // reading source image size
$img_wt_ar=GetImageSize ($water_img); // reading water image size
$location_height=300; // Location of water mark
$location_width=300; // Location of water mark
$im=ImageCreateFromJpeg($add); // for main image
$water_img1=ImageCreateFromgif($water_img); // for water image
$newimage=imagecreatetruecolor($img_ar[0],$img_ar[1]);//
imageCopyResized($newimage,$im,0,0,0,0,$img_ar[0],$img_ar[1],
$img_ar[0],$img_ar[1]);
$t=ImageCopy($newimage,$water_img1,$location_width,$location_height,
0,0,$img_wt_ar[0],$img_wt_ar[1]);
ImageJpeg($newimage,$add2,100);
chmod("$add2",0666);
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.