Jump to content

Recommended Posts

Posted

This original script was written by Mikko and can be found here:

 

http://valokuva.org/?cat=1&paged=2

 

I modified the script so that it would watermark all images in a directory. This doesn't actually put a permanent mark on the image. You only see the watermark when you view the image with your browser. If you move the image to a another directory you won't see the watermark anymore.

 

First create a .htaccess file in your image directory and add this to it:

 

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(gif|jpeg|jpg|png)$ watermarker.php [QSA,NC]

 

Next create a file named watermarker.php and add this script to it:

 

<?php

/* set image path */
$img = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];

/* Read the image in. This image will be watermarked. */
$image = new Imagick($img);

/* get and set image format */
$type = strtolower($image->getimageformat());
$image->setimageformat($type);

/* get image width and height */
$w = $image->getimagewidth();
$h = $image->getimageheight();

/* The text to write on the mark */
$text = "byrondallas.heliohost.org";

/* This object will hold the font properties */
$draw = new ImagickDraw();

/* Setting gravity to the center changes the origo
       where annotation coordinates are relative to */
$draw->setGravity(Imagick::GRAVITY_CENTER);

/* Use a custom truetype font or server font */
$draw->setFont("Utopia-Bold-Italic");

/* Set the font size */
$draw->setFontSize(26);

/* Create a new imagick object */
$im = new imagick();

/* Get the text properties */
$properties = $im->queryFontMetrics($draw, $text);

/* Region size for the watermark.
       Add some extra space on the sides  */
$watermark['w'] = intval($properties["textWidth"] + 10);
$watermark['h'] = intval($properties["textHeight"] + 10);

/* Create a canvas using the font properties.
       Add some extra space on width and height */
$im->newImage($watermark['w'], $watermark['h'], 
                   new ImagickPixel("transparent"));

/* Get a region pixel iterator to get the pixels in the watermark area */
$it = $image->getPixelRegionIterator(0, 0, $watermark['w'], $watermark['h']);

$luminosity = 0;
$i = 0;

/* Loop trough rows */
while($row = $it->getNextIteratorRow())
{
       /* Loop trough each column on the row */
       foreach ($row as $pixel)
       {
               /* Get HSL values */
               $hsl = $pixel->getHSL();
               $luminosity += $hsl['luminosity'];
               $i++;
       }
}

/* If we are closer to white, then use black font and
       the other way around */
$textColor = ( ( $luminosity / $i )> 0.5 ) ?
new ImagickPixel("black") : new ImagickPixel("white");

/* Use the same color for the shadow */
$draw->setFillColor($textColor);

/* Use png format */
$im->setImageFormat("png");

/* Annotate some text on the image */
$im->annotateImage($draw, 0, 0, 0, $text);

/* Clone the canvas to create the shadow */
$watermark = $im->clone();

/* Set the image bg color to black. (The color of the shadow) */
$watermark->setImageBackgroundColor($textColor);

/* Create the shadow (You can tweak the parameters
       to produce "different" kind of shadows */
$watermark->shadowImage(80, 2, 2, 2);

/* Composite the text on the background */
$watermark->compositeImage($im, Imagick::COMPOSITE_OVER, 0, 0);

/* get width and height of watermark image */
$ww = $im->getimagewidth();
$wh = $im->getimageheight();

/* Composite the watermark on the image to the bottom righthand corner. Use 0, 0 for top left */
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $w-$ww, $h-$wh);

/* Display the results */
header("Content-Type: image/$type");
echo $image;
?>

 

That's it. Now anytime somebody viesw one of your images they will see a watermark similar to the ones you see on my two examples:

 

http://byrondallas.heliohost.org/images/fs/little-cowboy.jpg

 

http://byrondallas.heliohost.org/images/fs/carmen_electra_106.jpg

 

btw, this only works on the Stevie server. Johnny doesn't have the Imagick Class.

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...