Watermarking hack (w/ ImageMagick) Watermarking hack (w/ ImageMagick)
 

News:

CPG Release 1.6.26
Correct PHP8.2 issues with user and language managers.
Additional fixes for PHP 8.2
Correct PHP8 error with SMF 2.0 bridge.
Correct IPTC supplimental category parsing.
Download and info HERE

Main Menu

Watermarking hack (w/ ImageMagick)

Started by flex, November 10, 2003, 11:50:10 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

flex

Hi there,

I just knocked up a quick hack to use water marking for coppermine. Check it out here: http://www.jvcam.co.uk/coppermine/

It doesn't alter any image on the server, just overlays the image every time a fullsize or normal image is requested (on-the-fly). It uses the 'composite' function of imagemagick but i think you can use a few of the others too to do the same thing. I'm no imagemagick expert.

The watermark image itself is a PNG file created in photoshop with alpha-layer transparency, you can find the one i use here: http://www.jvcam.co.uk/coppermine/watermark.png

If anyone's interested in the code, I'll try tidy it up and make it work for other people too.

I'm guessing that configuration could be put into coppermine to specify where the watermark is placed etc.

Before I tidy up the code, the imagemagick command I use is:


composite -compose over -gravity Southeast '".realpath("./watermark.png")."' '/home/users/jonny/www.jvcam.co.uk/html/coppermine/albums/$p' jpg:-


Obviously that's somewhat specific to my server cos I've not used any of the config includes from coppermine yet.

Anyways, enjoy :)

[edit]
It uses the full path of the image to pass to wm.php to get the image, but really this should use the db pid of the picture else it's not exactly secure.. but this is just a quick hack, i'll change it before i post the code ;)
[/edit]

moorey

Quote from: "flex"If anyone's interested in the code, I'll try tidy it up and make it work for other people too.

That would be great! Excellent work.

DefenceTalk.com

Who isn't interested in watermarking tool? I am!! I tried this another php based watermark tool... but i couldn't make it work!  :cry:

Only works with imagemagic???
(https://coppermine-gallery.com/forum/proxy.php?request=http%3A%2F%2Fwww.defencetalk.com%2Fpictures%2Fsignature_cpg.php&hash=c0098ed5314d39beb9e8799c7c8787a9a0353b45)

flex

ok here it is, it'll probably need some fixes. I've listed the known issues at the bottom:


Step 1. includes/functions.inc.php

Find this line right at the bottom of the script:

return $url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);


And change it to this:


        if((($mode == "normal") || ($mode == 'fullsize')) && ($CONFIG['thumb_method']=='im')){

                // return the url to the wm script
                return "wm.php?pid=".$pic_row["pid"]."&mode=".$mode;

        }else{
                // it's a thumb, don't bother wm'ing
                return $url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);

        }


Step 2. Create a new file in the coppermine root dir called wm.php:

<?
define('IN_COPPERMINE', true);
require('include/init.inc.php');

header('Content-Type: image/jpeg');
header('Content-Disposition: inline; filename=file.jpg');

$pid = $_REQUEST["pid"];
$mode = $_REQUEST["mode"];

$sql = "SELECT * FROM ".$CONFIG['TABLE_PREFIX']."pictures WHERE pid=$pid";
$db = mysql_connect($CONFIG['dbserver'], $CONFIG['dbuser'], $CONFIG['dbpass']);
mysql_select_db($CONFIG['dbname'], $db);


$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);

$pic_prefix = array(
    'thumb' => $CONFIG['thumb_pfx'],
    'normal' => $CONFIG['normal_pfx'],
    'fullsize' => ''
);


$cmd = "{$CONFIG['impath']}composite -compose over -gravity Southeast \"".realpath("./watermark.png")."\" \"".realpath('./'.$CONFIG["fullpath"].$row["filepath"].$pic_prefix[$mode].$row["filename"])."\" jpg:-";

passthru ($cmd, $output);

echo $output;
?>


Make sure you watch those line breaks when copying & pasting as it seems to have broken up some of them cos of page width etc.

Step 3. Get a copy of watermark.png

Get my one from here:
http://www.jvcam.co.uk/coppermine/watermark.png

And put it in your root coppermine directory.

Known Issues
1. Images stored on other servers, I've not even looked at how coppermine handles this but I know it does from looking at the code, but this doesn't support that and I'm not sure it would be possible for imagemagick to do it anyway with remote images.. Thinking it would take some php code to retrieve the image, wm it and then display it - I guess it's possible.

2. GD. GD implementation I've not even looked at yet, I don't know if it's possible but I'll try have a look in the next couple of days and see if it's possible. That said, if you're using GD, then it shouldn't effect the way you use coppermine, it just won't watermark the images.

3. At present there's no way to turn it on or off through the config screen, this is something the other dev's can do easily I'm sure, I didn't get stuck into the config stuff. The only way you can turn it off is to remove the altered script lines. Also, it watermarks both 'normal' and 'fullsize' images, there's no option to turn either one on/off yet.

4. Versions. It should work in anything greater than coppermine 1.1, since that's what I tested it with and I checked the 1.2 code and there's no changes in the lines I altered.

