skip ahead to content

we love web standards, css, html, actionscript, php, javascript, accesibility and usability

« SWF Communication and XKCD Christmas Banksy Desktop Backgrounds »

PHP Image Watermarking

I had to build this for a client rather swiftly but here is the brass tax of the matter. My client requested that every image in a section would be automatically water marked. I read a few good articles and played around a bit before creating this based on the others I had been reading about.

Criteria:

  1. Auto centered mark
  2. semi transparency
  3. works with 24-bit .png files for image watermark
  4. Must work on the fly
  5. must be able to watermark png/jpeg file types
  6. due Monday by noon :P

That being said I produced the following based on notes I took from various sites:

<?php
// example url
watermark.php?i=display.php?show=23
$imageUrl = $_GET['i'];
$watermarkUrl = “watermark.png”;
$orgimage=imagecreatefromjpeg(”{$imageUrl}”);
$watermark=imagecreatefrompng(”{$watermarkUrl}”);
$watermarkDetails=getimagesize(”{$watermarkUrl}”);
$imageDetails = getimagesize(”{$imageUrl}”);
$centerx = (($imageDetails[0] - $watermarkDetails[0])/2);
$centery = (($imageDetails[1] - $watermarkDetails[1])/2);
imagecopy($orgimage,$watermark,$centerx,$centery,0,0,
imagesx($watermark),imagesy($watermark));
header(”Content-Type: image/jpeg”);
imagejpeg($orgimage);
imagedestroy($watermark);
imagedestroy($orgimage);
exit;
?>

Monday, January 28th, 2008 at 6:02 pm and is filed under php, work. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply