Watermark on a per image basis Watermark on a per image basis
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

Watermark on a per image basis

Started by Karri, January 22, 2004, 02:15:46 AM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Karri

Hopefully this is in the right place.  gaugau suggested I post this in a thread so here goes.  I am starting this off by saying this is bits and pieces of code from different hacks for watermarking with my add on for deciding which images get it and which do not.

First I made my watermark image and uploaded it to my main gallery directory.  My file was called watermark.png

Then I created a file called logo.php and uploaded it to the main gallery directory with the following code

<?
//Most coding by semisphere
//http://gallery.menalto.com/index.php?name=PNphpBB2&file=viewtopic&t=4053&start=0
// A few lines by BY DJ AXION
// e-mail: DJ@the-expansion.com
// Enjoy this script!



######################################################################################################
## YOUR settings HERE
######################################################################################################
                                                                                                                                                                                       
        // watermark IMAGE settings
       
        $watermark_width        = 143;                                        // watermark wanted width
       
        $watermark_height        = 20;                                        // watermark wanted height
       
        /*
        NOTE
       
        If the watermark is resized, transparency may contain lines and spots of your transparency color.
        Try to put the right size from the beginning
       
        */
       
        $opacity                = 90;                                        // 0 completely invisible
       
        $margin_x                = 0.1;                                        // margin from the right in pixels (x axis)
       
        $margin_y                = 0.1;                                        // margin from the bottom in pixels (y axis)
       
        $quality                = 100;                                         // 100 is maximum quality
       
        $watermark_image        = "watermark.png";       
        // Full path to image
       
        $watermark_type                = "PNG";                                 // JPEG or PNG
       
        $transColor                = array(0, 0, 0);                 // transparency color index in rgb
       
######################################################################################################
## DON'T EDIT BELLOW THIS LINE
## well, if you want to, you won't be busted ;-) 
######################################################################################################


       
        // get the file we want to watermark
        $file                        = imagecreatefromjpeg($picturename);

        // get the image details and create an image
        $image_width                = imagesx($file);
        $image_height                = imagesy($file);
        $image                        = $file;








if (!preg_match("/thumb_/i", "$picturename"))
{

                // get the watermark details, and open it
                $watermark_info                = getImageSize($watermark_image);

                eval ("\$watermark = ImageCreateFrom$watermark_type(\$watermark_image);");

                // calculate scale of watermark and create scaled watermark
                $scaled_watermark         = imageCreateTrueColor($watermark_width, $watermark_height);

                // resize the watermark to the new scale
                imageCopyResampled($scaled_watermark, $watermark, 0, 0, 0, 0, $watermark_width, $watermark_height, $watermark_info[0], $watermark_info[1]);

                // set the transparent color ($transColor)
                $transparentColor        = imagecolorallocate ($scaled_watermark, $transColor[0],$transColor[1],$transColor[2]);

                imagecolortransparent($scaled_watermark, $transparentColor);

                // add the watermark to the image
                 ImageCopyMerge($image, $scaled_watermark, $image_width - $watermark_width - ($watermark_width * $margin_x), $image_height - $watermark_height - ($watermark_height * $margin_y), 0, 0, $watermark_info[0], $watermark_info[1], $opacity);


}


       


        // send out a header
        header("content-type:image/jpeg");

        // send the image
        imagejpeg($image,'',$quality);

        // clean up
        imagedestroy($image);

?>



Next in include/functions.inc.php find
        return $url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);

and change it to

if ($pic_row['user1']!="YES") {

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


} else {

return 'logo.php?picturename='.$url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']); }


In the config section of your gallery set the "Custom fields for image description" Field 1 name to "Watermark".

Now is the optional part.  If you want the watermark on by default (it is only on by default for images you upload after you make this change, the other images you will have to go through and turn it on one by one.  I haven't found a way around that although I am sure there is a way to do it in one big batch) go to

include/picmgmt.inc.php and change

function add_picture($aid, $filepath, $filename, $title = '', $caption = '', $keywords = '', $user1 = '', $user2 = '', $user3 = '', $user4 = '', $category = 0, $raw_ip = '', $hdr_ip = '')

to

function add_picture($aid, $filepath, $filename, $title = '', $caption = '', $keywords = '', $user1 = 'YES', $user2 = '', $user3 = '', $user4 = '', $category = 0, $raw_ip = '', $hdr_ip = '')

The script looks for the  "YES" in the watermark field ($user1) so you can basically set it to anything other than YES and the watermark won't appear. 

I am sure this hack is pretty primitive so if anyone has a way to improve it, feel free.  :)  I hope someone finds this useful. :)


