Full Stack LAMP - MEAN Developer, Python developer. Certified Azure Developer. Freelance programmer/consultant/trainer.

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

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments