Wade,

>Im starting a new project with a security firm and Im trying to simply a
>lot of their horrific ways of doing things.
>
>One of the things I thought about is how they watermark their security
>images. They use a piece of software that costs thousands per year in
>licensing fees just to watermark images. So I started to look into doing
>this with php.
Very easy, did one like that for www.frimach.nl, but ...

>I quickly came across many different tutorials on this but I quickly
>realized that they were applying a static watermark.
I am applying a static watermark here as well. However, the principle
remains the same.

Basically consists of generating an overlay image, then generating on the
fly an image that is a combination of the real image with the overlay. So
instead of using always the same overlay you need to generate on the fly the
overlay as well.

>What Im working on right now is, for every image that is taken I must
>imprint the time, date, and location of the image in the bottom right
>hand corner.
Imprint also the name of the user (say: "generated for Wade") in the
overlay. Then if a protected image shows up somewhere on the internet, it is
obvious which user was responsible for leaking it. Best is to use a
"handwriting" style font for this as the shape of the characters is more
irregular, making it more difficult to photoshop it out.

>Anyone got any tutorial links for something like this?
Not really but there are tons of tutorials available on the internet. Below
is what I use:

function watermark_image($Image, $Dir, $Repeat = true) {
        // original from
http://www.fedeblog.com.ar/files/view.php?i=Watermark/watermark.php,
        // modificated by Liem Bahneman ([EMAIL PROTECTED]) from
http://rs.bahneman.com/wm.php.html,
        // modificated by Marc Boncz to be called as standardised function

        // global variables (defined in config.inc.php)
        global $ImageDir, $JPGQuality;

        // set some variables
        $WatermarkImage = "watermark.png";
        $Watermark      = $ImageDir.$WatermarkImage;
        $Overlay                = imagecreatefrompng($Watermark);
        $Extension      = substr($Image, -3);
        $Stop                   = false;

        // create the first layer of the new image
        switch (strtolower($Extension)) {
                case 'gif':
                        $Stop = !($NewImage = imagecreatefromgif($Dir.$Image));
                        break;
                case 'png':
                        $Stop = !($NewImage = imagecreatefrompng($Dir.$Image));
                        break;
                case 'jpg':
                        $Stop = !($NewImage = imagecreatefromjpeg($Dir.$Image));
                        break;
                default:
                        $Stop = true;
                        break;
        }

        // copy the watermarklayer into this
        if (!$Stop) {
                if($Repeat) {
                        // line below places watermark exactly in the middle of 
new image
                        imagecopy($NewImage, $Overlay,
(imagesx($NewImage)/2)-(imagesx($Overlay)/2),
(imagesy($NewImage)/2)-(imagesy($Overlay)/2), 0, 0, imagesx($Overlay),
imagesy($Overlay));
                        $Waterless      = imagesy($NewImage) - 
imagesy($Overlay);
                        $Rest                   = 
ceil($Waterless/imagesy($Overlay)/2);
                        // multiple watermarks tiled on image from center to 
borders
                        for($n=1; $n<=$Rest; $n++) {
                                imagecopy($NewImage, $Overlay,
(imagesx($NewImage)/2)-(imagesx($Overlay)/2),
((imagesy($NewImage)/2)-(imagesy($Overlay)/2))-(imagesy($Overlay)*$n), 0, 0,
imagesx($Overlay), imagesy($Overlay));
                                imagecopy($NewImage, $Overlay,
(imagesx($NewImage)/2)-(imagesx($Overlay)/2),
((imagesy($NewImage)/2)-(imagesy($Overlay)/2))+(imagesy($Overlay)*$n), 0, 0,
imagesx($Overlay), imagesy($Overlay));
                        }
                } else {
                        // line below places image in bottom right corner
                        imagecopy($NewImage, $Overlay, 
imagesx($NewImage)-imagesx($Overlay),
imagesy($NewImage)-imagesy($Overlay), 0, 0, imagesx($Overlay),
imagesy($Overlay));
                }
                // save the newly created image
                switch (strtolower($Extension)) {
                        case 'gif':
                                imageGIF($NewImage, $Dir.$Image, $JPGQuality);
                                break;
                        case 'png':
                                imagePNG($NewImage, $Dir.$Image, $JPGQuality);
                                break;
                        case 'jpg':
                                imageJPEG($NewImage, $Dir.$Image, $JPGQuality);
                                break;
                        default:
                                break;
                }
                imagedestroy($NewImage);
        }
        imagedestroy($Overlay);
}

To obtain what you want you might change the call to

function watermark_image($Image, $Dir, $Repeat = true, $Dynamic=true)

and then change the line

$Watermark      = $ImageDir.$WatermarkImage;

to something like

$Watermark =
(($Dynamic)?$ImageDir.generate_watermark():$ImageDir.$WatermarkImage);

where generate_watermark() is a call to a function that will generate an
image containing "generated 23/03/2008, 11:56:13 for Wade Smart" in a
difficult to obscure font.

use something like:

function generate_watermark() {
        // global variables (defined in config.inc.php)
        global $IncludeDate, $IncludeTime, $IncludeUser;

        // construct watermark text
        $WatermarkText = "generated ";
        $WatermarkText .= (($IncludeDate)?date().", ":"");
        $WatermarkText .= (($IncludeTime)?time().", ":"");
        $WatermerkText .= (($IncludeUser)?$_SESSION["Username"]:"");

        // generate an image from this
        do GD imagelibrary stuff

        return $Watermark;
}

That way you will have two flexible functions that without too complicated
modifications you can use in sites of other customers as well, when needed
(no use to code the same application twice), just put them in a general
functions repository. Using functions with standardised function calls,
whenever you find time to improve the functions, you just create a newer
version of your funcions repository which you can distribute to your older
clients as well, thus always providing them with up-to-date functionalty.

Marc

Reply via email to