Permanent Watermark with GD2 Permanent Watermark with GD2
 

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

Permanent Watermark with GD2

Started by Burpee, June 20, 2004, 11:43:25 PM

Previous topic - Next topic

0 Members and 10 Guests are viewing this topic.

Burpee

I was unable to modify my original post and I do think that the top post should be the newest version. So I suggest you either split this post off or move it to the first somehow.

I decided to "modify" this mod so it'll work in CPG 1.3 and my brother helped me fix the "_normal" bug

Permanent Watermark with GD2 MOD by Sammy


For CPG users who wants to add GD2 watermark function on their gallery.

This MOD uses GD2!

Here is how to mod CPG 1.3 to be able to add watermark automatically on new pics and by choise on all pics in selected album.
This is quite long post, but it takes only about 10 minutes to add this mod

Files that needs to be modified:
Lang/english.php
include/picmgmt.inc.php
util.php
config.php
(and 4 SQL command to execute)


REMEMBER to take backup from all files that You are modifying


1.
Open file Lang/english.php

Find this:'th_wd' => 'Width',

Add this after it

                'wm_bottomright' => 'Bottom right',
                'wm_bottomleft' => 'Bottom left',
                'wm_topleftt' => 'Up left',
                'wm_topright' => 'Up Right',
                'wm_both' => 'Both',
                'wm_original' => 'Original',
                'wm_resized' => 'Resized',


Find this:

  array('Display notices in debug mode', 'debug_notice', 1), //cpg1.3.0


Add this after it


               'Image watermarking',
               array('Watermark Image on upload', 'enable_watermark', 1),
               array('Where to place the watermark', 'where_put_watermark', 11),
               array('which files to watermark', 'which_files_to_watermark', 12),
               array('Which file to use for watermark', 'watermark_file', 0),

               
Find this:  'delete_orphans' => 'Delete orphaned comments (works on all albums)',

Add this a line after it
        'watermarks' => 'Add watermarks',
        'watermark_normal' => 'Resized images only',
        'watermark_image' => 'Original sized only',
        'watermark_both' => 'Original and resized images',
        'watermark_wait' => 'Watermarking images...',
        'watermark_continue_wait' => 'Continuing to watermarking originals and/or resized images...',



2.
Open file include/picmgmt.inc.php

Find this:$image_filesize = filesize($image);

Add this before it
       //Adds watermarks to new images
       if ($CONFIG['enable_watermark'] == '1' && $CONFIG['which_files_to_watermark'] == 'both' || $CONFIG['which_files_to_watermark'] == 'original') {
               if (!watermark($image))  //this is for full size image
                       return false;
       }
       if ($CONFIG['enable_watermark'] == '1' && $CONFIG['which_files_to_watermark'] == 'both' || $CONFIG['which_files_to_watermark'] == 'resized') {
       if (file_exists($normal) && !watermark($normal))  //this is for intermediate size image
           return false;
       }
               

The following line has been modified to fix the the "_normal bug"
       if (file_exists($normal) && !watermark($normal))  //this is for intermediate size image



Find this:?> (or end of picmgmt.inc.php)

