lazy saunter

(personal learnings in computation)

Tuesday, April 03, 2007

apply watermark to a photo in PHP

as part of a page i might design for a photographer, i researched how to allow the artiste to display photos that are marked with a "PROOF" watermark to keep clients from just downloading the photos free and instead solicit them for a fee from the photographer. this is what i came up with as a script by modifying a script found online:

#!/usr/bin/php

// source image, watermark image, and final image
$source = "main_photo.jpg";
$watermark = "watermark.png";
$new_image = "new_image.jpg";

// prepare images for manipulation
$source = imagecreatefromjpeg($source);
$watermark = imagecreatefrompng($watermark);

// call function to imprint watermark on image
image_overlap($source, $watermark, $new_image);


function image_overlap($background, $foreground, $new_image){

// watermark dimensions
$insertWidth = imagesx($foreground);
$insertHeight = imagesy($foreground);

// image dimensions
$imageWidth = imagesx($background);
$imageHeight = imagesy($background);

// coordinates of lower right hand corner
$overlapX = $imageWidth-$insertWidth-5;
$overlapY = $imageHeight-$insertHeight-5;

// make watermark background transparent
imagecolortransparent($foreground,imagecolorat($foreground,0,0));

// merge the images together
imagecopymerge($background,$foreground,$overlapX,
$overlapY,0,0,$insertWidth,$insertHeight,30);
imagejpeg($background, $new_image);
}

?>

2 Comments:

  • At 11:44 PM , Anonymous Anonymous said...

    Ah! I wondered about that but am far too technologically unsophisticated to figure it out. Good thinking!

     
  • At 11:13 AM , Blogger Life So Far.... said...

    Here's a nice, easy watermarking tutorial. http://www.candeekis.com/pswatermark/pswmtut.html

     

Post a Comment

Subscribe to Post Comments [Atom]

<< Home