Advanced search  

News:

cpg1.5.36 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.34 or older update to this latest version as soon as possible.
[more]

Pages: [1] 2 3 4   Go Down

Author Topic: Watermark on a per image basis  (Read 144297 times)

0 Members and 1 Guest are viewing this topic.

Karri

  • Contributor
  • Coppermine novice
  • ***
  • Offline Offline
  • Gender: Female
  • Posts: 44
    • The Fuselage
Watermark on a per image basis
« on: January 22, 2004, 03:15:46 AM »

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
Code: [Select]
<?
//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
Code: [Select]
        return $url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);
and change it to

Code: [Select]
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

Code: [Select]
function add_picture($aid, $filepath, $filename, $title = '', $caption = '', $keywords = '', $user1 = '', $user2 = '', $user3 = '', $user4 = '', $category = 0, $raw_ip = '', $hdr_ip = '')
to

Code: [Select]
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
« Last Edit: August 25, 2004, 08:57:07 PM by GauGau »
Logged
Thanks,
Karri

DefenceTalk.com

  • Coppermine frequent poster
  • ***
  • Offline Offline
  • Posts: 230
    • http://www.defencetalk.com
Watermark on a per image basis
« Reply #1 on: January 22, 2004, 05:57:43 PM »

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?
Logged
(http://www.defencetalk.com/pictures/signature_cpg.php)

Karri

  • Contributor
  • Coppermine novice
  • ***
  • Offline Offline
  • Gender: Female
  • Posts: 44
    • The Fuselage
Watermark on a per image basis
« Reply #2 on: January 22, 2004, 06:27:12 PM »

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.
Logged
Thanks,
Karri

DefenceTalk.com

  • Coppermine frequent poster
  • ***
  • Offline Offline
  • Posts: 230
    • http://www.defencetalk.com
Watermark on a per image basis
« Reply #3 on: January 22, 2004, 07:00:18 PM »

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

Thanks it works great.
Logged
(http://www.defencetalk.com/pictures/signature_cpg.php)

Karri

  • Contributor
  • Coppermine novice
  • ***
  • Offline Offline
  • Gender: Female
  • Posts: 44
    • The Fuselage
Watermark on a per image basis
« Reply #4 on: January 22, 2004, 07:02:29 PM »

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

invision

  • Coppermine newbie
  • Offline Offline
  • Posts: 3
hello
« Reply #5 on: January 23, 2004, 04:21:00 PM »

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
Logged

DefenceTalk.com

  • Coppermine frequent poster
  • ***
  • Offline Offline
  • Posts: 230
    • http://www.defencetalk.com
Watermark on a per image basis
« Reply #6 on: January 23, 2004, 06:40:41 PM »

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.  :(
Logged
(http://www.defencetalk.com/pictures/signature_cpg.php)

Karri

  • Contributor
  • Coppermine novice
  • ***
  • Offline Offline
  • Gender: Female
  • Posts: 44
    • The Fuselage
Watermark on a per image basis
« Reply #7 on: January 23, 2004, 07:22:04 PM »

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.  

(http://www.baileychase.com/Gallery/watermark.png)
Logged
Thanks,
Karri

epsilon

  • Coppermine regular visitor
  • **
  • Offline Offline
  • Posts: 95
    • http://www.cremita.com
Some questions...
« Reply #8 on: January 23, 2004, 09:21:53 PM »

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?
Logged

hyperion

  • VIP
  • Coppermine addict
  • ***
  • Offline Offline
  • Posts: 1317
  • - retired -
Watermark on a per image basis
« Reply #9 on: January 24, 2004, 01:35:30 AM »

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.
Logged
&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

(http://www.mozilla.org/products/firefox/buttons/getfirefox_small.png)

epsilon

  • Coppermine regular visitor
  • **
  • Offline Offline
  • Posts: 95
    • http://www.cremita.com
Ok but..
« Reply #10 on: January 24, 2004, 10:58:57 AM »

┐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)
Logged

hyperion

  • VIP
  • Coppermine addict
  • ***
  • Offline Offline
  • Posts: 1317
  • - retired -
Watermark on a per image basis
« Reply #11 on: January 24, 2004, 06:28:54 PM »

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:

Code: [Select]

// 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:

Code: [Select]

// 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:

Code: [Select]

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:

Code: [Select]

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.
Logged
&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

(http://www.mozilla.org/products/firefox/buttons/getfirefox_small.png)

hyperion

  • VIP
  • Coppermine addict
  • ***
  • Offline Offline
  • Posts: 1317
  • - retired -
Watermark on a per image basis
« Reply #12 on: January 26, 2004, 08:05:47 AM »

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

Code: [Select]

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']);

}  
Logged
&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

(http://www.mozilla.org/products/firefox/buttons/getfirefox_small.png)

klaws

  • Contributor
  • Coppermine newbie
  • ***
  • Offline Offline
  • Posts: 11
hmm
« Reply #13 on: January 26, 2004, 12:55:53 PM »

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 :?:
Logged

ihorus

  • Coppermine newbie
  • Offline Offline
  • Posts: 2
Watermark on a per image basis
« Reply #14 on: January 26, 2004, 05:07:24 PM »

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.
Logged

Karri

  • Contributor
  • Coppermine novice
  • ***
  • Offline Offline
  • Gender: Female
  • Posts: 44
    • The Fuselage
Watermark on a per image basis
« Reply #15 on: January 26, 2004, 07:20:17 PM »

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.
Logged
Thanks,
Karri

klaws

  • Contributor
  • Coppermine newbie
  • ***
  • Offline Offline
  • Posts: 11
Watermark on a per image basis
« Reply #16 on: January 26, 2004, 07:37:04 PM »

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...
Logged

ihorus

  • Coppermine newbie
  • Offline Offline
  • Posts: 2
Watermark on a per image basis
« Reply #17 on: January 26, 2004, 08:21:53 PM »

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.
Logged

klaws

  • Contributor
  • Coppermine newbie
  • ***
  • Offline Offline
  • Posts: 11
Watermark on a per image basis
« Reply #18 on: January 30, 2004, 05:06:48 PM »

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
Logged

Frank

  • Coppermine newbie
  • Offline Offline
  • Posts: 10
Watermark on a per image basis
« Reply #19 on: February 01, 2004, 01:01:56 AM »

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:
Code: [Select]
return 'watermark.php?picturename='.$url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);
By:
Code: [Select]
return 'watermark.php?photo='.$pic_prefix[$mode]. $pic_row['filename'];


In watermark.php:

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

Add:
Code: [Select]
   $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?
Logged
Pages: [1] 2 3 4   Go Up
 

Page created in 0.087 seconds with 19 queries.