Image watermark with PHP

To prevent quality images being stolen, we can use PHP to watermark web-images in popular formats like GIF/PNG/JPEG. We print a transparent gif-image on a jpeg-photo in this tutorial. For best results I prefer gif than png, because some png formats require extra functions to print a transparent image. We can convert this script to a batch-watermarker easily to watermark photo albums/galleries with multiple pictures by putting code in a loop or create a function.

Steps:

  1. Load both images
  2. Get size of both images
  3. Copy watermark to main image
  4. Print image to screen



PHP functions:
imagecreatefromgif
imagecreatefromjpeg
getimagesize
imagecopymerge
header
imagejpeg
imagedestroy


Watermark image:




Main image:




PHP Code:

<?php  

$main_img 		= "Porsche_911_996_Carrera_4S.jpg"; // main big photo / picture
$watermark_img	= "watermark.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding 		= 3; // distance to border in pixels for watermark image
$opacity		= 100;	// image opacity for transparent watermark

$watermark 	= imagecreatefromgif($watermark_img); // create watermark
$image 		= imagecreatefromjpeg($main_img); // create main graphic

if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");


$watermark_size 	= getimagesize($watermark_img);
$watermark_width 	= $watermark_size[0];  
$watermark_height 	= $watermark_size[1];  

$image_size 	= getimagesize($main_img);  
$dest_x 		= $image_size[0] - $watermark_width - $padding;  
$dest_y 		= $image_size[1] - $watermark_height - $padding;


// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);


// print image to screen
header("content-type: image/jpeg");   
imagejpeg($image);  
imagedestroy($image);  
imagedestroy($watermark);  



Example images:

Watermarked image with 10% opacity




Watermarked image with 50% opacity




Watermarked image with 100% opacity




Comments

On the fly Watermark

Hello,
this is really great. However, I was wondering how to accomplish the following:
-Present the visitor with a field which he has to fill in (say with his name or a message) and an image,
-As he types, his text is displayed above the image and saved as such.
I've been searching throughout the web but can't find anything, could you help?

Tnx

Simple Examples FTW!!

Thanks for this striped down example, its great. First few examples I’d seen had loads to do with file writing as well, that just confused the matter. But this really shows just how simple it is.

this code help me a lot .

Thanks ~!

Jpeg to Gif

Can we convert jpeg file to gif format when we upload files in php.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <pre> <h1> <h2> <h3> <h4> <h5> <h6> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <div> <style><img> <br> <blockquote>
  • Lines and paragraphs break automatically.
  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

CafeWebmaster.com(CW) is a free online community for webdevelopers and beginners. Anybody can share their code, articles, tips, tutorials, code-examples or other webdesign related material on the site. Newbies can submit their questions and reply to existing questions. CW does not guarantee or warrant reliability of code, data and information published on the site. Use the site on your own risk. The site takes no responsibility of direct or indirect loss or any kind of harm to its users. The site also doesn't take responsibility of infected files or source code with any kind of infection or viruses, worms, spywares, malwares, trojan horses. CW reserves the right to edit, move, or delete any of content for any reason.