Skip to content

PHP: watermark all over the image

In this tutorial, you will learn how to add a watermark on all over the base image. We gonna use PHP’s built-in functions to watermark images, no plugins or third-party libraries are needed.

<?php
$originalFilePath = "uploads/origianl-image.jpg";
$watermark_path='uploads/watermark.png';

$watermarkImage = imagecreatefrompng($watermark_path);
$originalImage = imagecreatefromjpeg($originalFilePath);

$originalImagegWidth= imagesx($originalImage);
$originalImagegHeight= imagesy($originalImage);

$waterMarkImageWidth = imagesx($watermarkImage);
$waterMarkImageHeight = imagesy($watermarkImage);

$x=0;
$y=0;
while($y<=$originalImagegHeight){

    imagecopymerge($originalImage, $watermarkImage, $x , $y , 0, 0, $waterMarkImageWidth, $waterMarkImageHeight, 50);
    
    $x+=$waterMarkImageWidth;
    if($x>=$originalImagegWidth){
        $x=0;
        $y+=$waterMarkImageHeight;
    }
}

$newFileName = "./watermark_added_image.png";

// Save the image to file
imagepng($originalImage, $newFileName);

// free memory
imagedestroy($originalImage);
imagedestroy($watermarkImage);

// Output to browser
header('Content-Type: image/png');
imagepng($originalImage);

my test output

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments