How to set watermark on image in php

Hello Friends !

Today we are going to do how to set watermark on image in php . yes you can set a watermark with text or image on your image with the help of php.

You do not need any supporting files or plugins to make watermark only Using PHP you can create different types of watermarks on all of your images. I am going to show you a way how to dynamically put a text/messages on your images. What you need is a JPG/png whatever type of  image file used for generate the watermark message. i have zipped a demo files in below code for your help and better understanding to implement the code ..

without watermark php

follow the only one simple step :-

1) Set watermark with image on image :

// WaterMark with Image //

$stamp = imagecreatefrompng('logo.png');
$im = imagecreatefromjpeg("bus.png");
$save_watermark_photo_address ="watermark.png";

// Set the margins for the stamp and get the height/width of the stamp image

$marge_right = 60; // here you can set adjustment of your image
$marge_bottom =60; // 60 is just for my case you can choose according you.
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp.

imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
// header('Content-type: image/png');

imagejpeg($im, $save_watermark_photo_address, 80); 

2) Set watermark with text on image :

// Load the stamp and the photo to apply the watermark to
$im = imagecreatefromjpeg('photo.jpg');

// First we create our stamp image manually from GD
$stamp = imagecreatetruecolor(100, 70);
imagefilledrectangle($stamp, 0, 0, 99, 69, 0x0000FF);
imagefilledrectangle($stamp, 9, 9, 90, 60, 0xFFFFFF);
$im = imagecreatefromjpeg('photo.jpg');
imagestring($stamp, 5, 20, 20, 'Divakar', 0x0000FF);
imagestring($stamp, 3, 20, 40, '(c) 2007-9', 0x0000FF);

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Merge the stamp onto our photo with an opacity of 50%
imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);

// Save the image to file and free memory
imagepng($im, 'photo_stamp.png');

Download the source code by clicking the button below.

Click Here to Download Source Code





Author: Divakarcool
I am Divakar Kumar Software Engineer in IT Company since 3 Years.I studied Master in Computer Application(MCA). I love to read and write code and blogs, watching movies, Video games.

Leave a Reply

Hey there!

Sign in

Forgot password?

Processing files…