Watermark Images on the Fly in PHP

thanks for it, i was thinking to watermark my project’s images (not ready yet) and u gave me a great idea.

Can PHP do the invisible watermark

Is there any way to do this to Videos using PHP?

You could call an instance of vlc using the exec function to watermark your videos.

I had to add this after the second line to make it work with png-24 and alpha channel:

imageAlphaBlending($watermark, false);
imageSaveAlpha($watermark, true);

when i try and use this watermark script the image does not show in my browser? i get a small box with a red x in it. I am a novice what’ s causing this? thanks

This does not work in the new firefox 2 for some reason, what the hell? Isn’t it all server-sided???

I know this is an old thread, but for all of you who want to apply watermarks on the fly, you might want to check the open-source project Asido: there are a lot of useful features when creating applying [URL=“http://www.asido.info/about/features/#watermark”]watermarks, like [URL=“http://www.asido.info/howto/watermark/tile/”]tile watermarks or [URL=“http://www.asido.info/howto/watermark/scaling/”]watermark scaling. There’s no problem at all using transparent images both as watermarks or as images that are about to be watermarked. Here’s a brief example copied from [URL=“http://asido.info/”]Asido website:

<?php
 
/**
* Include the main Asido class
*/
include('./../../class.asido.php');
 
/**
* Set the correct driver: this depends on your local environment
*/
asido::driver('gd');
 
/**
* Create an Asido_Image object and provide the name of the source
* image, and the name with which you want to save the file
*/
$i1 = asido::image('example.jpg', 'result_01.jpg');
 
/**
* Put a watermark image 
*/
Asido::watermark($i1, 'watermark_01.png');
 
/**
* Save the result
*/
$i1->save(ASIDO_OVERWRITE_ENABLED);
 
?>

You can see the results here: http://www.asido.info/howto/watermark/gravity/

I too failed to get the origional script to work… But after reading all your posts today about asido, I downloaded asido this afternoon from soundforge and hey! It works!! :slight_smile:

The only thing it did that confused me was it made my watermark way smaller, but digging through the examples I found that by default it reduces the watermark to 25%. This reduction can be eliminated by adding:–

, ASIDO_WATERMARK_SCALABLE_DISABLED

and, even neater, a custom scale can be added, for example 66%:-

, ASIDO_WATERMARK_SCALABLE_ENABLED, 0.66

If you want to watermark images then give asido a try.

This is howto tile your watermark…call your image in html like this

<img src=“watermark.php?file=image.jpg”></img>

contents of watermark.php

<?php
// watermark.php
// 4/29/2007 11:32:42 PM

header(‘content-type: image/jpeg’);

$watermark = imagecreatefrompng(‘watermark.png’);

$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);

$alpha=15;

$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($_GET[‘file’]);
$size = getimagesize($_GET[‘file’]);

$dest_x = $size[0];
$dest_y = $size[1];

$c_dest_x = 0;
$c_dest_y = 0;

// loop/tile watermark until image full
while ($dest_x > $c_dest_x && $dest_y >= $c_dest_y)
{ // insert wartermark

imagecopymerge($image, $watermark, $c_dest_x, $c_dest_y, 0, 0, $watermark_width, $watermark_height, $alpha);

// math for new watermark postition
if ($c_dest_x < $dest_x)
{ $c_dest_x = $c_dest_x + $watermark_width; }

if ($c_dest_x >= $dest_x && $c_dest_y <= $dest_y)
{ $c_dest_x = 0;
$c_dest_y = $c_dest_y + $watermark_height;
}
}

imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);

?>

Yeah, indeed the default behaviour is to have the watermark scaling ON and 1/4 scaling factor, and this is because in most cases you don’t need your watermark to be bigger then the image you are watermarking. Anyway, I am glad that you find Asido useful, and even more that you find the information on the website is helpful - I don’t want to be one of those open-source projects that lack comprehensible documentation.

Recently I’ve been thinking about how to improve the watermark scaling. Right now the scale factor is applied to the watermark image without any regard to the image being watermarked. Let me show you an example: you have an image that is 100x100, and you have a watermark that is 800x800; since the watermark is bigger, the watermark scaling will try to reduce it by the defautl scaling factor, which 0.25 (1/4), so the watermark will become 200x200 and it will be still bigger than the image that’s about to be watermarked. The solution
is to tie the scaling factor not to the dimensions of the watermark, but to the dimensions of thre image, so when the scaling factor is 1/4, this means that the watermark will become 1/4 from the size of the watermarked image. Using 100% (1.0) as scaling factor will cover the whole image w/ the watermark. Anyway, I don’t want to drop the way the scaling is applied right now, so probably there will be a new “mode” for the scaling, and the available options will be: ASIDO_WATERMARK_SCALABLE_DISABLED, ASIDO_WATERMARK_SCALABLE_ENABLED_TARGET (current implementation) and ASIDO_WATERMARK_SCALABLE_ENABLED_SOURCE (the feature I just explained)

good

If I wanted to add this script to a CMS, what would be the easiest way to call it into the existing code? Would I “include once” on the script in the header and use variables where you have the $GET statements? And how would I then transfer the watermarked image to my TPL file for diplay?

You may actually want to use:
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);

instead of imagecopymerged()

For some reason there was some problems using imagecopymerged(). The watermark was converted into a jpg then merged without transparency.

I found anothe r script that used htaccess to autmoattically wrap any image called.

it used;
addhandler wtmrk jpg
action wtmrk watermark.php

Any idea how to achive this here?

This only works for jpg’s?

I can not get it to work for gif’s.

Any ideas?

To make it work for GIFs, you should use imagecreatefromgif() instead of imagecreatefromjpeg().

While browsing PHP.net’s site I stumbled upon a user’s snipplet for a function named imageCreateFromFile() somewhere in:
http://es.php.net/gd

I use a modified version of it.

really great post!

i can’t get this to work…

here’s what I did…

<?php
header(‘content-type: image/jpeg’);

$watermark = imagecreatefrompng(‘watermark.png’);

$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);

$image = imagecreatefromjpeg($_GET[‘originalImage.jpg’]);

$size = getimagesize($_GET[‘originalImage.jpg’]);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);

imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>

am i missing something obvious? i don’t really write php!

please help!

can’t watermask animated gif.