5. I'll try and produce a tutorial in creating the watermark images because it's not exactly straight forward and it has to be a png file. I couldn't get transparent gifs to work at all.

6. Server load. I've no idea how much server load it takes to wm an image every time it's requested as I don't have full access to the server I work on... Anyone who does have access and can check, I'd be interested to see how much load it creates.

I corrected it so that it doesn't pass the full url to the wm.php script, it now uses the pid of the image to find it and look it up in the db. This should somewhat stop people from stealing your images by finding the full path but it's not infallable.

I'm sure there's a few other issues too that I'm not aware of so I encourage people to try it out and see what happens and report back to this thread.

Last note, the wm image I use is this one http://www.jvcam.co.uk/coppermine/watermark.png
Those with photoshop can check it out to see how it works.

Good luck!

- Flex :D

moorey

Thanks, flex!
FYI, moved this to the MOD section..

DJMaze

There are 2 kinds of users in this world: satisfied and complainers.
Why do we never hear something from the satisfied users?
http://coppermine-gallery.net/forum/index.php?topic=24315.0

flex

Yeh I had a look yesterday at GD but my server is playing up saying it's running out of memory when using createimagefromjpeg(), not sure what's up with that. Thought it was my code to start with but I tried switching coppermine to GD 1 or 2 and I get the same probs :\

Getting this error message:

Fatal error:  Allowed memory size of 8388608 bytes exhausted (tried to allocate 6400 bytes) in /home/users/jonny/www.jvcam.co.uk/html/coppermine/wm2.php on line 26

DJMaze

It seems a lot of servers have this problem.
A jpg is converted to a truecolor GD bitmap when imported.
It seems this uses a lot of memmory or is even a bug.

http://www.php.net/manual/en/function.memory-get-usage.php is a new function since php 4.3.2 check if you can use it and trace the allocated memsize for php tha will maybe give a solution.
There are 2 kinds of users in this world: satisfied and complainers.
Why do we never hear something from the satisfied users?
http://coppermine-gallery.net/forum/index.php?topic=24315.0

flex

I used phpinfo() to check the memory allocation for PHP, and it's only 8mb. Which I'm guessing is not enough for the images I'm using to test watermarking, 1600 x 1200.

Unfortunately I can't change the amount of memory allocated for PHP as I don't have that kind of access to the server :\

DJMaze

well hack yeah that kind of image uses a lot of memory, try to reduce the color use when opening in GD the function is in image functions list of php.net
There are 2 kinds of users in this world: satisfied and complainers.
Why do we never hear something from the satisfied users?
http://coppermine-gallery.net/forum/index.php?topic=24315.0

DJMaze

What i realy mentioned with the new memory_get_usage() function is:echo memory_get_usage();
createfromjpeg();
echo memory_get_usage();


This way we can trace how much memory is actualy needed for the image.
There are 2 kinds of users in this world: satisfied and complainers.
Why do we never hear something from the satisfied users?
http://coppermine-gallery.net/forum/index.php?topic=24315.0

flex

The thing is with the error, is that it's a fatal error message so nothing gets processed after imagecreatefromjpeg. So even trying echo memory_get_usage(); doesn't work :\

Is there an actual fix? Cos I don't see why GD should just fail on a image that size, it's not like it's that massive in terms of digital camera photos.

terrorhawk

Hi there..

i was trying to add the watermark hack but when it wants to open the pic in full size (so not the thumpnail) i get a X (image not found).
and i dont have any iedea what this could be.

please help..
if you need the files i changed just ask and i mail

flex

If you could email me your changed files then I can have a look, email them to jonny (at) jvcam (dot) co (dot) uk

I'll check them and see what's up with them. It could be possible that your installation of imagemagick on your server doesn't have the 'composite' command installed, but I don't think this is likely.

I await your email :)

hardlocke

hi
i use the watermark-hack from "DJ AXION".... it's great!

- Link to Thread in the old board
- Sample

terrorhawk

Quote from: "flex"It could be possible that your installation of imagemagick on your server doesn't have the 'composite' command installed, but I don't think this is likely.

I await your email :)

it is possible couse i run it at my own server. sow also i can check what the cpu load will be and memory load but it has to working first..

i will mail it today to you

flex

Can you send me the link to your gallery too? I had a quick look at the code, doesn't seem to be anything wrong with it, but I can check the errors if I can see your gallery.

flex

ok all fixed up for working in windows as well now. Imagemagick in windows didn't like using single quotes around the filenames, changed them to double quotes.

Thanks to terrorhawk for letting me test on his server & helping me fix it!

I updated the source on my previous post to reflect.

gdn

OK, I installed the hack and here's what I got:

Fatal error: Call to undefined function: get_pic_url() in /home/sites/site72/web/coppermine/index.php on line 462

 :?
Loïc

gdn

Forget it... I used DJ Axion hack, and it runs OK.

The only problem I have is that I want to allow people to download the pics. And when you click right and click "save as", "logo.htm" is in the name field. I would like something like "file" or "image", just like in your hack. An idea about what I have to do?
Loïc