BTW: If you want to see it in action go to http://www.baileychase.com/Gallery/  most of the images have the watermark on check http://www.baileychase.com/Gallery/thumbnails.php?album=173 for a few that do not.

EDITED: Updated watermarking code to include the change Frank suggested for determining a thumbnail on page 3 of this thread
Thanks,
Karri

DefenceTalk.com

Hi, thanks for the "mod."

I tried but it shows red X intead of the pic.  :?  What resolution should the logo.png file be??? 256 color?
(https://coppermine-gallery.com/forum/proxy.php?request=http%3A%2F%2Fwww.defencetalk.com%2Fpictures%2Fsignature_cpg.php&hash=c0098ed5314d39beb9e8799c7c8787a9a0353b45)

Karri

I forgot to mention that you must have GD2 to make this particular watermark script work.  The choosing which images it is on is separate from that.

Mine is 2 colors.  I just kept it simple.  But realistically you could use any of the actual watermark scripts listed on this site.  DJ AXION's script on the old board will work great if you have used that before and it worked.  The main important bit for the deciding which images it is on is the if statement in the include/functions.inc.php file and setting the "Custom fields for image description" Field 1 name to "Watermark" and putting "YES" in that field for each picture you want to carry the watermark.  Once you have that value set to YES you can find the line that your watermark script replaces in the functions.inc.php file and add the if else conditional to it to get the same results.  I hope I explained that well.
Thanks,
Karri

DefenceTalk.com

Got it. I forgot to rename logo.png to watermark.png. :D ;)

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

Karri

Very cool. :)  I am glad it worked for you. :)
Thanks,
Karri

invision

can you help me to make this :

here is the demo of this : http://www.vietphp.info/a2album/

you can see the random pictures is  black and white color ! but when you click to the pic ! and this will come to original color !

tell me how can i make it ? Thanks

DefenceTalk.com

