As a freelance web developer, I once encountered a situation where a client requested an overhaul of their old website, including adding watermarks to all existing images. With over 10,000 images scattered across various folders and subfolders, manually adding watermarks seemed impractical.
To tackle this daunting task efficiently, I decided to write a custom PHP script. The script would recursively traverse through the image directories, apply the client's logo as a watermark to each image, and save the watermarked images in a designated folder.
Despite the images varying in size from 200 to 7000 pixels in width, the script dynamically adjusted the watermark placement and size for uniformity. It utilized the GD library for PHP to handle image processing tasks seamlessly.
After a few minutes of script execution, all images were successfully watermarked, ready to be integrated into the revamped website. This automation not only saved significant time but also ensured consistency across the entire image collection.
By sharing this personal experience, I hope to highlight the importance of leveraging automation and scripting tools in handling repetitive tasks efficiently, ultimately streamlining web development processes.
Below is a PHP script example for bulk adding a watermark to photos using the GD library. This script assumes you have a logo named logo.png that you want to use as the watermark. Make sure to replace path/to/img with the actual path to your image folder and adjust other parameters as needed.
// Set the path to your image folder.
$imageFolder = "path/to/img";
// Set the path to save watermarked images.
$destinationFolder = "path/to/img2";
// Load the watermark image.
$watermark = imagecreatefrompng("logo.PNG");
// Get the dimensions of the watermark.
$watermarkWidth = imagesx($watermark);
$watermarkHeight = imagesy($watermark);
// Traverse through all files in the image folder.
$files = scandir($imageFolder);
foreach ($files as $file) {
// Check if the file is an image (you may need to adjust the condition based on your file types).
if (is_file($imageFolder . "/" . $file) && in_array(pathinfo($file, PATHINFO_EXTENSION), ["jpg", "jpeg", "png", "gif"])) {
// Load the original image.
$image = imagecreatefromjpeg($imageFolder . "/" . $file); // Change function based on file type
// Get the dimensions of the original image.
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
// Calculate the position to place the watermark at the center.
$positionX = ($imageWidth - $watermarkWidth) / 2;
$positionY = ($imageHeight - $watermarkHeight) / 2;
// Apply the watermark onto the original image.
imagecopy($image, $watermark, $positionX, $positionY, 0, 0, $watermarkWidth, $watermarkHeight);
// Save the watermarked image to the destination folder.
imagejpeg($image, $destinationFolder . "/" . $file); // Change function based on file type
// Free up memory.imagedestroy($image); }}
imagedestroy($watermark);
Comments (0)