What is the purpose of adding a watermark to an uploaded image using PHP?

Adding a watermark to an uploaded image using PHP helps protect the image from unauthorized use or distribution. It also helps to brand the image with a logo or copyright information. By adding a watermark, the original creator can ensure that their work is recognized and attributed properly.

// Load the original image
$source = imagecreatefromjpeg('original.jpg');

// Load the watermark image
$watermark = imagecreatefrompng('watermark.png');

// Set the position for the watermark
$watermark_x = imagesx($source) - imagesx($watermark) - 10;
$watermark_y = imagesy($source) - imagesy($watermark) - 10;

// Apply the watermark to the original image
imagecopy($source, $watermark, $watermark_x, $watermark_y, 0, 0, imagesx($watermark), imagesy($watermark));

// Save the modified image
imagejpeg($source, 'output.jpg');

// Free up memory
imagedestroy($source);
imagedestroy($watermark);