Hey everyone,
I did my 'watermarking' my own way ! Using text rather than images. This way, the text can be easily changed by editing the setting in zp-config.php, and there is no need to create an image.
You can see it here (just get to an image)
http://test.lostocean.net/zp/
The code calculates the text color based on the average color of the area that is underneath !
Looks pretty cool Skwid, think you could make it transparent and anti-aliased?
If you want, bundle it up and submit a patch at http://bugs.zenphoto.org.
In case anyone wanted to see it in action, I have a zenphoto implementation up at http://www.photosbydaisy.com/zenphoto which is utilizing Skwid's watermarking function.
Thinkdreams
Hum trisweb, I'm not sure if i can make transparent text using GD functions, plus it would be hard to read it ...
By the way, I wrote you an email, but never had an answer. If you didn't have time that's fine, but if you didn't get it, please tell me so I can resend it.
This might do the trick:
http://www.phpbuilder.com/snippet/download.php?type=snippet&id=68
I haven't had a chance to try it yet though. It solves the different font thing we were talking about the other night Skwid.
Ok, I got the watermarking working with a transparent PNG made from custom text, font, color, whatever
You can see it in action at the usual URL:
This is color transparency, i still need to work on alpha transparency.
I still need to polish it a little.. but it works !
Okay. First of all, sorry for being late. I completely forgot about it.
Here is what needs to be done. For ease of configuration, there are some changes to the zp-config.php file AS WELL as the i.php file. I will try and step it out with easy instructions, but if I am unclear on anything, please let me know. This will eventually be the wiki page.
`
// turn the watermarking on and off
$conf['perform_watermark'] = true;
// the image (png-24) in the zen/images/ directory
$conf['watermark_image'] = "images/watermark.png";
`
You can add those anywhere ABOVE the line that says:
`
/ Do not edit below this line. /
/**/
`
`
$perform_watermark = zp_conf('perform_watermark');
$watermark_image = zp_conf('watermark_image');
`
// Create the cached file (with lots of compatibility)...`
//-- watermarking
if ($perform_watermark == true) {
$watermark = imagecreatefrompng($watermark_image);
imagealphablending($watermark, false);
imagesavealpha($watermark, true);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
// Position Overlay in Bottom Right
$dest_x = imagesx($newim) - $watermark_width;
$dest_y = imagesy($newim) - $watermark_height;
imagecopy($newim, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagedestroy($watermark);
}
//-- /watermarking
`
That is it. Make sure you put the image in the zen/images/ directory and it will work.
This WILL also add the watermark to the thumbs. To turn that off, add this line in the thumbnailing part og the i.php file (about line 62). Right after the $thumb = true;.
$perform_watermark = false;
Is that wiki worthy?
works good for me nathan. the image is a 50% opacity .png created with a text font in photoshop.
Very nice solution to watermarking.
The only problem that I have with this hack is that it will only work for those themes which use values set in zp-config.php because those themes use the [b]getImageThumb()[/b] and [b]printImageThumb()[/b] functions. For both of my themes, I never used those because I wanted to basically "hard-code" (i.e. [b]getCustomImageURL()[/b]) the sizes of the thumbnails. In order to get it to work with that function, I'll have to do some major work with [b]i.php[/b] and even [b]classes.php[/b].
Also, I'd like to make a suggestion to your code.
Instead of having you copy & paste code in three different areas of [b]i.php[/b], just copy and paste the following into i.php "at about line 213, just before the // Create the cached file (with lots of compatibility)"
`////////////////////////////////////////////////////////////////////////////////
// Watermark Hack
////////////////////////////////////////////////////////////////////////////////
$perform_watermark = zp_conf('perform_watermark');
$watermark_image = zp_conf('watermark_image');
if ($perform_watermark == true && $thumb == false) {
$watermark = imagecreatefrompng($watermark_image);
imagealphablending($watermark, false);
imagesavealpha($watermark, true);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
// Position Overlay in Bottom Right
$dest_x = imagesx($newim) - $watermark_width;
$dest_y = imagesy($newim) - $watermark_height;
imagecopy($newim, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagedestroy($watermark);
}
////////////////////////////////////////////////////////////////////////////////`
This code also takes care of the fact that it's a thumbnail.
Okay, after further thought, here goes a fix that will work for my themes (and maybe others that use the [b]getCustomImageURL()[/b]). The only thing is that you'll have to edit more than just [b]i.php[/b].
copy and paste the following code into [b]i.php[/b] around line 213 (just as stated in the above post):
`////////////////////////////////////////////////////////////////////////////////
// Watermark Hack
////////////////////////////////////////////////////////////////////////////////
$perform_watermark = zp_conf('perform_watermark');
$watermark_image = zp_conf('watermark_image');
if ($perform_watermark == true && $thumb == false && $_GET['wm'] == true) {
$watermark = imagecreatefrompng($watermark_image);
imagealphablending($watermark, false);
imagesavealpha($watermark, true);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
// Position Overlay in Bottom Right
$dest_x = imagesx($newim) - $watermark_width;
$dest_y = imagesy($newim) - $watermark_height;
imagecopy($newim, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagedestroy($watermark);
}
////////////////////////////////////////////////////////////////////////////////`
now from lines 257-263 of [b]classes.php[/b] change the function from:
`function getCustomImage($size, $width, $height, $cropw, $croph, $cropx, $cropy) {
return WEBPATH . "/zen/i.php?a=" . urlencode($this->album->name) . "&i=" . urlencode($this->filename)
. ($size ? "&s=$size" : "" ) . ($width ? "&w=$width" : "") . ($height ? "&h=$height" : "")
. ($cropw ? "&cw=$cropw" : "") . ($croph ? "&ch=$croph" : "")
. ($cropx ? "&cx=$cropx" : "") . ($cropy ? "&cy=$cropy" : "") ;
}`
to the following:
`function getCustomImage($size, $width, $height, $cropw, $croph, $cropx, $cropy, $wm=true) {
return WEBPATH . "/zen/i.php?a=" . urlencode($this->album->name) . "&i=" . urlencode($this->filename)
. ($size ? "&s=$size" : "" ) . ($width ? "&w=$width" : "") . ($height ? "&h=$height" : "")
. ($cropw ? "&cw=$cropw" : "") . ($croph ? "&ch=$croph" : "")
. ($cropx ? "&cx=$cropx" : "") . ($cropy ? "&cy=$cropy" : "")
. ($wm ? "&wm=$wm" : "") ;
}`
Then in [b]template-functions.php[/b] change lines 554-558 from:
`function getCustomAlbumThumb($size, $width=NULL, $height=NULL, $cropw=NULL, $croph=NULL, $cropx=NULL, $cropy=null) {
global $_zp_current_album;
$thumb = $_zp_current_album->getAlbumThumbImage();
return $thumb->getCustomImage($size, $width, $height, $cropw, $croph, $cropx, $cropy);
}`
to the following:
`function getCustomAlbumThumb($size, $width=NULL, $height=NULL, $cropw=NULL, $croph=NULL, $cropx=NULL, $cropy=null, $wm=false) {
global $_zp_current_album;
$thumb = $_zp_current_album->getAlbumThumbImage();
return $thumb->getCustomImage($size, $width, $height, $cropw, $croph, $cropx, $cropy, $wm);
}`
Also in [b]template-functions.php[/b] change lines 892-903 from:
`function getCustomImageURL($size, $width=NULL, $height=NULL, $cropw=NULL, $croph=NULL, $cropx=NULL, $cropy=NULL) {
global $_zp_current_image;
return $_zp_current_image->getCustomImage($size, $width, $height, $cropw, $croph, $cropx, $cropy);
}`
`function printCustomSizedImage($alt, $size, $width=NULL, $height=NULL, $cropw=NULL, $croph=NULL, $cropx=NULL, $cropy=NULL, $class=NULL, $id=NULL) {
$sizearr = getSizeCustomImage($size, $width, $height, $cropw, $croph, $cropx, $cropy);
echo "[img][/img]";
}`
to the following:
`function getCustomImageURL($size, $width=NULL, $height=NULL, $cropw=NULL, $croph=NULL, $cropx=NULL, $cropy=NULL, $wm=true) {
global $_zp_current_image;
return $_zp_current_image->getCustomImage($size, $width, $height, $cropw, $croph, $cropx, $cropy, $wm);
}`
`function printCustomSizedImage($alt, $size, $width=NULL, $height=NULL, $cropw=NULL, $croph=NULL, $cropx=NULL, $cropy=NULL, $class=NULL, $id=NULL, $wm=true) {
$sizearr = getSizeCustomImage($size, $width, $height, $cropw, $croph, $cropx, $cropy);
echo "[img][/img]";
}`
What this code does is that by default, when you use [b]getCustomImageURL()[/b] it will automatically watermark your image (this way your images are ALWAYS protected. If you want to turn it off (to maybe make a custom-sized thumbnail), just set the [b]8th[/b] parameter of the function to [b]false[/b]. However, it's the opposite for [b]getCustomAlbumThumb()[/b]. Because we have it explicitly get a thumb, we have $wm be false by default.
That should do it.
This is a fairly lengthy hack. I'll take a look at seeing if there is a different way to do this. I tired my best to make the code small, and this is really a bit too much for what I was after.
I like this bit though:
`////////////////////////////////////////////////////////////////////////////////
// Watermark Hack
////////////////////////////////////////////////////////////////////////////////
$perform_watermark = zp_conf('perform_watermark');
$watermark_image = zp_conf('watermark_image');
if ($perform_watermark == true && $thumb == false && $_GET['wm'] == true) {
$watermark = imagecreatefrompng($watermark_image);
imagealphablending($watermark, false);
imagesavealpha($watermark, true);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
// Position Overlay in Bottom Right
$dest_x = imagesx($newim) - $watermark_width;
$dest_y = imagesy($newim) - $watermark_height;
imagecopy($newim, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagedestroy($watermark);
}
////////////////////////////////////////////////////////////////////`
Thanks for that.
Well, if this will be worked in "officially" it would be great if not only would we be able to specify the default watermark image in the config, but if we were also able to specify a different watermark image during runtime.
This would be useful, for example, if I wanted a default watermark on my bigger images (say it was a logo and my domain name), but then later on decide to watermark my thumbnails with just the logo. When I call the function to specify a thumbnail, I could just easily give it the name of the new watermark.
Dax-
I was able to get the antialiasing working on my site OK. What seems to not be working?
Thinkdreams.
In case you haven't seen it, http://www.thinkdreams.com/devzp is where my test site is at.
I used Nathan's original code, without the GameDudeX modifications (just haven't had time to integrate them yet).
Thinkdreams