I was playing with the opecity option but can't really get it to work. I tried putting the original pic with transparency level of about 50% and changed the value in the logo.php file to 50 as well but it does not display it correctly.  :(
(https://coppermine-gallery.com/forum/proxy.php?request=http%3A%2F%2Fwww.defencetalk.com%2Fpictures%2Fsignature_cpg.php&hash=c0098ed5314d39beb9e8799c7c8787a9a0353b45)

Karri

I will attach my image here for you to play with if that will help.  It doesn't actually have any transparency in it.  It is just a two color image.  Black is the color that I want to be transparent so I have the $transcolor variable set to match.  Then change your $opacity variable to some different values to see the difference it makes on your site.  

(https://coppermine-gallery.com/forum/proxy.php?request=http%3A%2F%2Fwww.baileychase.com%2FGallery%2Fwatermark.png&hash=ce96e964fa679705aa1d5487264176bd13ff63af)
Thanks,
Karri

epsilon

I would know if this hack consumes resources extra of the server on having created the watermarks in the air...

How it works?

The hack Overwrites the original image or does it create a temporary image/photo to add the watermark?

Its possible select the albums where do you want add the watermark?

hyperion

Re: Batch change for $user1 -

An uppdate command to the pictures table for the user1 field would accomplish this.  You could add where clauses to update specific albums, etc.

@Epsilon:

Server resources/Method: Yes, this is an on-the-fly watermark system.

Possible to select an album: Yes, you could turn $user1 into an album id value in db_input. Then you could modify the if statement to check for the album IDs that you want to auto watermark.
&quot;Then, Fletch,&quot; that bright creature said to him, and the voice was very kind, &quot;let&#039;s begin with level flight . . . .&quot;

-Richard Bach, Jonathan Livingston Seagull

(https://coppermine-gallery.com/forum/proxy.php?request=http%3A%2F%2Fwww.mozilla.org%2Fproducts%2Ffirefox%2Fbuttons%2Fgetfirefox_small.png&hash=9f6d645801cbc882a52f0ee76cfeda02625fc537)

epsilon

¿It deletes the original non watermarked photos/imagens?

¿how about resources? it consumes a lot of?

Thnks (please an example with code of change $user1 to id of albums)

hyperion

Epsilon,

Quote from: "Epsilon"¿It deletes the original non watermarked photos/imagens?

No, this is an on-th-fly system, which means it creates the image in memory and sends it directly to the browser.

Quote from: "Epsilon"¿how about resources? it consumes a lot of?

Resource consumption depends on website/server traffic.  If you have a small site with your own server, you will probably not even notice it. If you have a high traffic site/server, you might notice image serving delays if your hardware is not good enough to handle the increased CPU/memory demand.  

Quote from: "Epsilon"Thnks (please an example with code of change $user1 to id of albums)

Make the following change to include/picmgmt.inc.php. Find:


// Add a picture to an album
function add_picture($aid, $filepath, $filename, $title = '', $caption = '', $keywords = '', $user1 = '', $user2 = '', $user3 = '', $user4 = '', $category = 0, $raw_ip = '', $hdr_ip = '')
{
    global $CONFIG, $ERROR, $USER_DATA, $PIC_NEED_APPROVAL;
    global $lang_errors;



Change it to:


// Add a picture to an album
function add_picture($aid, $filepath, $filename, $title = '', $caption = '', $keywords = '', $user1 = '', $user2 = '', $user3 = '', $user4 = '', $category = 0, $raw_ip = '', $hdr_ip = '')
{
    global $CONFIG, $ERROR, $USER_DATA, $PIC_NEED_APPROVAL;
    global $lang_errors;

    $user1 = $aid;


Then, take the following part of Karri's modification:


if ($pic_row['user1']!="YES") {

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


} else {

return 'logo.php?picturename='.$url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']); }  


and change it to:


if ($pic_row['user1']=1 or $pic_row['user1']=2 or $pic_row['user1']=3 ) {

return 'logo.php?picturename='.$url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);

} else {

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

}  


You can determine which album ID numbers should go in the "or" list by looking at the albums table in your database, or by looking at the HTML source to the upload page.

Please note that I have not tested this.
&quot;Then, Fletch,&quot; that bright creature said to him, and the voice was very kind, &quot;let&#039;s begin with level flight . . . .&quot;

-Richard Bach, Jonathan Livingston Seagull

(https://coppermine-gallery.com/forum/proxy.php?request=http%3A%2F%2Fwww.mozilla.org%2Fproducts%2Ffirefox%2Fbuttons%2Fgetfirefox_small.png&hash=9f6d645801cbc882a52f0ee76cfeda02625fc537)

hyperion

Alternatively, one can forgo the use of the $user1 field and rely directly on the album id, like so:


if ($pic_row['aid']=1 or $pic_row['aid']=2 or $pic_row['aid']=3 ) {

return 'logo.php?picturename='.$url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);

} else {

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

}  
&quot;Then, Fletch,&quot; that bright creature said to him, and the voice was very kind, &quot;let&#039;s begin with level flight . . . .&quot;

-Richard Bach, Jonathan Livingston Seagull

(https://coppermine-gallery.com/forum/proxy.php?request=http%3A%2F%2Fwww.mozilla.org%2Fproducts%2Ffirefox%2Fbuttons%2Fgetfirefox_small.png&hash=9f6d645801cbc882a52f0ee76cfeda02625fc537)

klaws

Cant seem to get it to work...
just get a red cross instead of the image.

i have installed it, made the fields, and put YES on one picture...

Maybe i'm missing something on the server?!?
running Linux, Apache, PHP4, GD + GD2, ImageMagick...

Any ideas :?:

ihorus

hi, Klaws i got the same problems that you have, the same red cross instead of the pic, if you found the solution, please let me know. Thanx.

Karri

Do any of the watermark mods posted around here work for you?  I started with the one from DJ AXION and added to it.  Maybe try those and see if you can get any of them to work, if yes then it is fairly easy to add on my yes/no part.
Thanks,
Karri

klaws

nope doesn't work either...

posted my problem here because the other post is getting toally
impossible to make anything out of now  :)

i think it's a server problem, but cant see what the problem is...

ihorus

ok,  i got it,!! now is working perfectly!! it was a studid "text document/Rich text format" error :lol:   Klaws, save your logo.php as a "text document" file not a "rich text format (rtf)" file.

klaws

Hmm... don't think that's my problem..

pasting the php code directly in "pico" in a shell.
And Linux certanly doesn't now what rtf is :)

-Klaws

Frank

I modified Karri's code to hide the path to the albums and to make the script work with global_registers Off (it was the reason why it wasn't working for me). If you want to protect the albums folders, simply make a ".htaccess" file with "deny from all" written in it and it'll be protected.


In include/functions.inc.php:

Replace:
return 'watermark.php?picturename='.$url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);
By:
return 'watermark.php?photo='.$pic_prefix[$mode]. $pic_row['filename'];


In watermark.php:

Before:
   // get the file we want to watermark
    $file            = imagecreatefromjpeg($picturename);

Add:
   $picture = $_GET[photo];
    $picturename = 'albums/userpics/'. $picture;



Now the pictures will be loaded as: watermark.php?photo=blablabla.jpg! It is very easy to modify if you want something else than "?photo=*" you only have two words to change.



#############################################
On the older board, there were a mod for watermark with GD that was allowing .gif and .png images to be watermarked, I didn't get it to work, anyone got it working with CPG 1.2.1?