Add this before it
        /**
         * watermark_image()
         *
         * Create a file containing a watermarked image
         *
         * @param  $src_file the source file
         * @param  $dest_file the destination file
         * @param  $new_size the size of the square within which the new image must fit
         * @param  $method the method used for image resizing  //ainoastaan gd2
         * @return 'true' in case of success
         */
        function watermark($src_file)
        {
            global $CONFIG, $ERROR;
            global $lang_errors;

            $imginfo = getimagesize($src_file);
            if ($imginfo == null)
                return false;
            // GD can only handle JPG & PNG images
            if ($imginfo[2] != GIS_JPG && $imginfo[2] != GIS_PNG) {
                $ERROR = $lang_errors['gd_file_type_err'];
                return false;
            }
            // height/width
            $srcWidth = $imginfo[0];
            $srcHeight = $imginfo[1];
         /*   if ($thumb_use == 'ht') {
                $ratio = $srcHeight / $new_size;
            } elseif ($thumb_use == 'wd') {
                $ratio = $srcWidth / $new_size;
            } else {
                $ratio = max($srcWidth, $srcHeight) / $new_size;
            }
            $ratio = max($ratio, 1.0);*/
            $destWidth = $srcWidth; //(int)($srcWidth / $ratio);
            $destHeight = $srcHeight; //(int)($srcHeight / $ratio);

                        $dest_file = $src_file;
            // Method for thumbnails creation
        //    switch ($method) {
        //        case "gd2" :
                    if (!function_exists('imagecreatefromjpeg')) {
                        cpg_die(CRITICAL_ERROR, 'PHP running on your server does not support the GD image library, check with your webhost if ImageMagick is installed', __FILE__, __LINE__);
                    }
                    if (!function_exists('imagecreatetruecolor')) {
                        cpg_die(CRITICAL_ERROR, 'PHP running on your server does not support GD version 2.x, please switch to GD version 1.x on the config page', __FILE__, __LINE__);
                    }
                    if ($imginfo[2] == GIS_JPG)
                        $src_img = imagecreatefromjpeg($src_file);
                    else
                        $src_img = imagecreatefrompng($src_file);
                    if (!$src_img) {
                        $ERROR = $lang_errors['invalid_image'];
                        return false;
                    } //duunataan wesileima
                    $dst_img = imagecreatetruecolor($destWidth, $destHeight);
                                        /*$dst_img =*/   ImageAlphaBlending($dst_img, true) or die ("Could not alpha blend"); // Enable when on GD 2+
                   imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
                                   $logoImage = ImageCreateFromPNG($CONFIG['watermark_file']);
                                        $logoW = ImageSX($logoImage);
                                        $logoH = ImageSY($logoImage);
                                        //where is the watermark displayed...
                                        $pos = $CONFIG['where_put_watermark'];
                                        if ($pos == "topleft") {
                                        $src_x = 5; 
                                        $src_y = 5;
                                        } else if ($pos == "topright") {
                                        $src_x = $srcWidth - ($logoW + 5);
                                        $src_y = 5;
                                        } else if ($pos == "bottomleft") {
                                        $src_x = 5;
                                        $src_y = $srcHeight - ($logoH + 5);
                                        } else if ($pos == "bottomright") {
                                        $src_x = $srcWidth - ($logoW + 5);
                                        $src_y = $srcHeight - ($logoH + 5);
                                        }

                                        ImageCopy($dst_img,$logoImage,$src_x,$src_y,0,0,$logoW,$logoH); //$dst_x,$dst_y,0,0,$logoW,$logoH);
                    imagejpeg($dst_img, $src_file, $CONFIG['jpeg_qual']);
                    imagedestroy($src_img);
                    imagedestroy($dst_img);
                //    break;
            //}
            // Set mode of uploaded picture
            // We check that the image is valid
            $imginfo = getimagesize($src_file);
            if ($imginfo == null) {
                $ERROR = $lang_errors['resize_failed'];
                @unlink($src_file);
                return false;
            } else {
                return true;
            }
        }


Burpee

#1
3.
Open File util.php

Find this: function deleteorig()

Add this before it
function vesileimaathumbit()   //watermarkthumbs
{
   global $picturetbl, $CONFIG, $lang_util_php;
   $phpself = $_SERVER['PHP_SELF'];
   $albumid = $_POST['albumid'];
   $updatetype = $_POST['updatetype'];
   $numpics = $_POST['numpics'];
   $startpic = 0;
   $startpic = $_POST['startpic'];

   $query = "SELECT * FROM $picturetbl WHERE aid = '$albumid'";
   $result = MYSQL_QUERY($query);
   $totalpics = mysql_numrows($result);

   if ($startpic == 0) {
       // 0 - numpics
       $num = $totalpics;
       // Over picture limit
       if ($totalpics > $numpics) $num = $startpic + $numpics;
   } else {
       // startpic - numpics
       $num = $startpic + $numpics;
       if ($num > $totalpics) $num = $totalpics;
   }

   $i = $startpic;
   while ($i < $num) {
       $image = $CONFIG['fullpath'] . mysql_result($result, $i, "filepath") . mysql_result($result, $i, "filename");

       if ($updatetype == 0 || $updatetype == 2) {
           $imagesize = getimagesize($image);
           if (max($imagesize[0], $imagesize[1]) > $CONFIG['picture_width'] && $CONFIG['make_intermediate'])        {
           $image = $CONFIG['fullpath'] . mysql_result($result, $i, "filepath") . $CONFIG['normal_pfx'] . mysql_result($result, $i, "filename");
                       }        else        {
           $image = $CONFIG['fullpath'] . mysql_result($result, $i, "filepath") . mysql_result($result, $i, "filename");
                       }

           if (watermark($image)) {
               print $image .' '. $lang_util_php['updated_succesfully'] . '!<br />'; //$normal .' '. $lang_util_php['updated_succesfully'] . '!<br />';
               my_flush();
           } else {
               print $lang_util_php['error_create'] . ':$image<br />'; //$lang_util_php['error_create'] . ':$normal<br />';
               my_flush();
           }
       }

       if ($updatetype == 1 || $updatetype == 2) {
           $image = $CONFIG['fullpath'] . mysql_result($result, $i, "filepath") . mysql_result($result, $i, "filename");

           if (watermark($image)) {
               print $image .' '. $lang_util_php['updated_succesfully'] . '!<br />'; //$normal .' '. $lang_util_php['updated_succesfully'] . '!<br />';
               my_flush();
           } else {
               print $lang_util_php['error_create'] . ':$image<br />'; //$lang_util_php['error_create'] . ':$normal<br />';
               my_flush();
           }
       }

       ++$i;
   }
   $startpic = $i;

   if ($startpic < $totalpics) {

       ?>
           <form action=<?php echo $phpself;
        
?>
method="post">
                   <input type="hidden" name="action" value="continuewatermarks" />
                   <input type="hidden" name="numpics" value="<?php echo $numpics?>" />
                   <input type="hidden" name="startpic" value="<?php echo $startpic?>" />
                   <input type="hidden" name="updatetype" value="<?php echo $updatetype?>" />
           <input type="hidden" name="albumid" value="<?php echo $albumid?>" />
           <input type="submit" value="<?php print $lang_util_php['continue'];
        
?>
" class="submit" /></form>
                   <?php
    
}
}  



