on Basics

Add watermark on image using PHP

1 comments
php
Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon

So you have some images that you need watermarked, but it would be very time consuming to do it by hand. Or you have an application and want to add watermark to user uploaded images. You obviously can not ask the user to do it for you.

This simple solution will help you to make watermarked images on the fly, so your original images stay intact. First of all, you will need an image for the watermark and original image. In a live production you would get original image from the database.

You can see the demo below:


Then declare the header for jpeg image:

// this tells the browser to render jpg image
header('content-type: image/jpeg'); 

Next, we are creating a png image of our watermark and fetching its dimensions:

// creating png image of watermark
$watermark = imagecreatefrompng('watermark.png');  
// getting dimensions of watermark image
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);

We are doing the same for original picture, this time we are creating jpg. It is probably the best scenario that you always create jpg images from uploaded content. This way you can manipulate it more easily:

// creting jpg from original image
$image_path = 'original.jpg';
$image = imagecreatefromjpeg($image_path);  
// getting the dimensions of original image
$size = getimagesize($image_path);

Now, we need to place a watermark over the original image:

// placing the watermark 5px from bottom and right
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;

After that, we will use alpha blending function to set blending option for both images to true:

imagealphablending($image, true);
imagealphablending($watermark, true);

And finally, we are creating a new image and showing it to the world using taken parameters:

// creating the new image
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height); 
imagejpeg($image); 

It is important to free the used resources:

// destroying and freeing memory
imagedestroy($image);  
imagedestroy($watermark);

You can adjust the transparency effect of the watermark by reducing opacity for watermark image in Photoshop.

OK, I showed you the theory. Now let’s see how to use this in a live environment. This is the whole code that you will need to save in a file, let’s say, watermark.php:

// this tells the browser to render jpg image
header('content-type: image/jpeg'); 

// getting the image name from GET variable 
$image = $_GET['image']; 

// creating png image of watermark
$watermark = imagecreatefrompng('watermark.png');   

// getting dimensions of watermark image
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);  

// creting jpg from original image
$image_path = '/path/to/image/folder/' . $image;
$image = imagecreatefromjpeg($image_path);
//something went wrong 
if ($image === false) {
    return false;
} 
// getting the dimensions of original image
$size = getimagesize($image_path);  
// placing the watermark 5px from bottom and right
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;
// blending the images together
imagealphablending($image, true);
imagealphablending($watermark, true); 
// creating the new image
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height); 
imagejpeg($image);  
// destroying and freeing memory
imagedestroy($image);  
imagedestroy($watermark);  

Now, when you need to show watermarked image just use above code like this:


You just parse the image name to your watermark script, and use that as source of your image tag. Simple as that.

Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon



  • i’m getting a black square in place of the watermark image in my code. i have gd2.0.30 something.

    but im using imagecopyresized instead of imagecopy. can you help?

    this is what i have:

    $tempfile = $_FILES[‘filename’][‘tmp_name’];
    $src = imagecreatefromjpeg($tempfile);
    list($origWidth, $origHeight) = getimagesize($tempfile);
    // creating png image of watermark
    $watermark = imagecreatefrompng(‘../imgs/watermark.png’);
    // getting dimensions of watermark image
    $watermark_width = imagesx($watermark);
    $watermark_height = imagesy($watermark);
    // placing the watermark
    $dest_x = $origWidth / 2 – $watermark_width / 2;
    $dest_y = $origHeight / 2 – $watermark_height / 2;
    imagecopyresampled($src, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $origWidth, $origHeight);
    imagealphablending($src, true);
    imagealphablending($watermark, true);
    imagejpeg($src,$galleryPhotoLocation,100);