Watermark images using PHP
To watermark images using PHP, you can use the GD Library, which is a graphics library bundled with PHP. Here's a basic PHP code example to add a watermark to an image:
here are the steps on how to create watermark images using PHP:
- Create a PHP script. The script will need to include the following functions:
imagecreatefromjpeg(): This function creates a new image from a JPEG file.imagecopy(): This function copies part of one image to another image.imagejpeg(): This function saves an image to a file.imagedestroy(): This function destroys an image resource.
- Load the original image and the watermark image. The original image is the image that you want to watermark. The watermark image is the image that you want to overlay on the original image.
- Set the position of the watermark image. You can use the
imagecopy()function to position the watermark image anywhere on the original image. The following code shows how to position the watermark image in the top-left corner of the original image:
$x = 0;
$y = 0;
imagecopy($im, $watermark, $x, $y, 0, 0, imagesx($watermark), imagesy($watermark));
- Save the watermarked image. Use the
imagejpeg()function to save the watermarked image to a file. - Destroy the image resources. Use the
imagedestroy()function to destroy the image resources for the original image and the watermark image.

<?php
// Load the original image and the watermark image.
$original_image = imagecreatefromjpeg("original.jpg");
$watermark_image = imagecreatefrompng("watermark.png");
// Set the position of the watermark image.
$x = 0;
$y = 0;
imagecopy($im, $watermark, $x, $y, 0, 0, imagesx($watermark), imagesy($watermark));
// Save the watermarked image.
imagejpeg($im, "watermarked.jpg");
// Destroy the image resources.
imagedestroy($original_image);
imagedestroy($watermark_image);
?>
Another example is here with an explanation;
<?php
// Function to add a watermark to an image
function addWatermark($sourceImagePath, $watermarkImagePath, $outputImagePath, $positionX = 10, $positionY = 10) {
// Load the source image and the watermark image
$sourceImage = imagecreatefromjpeg($sourceImagePath);
$watermarkImage = imagecreatefrompng($watermarkImagePath);
// Get the dimensions of the source and watermark images
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);
$watermarkWidth = imagesx($watermarkImage);
$watermarkHeight = imagesy($watermarkImage);
// Calculate the position to place the watermark on the source image
$positionX = $sourceWidth - $watermarkWidth - $positionX;
$positionY = $sourceHeight - $watermarkHeight - $positionY;
// Copy the watermark image onto the source image using transparency
imagecopy($sourceImage, $watermarkImage, $positionX, $positionY, 0, 0, $watermarkWidth, $watermarkHeight);
// Save the watermarked image to the output file path
imagejpeg($sourceImage, $outputImagePath);
// Free up memory by destroying the images
imagedestroy($sourceImage);
imagedestroy($watermarkImage);
}
// Example usage:
$sourceImagePath = 'path/to/source/image.jpg';
$watermarkImagePath = 'path/to/watermark/logo.png';
$outputImagePath = 'path/to/output/image_with_watermark.jpg';
// Call the function to add the watermark
addWatermark($sourceImagePath, $watermarkImagePath, $outputImagePath, 10, 10);
?>
In this example, we assume that the source image is a JPEG, and the watermark is a PNG image with transparency. The addWatermark function takes four parameters:
- $sourceImagePath: The path to the source image that you want to watermark.
- $watermarkImagePath: The path to the watermark image that will be added to the source image.
- $outputImagePath: The path where the watermarked image will be saved.
- $positionX (optional): The X-coordinate position of the watermark from the top-left corner of the source image.
- $positionY (optional): The Y-coordinate position of the watermark from the top-left corner of the source image.
You can adjust the $positionX and $positionY values to control the position of the watermark on the source image. Additionally, you may modify the function to handle other image formats or apply different watermarking effects based on your requirements.