Find this:updatethumbs();

Add this after it

   echo '<br /><a href="' . $phpself . '">' . $lang_util_php['back'] . '</a>';
} else if ($action == 'watermarks') {//tämä
   global $picturetbl, $CONFIG;
   print '<a href="' . $phpself . '">' . $lang_util_php['back'] . '</a><br />';
   print '<h2>' . $lang_util_php['watermark_wait'] . '</h2>';

   vesileimaathumbit();  //watermarkthumbs

   echo '<br /><a href="' . $phpself . '">' . $lang_util_php['back'] . '</a>';
} else if ($action == 'continuewatermarks') {//tämä
   print '<a href="' . $phpself . '">' . $lang_util_php['back'] . '</a><br />';
   print '<h2>' . $lang_util_php['watermark_wait'] . '</h2>';

   vesileimaathumbit();  //watermarkthumbs


Find this:starttable('100%', '<input type="radio" name="action" value="title" id="title" class="nobg"
(only part of that line)

Add this before it
//This is for watermarking
   starttable('100%', '<input type="radio" name="action" value="watermarks" id="watermarks" class="nobg" /><label for="watermarks" accesskey="w" class="labelradio">' . $lang_util_php['watermarks'] . '</label> (1)');
   print '
<tr><td>
' . $lang_util_php['update_what'] . ' (2):<br />
<input type="radio" name="updatetype" value="0" id="thumb" class="nobg" /><label for="thumb" accesskey="w" class="labelradio">' . $lang_util_php['watermark_normal'] . '</label><br />
<input type="radio" name="updatetype" value="1" id="resized" class="nobg" /><label for="resized" accesskey="r" class="labelradio">' . $lang_util_php['watermark_image'] . '</label><br />
<input type="radio" name="updatetype" value="2" checked="checked" id="all" class="nobg" /><label for="all" accesskey="a" class="labelradio">' . $lang_util_php['watermark_both'] . '</label><br />
' . $lang_util_php['update_number'] . '
<input type="text" name="numpics" value="' . $defpicnum . '" size="5" /><br />
' . $lang_util_php['update_option'] . '<br /><br />
</td></tr>';
   endtable();

   print '<br />';




4.
Open file config.php

Find this:function create_form(&$data)

Add this above it
// Added for allowing user to select where to add watermarks on pics...
function form_watermark($text, $name)
{
   global $CONFIG, $lang_config_php;

   $value = $CONFIG[$name];
   $bottomright_selected = ($value == 'bottomright') ? 'selected' : '';
   $bottomleft_selected = ($value == 'bottomleft') ? 'selected' : '';
   $topleftt_selected = ($value == 'topleftt') ? 'selected' : '';
   $topright_selected = ($value == 'topright') ? 'selected' : '';

   echo <<<EOT
       <tr>
           <td class="tableb">
                       $text
       </td>
       <td class="tableb" valign="top">
                       <select name="$name" class="listbox">
                               <option value="bottomright" $bottomright_selected>{$lang_config_php['wm_bottomright']}</option>
                               <option value="bottomleft" $bottomleft_selected>{$lang_config_php['wm_bottomleft']}</option>
                               <option value="topleftt" $topleftt_selected>{$lang_config_php['wm_topleftt']}</option>
                               <option value="topright" $topright_selected>{$lang_config_php['wm_topright']}</option>
                       </select>
               </td>
       </tr>

EOT;
}
// Added for allowing user to select which files to watermark...
function form_watermark2($text, $name)
{
   global $CONFIG, $lang_config_php;

   $value = $CONFIG[$name];
   $both_selected = ($value == 'both') ? 'selected' : '';
   $original_selected = ($value == 'original') ? 'selected' : '';
   $resized_selected = ($value == 'resized') ? 'selected' : '';

   echo <<<EOT
       <tr>
           <td class="tableb">
                       $text
       </td>
       <td class="tableb" valign="top">
                       <select name="$name" class="listbox">
                               <option value="both" $both_selected>{$lang_config_php['wm_both']}</option>
                               <option value="original" $original_selected>{$lang_config_php['wm_original']}</option>
                               <option value="resized" $resized_selected>{$lang_config_php['wm_resized']}</option>
                       </select>
               </td>
       </tr>

EOT;
}



Find this:                case 10 :
                   form_number_dropdown($element[0], $element[1]);
                   break;


Few lines below isdie('Invalid action');

Now we need to paste this
              //Watermark place
              case 11 :
                  form_watermark($element[0], $element[1]);
                                      break;
              //Which filest to watermark
              case 12 :
                  form_watermark2($element[0], $element[1]);
                                      break;
              case 13 :
                  // do nothing
                  break;
              default:
between them.

After You have done it, it should look like this (I have put couple lines befor and after those lines in this example (hoppe You got it, becouse of My poor english :?
               // tabbed display fix
               case 10 :
                   form_number_dropdown($element[0], $element[1]);
                   break;

              //Watermark place
              case 11 :
                  form_watermark($element[0], $element[1]);
                                      break;
              //Which filest to watermark
              case 12 :
                  form_watermark2($element[0], $element[1]);
                                      break;
              case 13 :
                  // do nothing
                  break;
              default:
                   die('Invalid action');
           } // switch
       } else {
           form_label($element);



5.
Then You need to execute theese SQL commands with phpMyAdmin or Webmin, etc.

insert into cpg11d_config (name, value) values ('enable_watermark', '1');
insert into cpg11d_config (name, value) values ('where_put_watermark', 'bottomright');
insert into cpg11d_config (name, value) values ('watermark_file', '/your/full/path/to/logo.png');
insert into cpg11d_config (name, value) values ('which_files_to_watermark', 'both');


Just to make things entirely clear for beginners:

insert into cpg11d_config (name, value) values ('enable_watermark', '1');
insert into cpg11d_config (name, value) values ('where_put_watermark', 'bottomright');
insert into cpg11d_config (name, value) values ('watermark_file', '/*******/**/*********/htdocs/gallery/watermark.png');
insert into cpg11d_config (name, value) values ('which_files_to_watermark', 'both');

the prefix highlighted in red should be edited by the user to make sure it corresponds to their gallery prefix.. latest CPG should have cpg130 as prefix.

the path to the watermark image highlighted in blue has to be the full directory path to the image and not a URL


Thats it. Now You can go to settings and set it up.
Actually You need to go there, before trying to add new pics.
You have to change 'Which file to use for watermark' to point to your own logo.png file that is used as watermark.


This should contain every modification I have made.

- Made by Sammy

khayman

Hey all. I've added a bit of code to allow you the option to CENTER the watermark on the image. It's not much of a contribution but... it's a contribution nonetheless! ;)

First, make the necessary changes to the files as above.

Further changes need to be made to the following files:

Lang/english.php
include/picmgmt.inc.php
config.php

As above, remember to make backups of all the files you are editing!

1. Open file Lang/english.php

Find this:

'wm_topright' => 'Up right',


Add this after it:

'wm_centered' => 'Centered',


2. Open file include/picmgmt.inc.php

Find this:

} else if ($pos == "bottomright") {
$src_x = $srcWidth - ($logoW + 5);
$src_y = $srcHeight - ($logoH + 5);


Add this after it:

} else if ($pos == "centered") {
$src_x = ($srcWidth/2) - ($logoW/2);
$src_y = ($srcHeight/2) - ($logoH/2);


3. Open file config.php

Find this:

$bottomleft_selected = ($value == 'bottomleft') ? 'selected' : '';


And add this after it:

$centered_selected = ($value == 'centered') ? 'selected' : '';


Find this:

<option value="bottomleft" $bottomleft_selected>{$lang_config_php['wm_bottomleft']}</option>


And add this after it:

<option value="centered" $centered_selected>{$lang_config_php['wm_centered']}</option>


Note: the above two lines place the CENTERED option on the configuration page in alphabetical order, you can put it in any order within the

<select name="$name" class="listbox">

section of the code, as long as it is after the above code and before the

</select>

line of code.

That's it! All you have to do now is upload your pages and login as an administrator and update your configuration through the config button.

rhydewithdis

First off - thanks for putting this together... maybe someone can assist with this error message I get after when trying to use the watermark feature (everything else worked fine):

PHP running on your server does not support the GD image library, check with your webhost if ImageMagick is installed

Joachim Müller

check phpinfo to see if you actually have GD (refer to the FAQ for details).

GauGau

tanfwc

is there any way to update all images after changing the watermark and the position after a few time?
tanfwc
[ + ] My HomePage
[ + ] GuestBook
[ + ] Photo Gallery
[ + ] Free Image Hosting

Joachim Müller

Watermarking is ireversible, that's why this mod is called "permament watermarking". I'm not sure what you mean though.

GauGau

tanfwc

Quote from: GauGau on July 01, 2004, 12:51:26 PM
Watermarking is ireversible, that's why this mod is called "permament watermarking". I'm not sure what you mean though.

GauGau

Thank you for replying. What i mean is that if i have apply Watermark A and after a while i decided to change to Watermark B, will the picture that has watermark A able to change to watermark B?
tanfwc
[ + ] My HomePage
[ + ] GuestBook
[ + ] Photo Gallery
[ + ] Free Image Hosting

Joachim Müller

no, you can only add watermark B to the already watermarked pic afaik. It's strongly recommended you do watermarking on your client before uploading, will bring much better results and less load on the server. There are some good watermarking apps like PictureShark available for free.

GauGau

tanfwc

Quote from: GauGau on July 01, 2004, 03:18:32 PM
no, you can only add watermark B to the already watermarked pic afaik. It's strongly recommended you do watermarking on your client before uploading, will bring much better results and less load on the server. There are some good watermarking apps like PictureShark available for free.

GauGau

Thank you for pointing out such a good software
tanfwc
[ + ] My HomePage
[ + ] GuestBook
[ + ] Photo Gallery
[ + ] Free Image Hosting

No0dle


tanfwc

tanfwc
[ + ] My HomePage
[ + ] GuestBook
[ + ] Photo Gallery
[ + ] Free Image Hosting

No0dle

Well i get that but how do you do that with phpAdmin what do you have to do there in the admin

tanfwc

why i apply the hack and the images come out like that?

http://www.tanfwc.com/beta/albums/userpics/10001/Shannon%20on%20Sandy%232.jpg

i am using a transparent images and the watermark is in PNG format...strange...
tanfwc
[ + ] My HomePage
[ + ] GuestBook
[ + ] Photo Gallery
[ + ] Free Image Hosting

nyt84

Thanks for the watermark hack..i love it.
For those who want a demo look of the hack, take a look at my gallery
http://gallery.greensg.org

The hack only apply to resized pics only.


Cheers!!

tanfwc

Quote from: tanfwc on July 02, 2004, 10:58:12 AM
why i apply the hack and the images come out like that?

http://www.tanfwc.com/beta/albums/userpics/10001/Shannon%20on%20Sandy%232.jpg

i am using a transparent images and the watermark is in PNG format...strange...

anyone?
tanfwc
[ + ] My HomePage
[ + ] GuestBook
[ + ] Photo Gallery
[ + ] Free Image Hosting

DefenceTalk.com

Excellent mod. Thank you!

Anyway to apply the watermark on existing pictures as well as those that are uploaded after this mod was added?

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

tanfwc

Quote from: DefenceTalk.com on July 05, 2004, 06:34:31 AM
Excellent mod. Thank you!

Anyway to apply the watermark on existing pictures as well as those that are uploaded after this mod was added?

thanks!

go to Admin Tools and update those images with watermark...
tanfwc
[ + ] My HomePage
[ + ] GuestBook
[ + ] Photo Gallery
[ + ] Free Image Hosting

WreckRman2

Quote from: nyt84 on July 02, 2004, 11:58:20 AM
Thanks for the watermark hack..i love it.
For those who want a demo look of the hack, take a look at my gallery
http://gallery.greensg.org

The hack only apply to resized pics only.


Cheers!!



Hey nyt84, Very nice watermark... I'm no graphic artist would you make me a watermark for a few dollars?
David Smith
Indy Web Design - http://www.indywebdesign.com
317-573-2486

We offer web hosting that includes easy installation for Coppermine Photo Gallery, phpBB2, PHP-Nuke, and a lot more...

tanfwc

Can this work with the lastest CPG 1.3.1?
tanfwc
[ + ] My HomePage
[ + ] GuestBook
[ + ] Photo Gallery
[ + ] Free Image Hosting