Watermark transparency/blending with ImageMagick

Tmack

Joined: 2005-06-01
Posts: 5
Posted: Mon, 2006-10-02 17:25

Noticing the watermarking done by the 1.x version of Gallery doesnt do blending/transparency by default with ImageMagic (aside from transparent gif/png), I edited a few files to add it. The below diff is off of 1.5.4. It adds a field to the watermark forms to allow the user to enter a percentage for the watermark to be blended. Previewing the watermark will show the expected results. Note that this simply turns on the ImageMagick -dissolve feature and passes around the $dissolve var. It appears NetPBM already includes alpha blending of some sort, but I dont have NetPBM to test. It looks like adding -opacity=($dissolve/100) to the NetPBM $args should accomplish the same results as this does with ImageMagick, but I didnt add since again, I dont have it installed to test with.

diff -r gallery/classes/Album.php ../gallery/classes/Album.php
1155c1155
<     function addPhoto($file, $tag, $originalFilename, $caption, $pathToThumb = '', $extraFields = array(), $owner = '', $votes = NULL, $wmName = '', $wmAlign = 0, $wmAlignX = 0, $wmAlignY = 0, $wmSelect = 0, $exifRotate = true) {
---
>     function addPhoto($file, $tag, $originalFilename, $caption, $pathToThumb = '', $extraFields = array(), $owner = '', $votes = NULL, $wmName = '', $wmAlign = 0, $wmAlignX = 0, $wmAlignY = 0, $wmSelect = 0, $exifRotate = true, $dissolve = 30) {
1335c1335
<             $wmName, '', $wmAlign, $wmAlignX, $wmAlignY, 0, 0, $wmSelect);
---
>             $wmName, '', $wmAlign, $wmAlignX, $wmAlignY, 0, 0, $wmSelect, $dissolve);
1839c1839
<     function watermarkPhoto($index, $wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $preview=0, $previewSize=0, $wmSelect=0) {
---
>     function watermarkPhoto($index, $wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $preview=0, $previewSize=0, $wmSelect=0, $dissolve = 0) {
1851c1851,1852
<             $wmSelect
---
>             $wmSelect,
>           $dissolve
1860c1861
<     function watermarkAlbum($wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $recursive=0, $wmSelect=0) {
---
>     function watermarkAlbum($wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $recursive=0, $wmSelect=0, $dissolve = 0) {
1871c1872
<                     $wmAlign, $wmAlignX, $wmAlignY, $recursive, $wmSelect);
---
>                     $wmAlign, $wmAlignX, $wmAlignY, $recursive, $wmSelect, $dissolve);
1880c1881
<                 $wmSelect);
---
>                 $wmSelect, $dissolve);


diff -r gallery/classes/AlbumItem.php ../gallery/classes/AlbumItem.php
517c517
<     function watermark($dir, $wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $preview=0, $previewSize=0, $wmSelect=0) {
---
>     function watermark($dir, $wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $preview=0, $previewSize=0, $wmSelect=0, $dissolve=0) {
549c549
<               $wmAlign, $wmAlignX, $wmAlignY
---
>               $wmAlign, $wmAlignX, $wmAlignY, $dissolve
567c567
<               $wmAlign, $wmAlignX, $wmAlignY
---
>               $wmAlign, $wmAlignX, $wmAlignY, $dissolve
586c586
<                       $wmAlignY
---
>                       $wmAlignY, $dissolve


diff -r gallery/edit_watermark.php ../gallery/edit_watermark.php
38,39c38,39
< list($wmName, $wmAlign, $wmAlignX, $wmAlignY, $wmSelect) = 
<       getRequestVar(array('wmName', 'wmAlign', 'wmAlignX', 'wmAlignY', 'wmSelect'));
---
> list($wmName, $wmAlign, $wmAlignX, $wmAlignY, $wmSelect, $dissolve) =
>       getRequestVar(array('wmName', 'wmAlign', 'wmAlignX', 'wmAlignY', 'wmSelect', 'dissolve'));
63c63,64
<                   isset($wmSelect) ? $wmSelect : 0
---
>                   isset($wmSelect) ? $wmSelect : 0,
>                 isset($dissolve) ? $dissolve : 0
77c78,80
<                   isset($previewFull) ? $previewFull : 0
---
>                   isset($previewFull) ? $previewFull : 0,
>                 '',
>                 ($dissolve > 0) ? $dissolve : 0


diff -r gallery/layout/watermarkform.inc ../gallery/layout/watermarkform.inc
33a34
> global $dissolve;
54a56,58
> if (!isset($dissolve)) {
>     $dissolve = 0;
> }
145a150,152
> <tr>
>   <td colspan=3>Dissolve %<input type="text" name="dissolve" value="<?php if ($dissolve>0) echo $dissolve; else echo 0; ?>" size=4 maxlength=3></td>
> </tr>


diff -r gallery/lib/imageManipulation.php ../gallery/lib/imageManipulation.php
177c177
< function watermark_image($src, $dest, $wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY) {
---
> function watermark_image($src, $dest, $wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $dissolve) {
316c316
<             $srcOperator = "-geometry +$wmAlignX+$wmAlignY $overlayFile";
---
>             $srcOperator = "-geometry +$wmAlignX+$wmAlignY -dissolve $dissolve $overlayFile";


diff -r gallery/watermark_album.php ../gallery/watermark_album.php
36,37c36,37
< list($index, $save, $preview, $wmAlign, $wmName, $wmSelect) =
<     getRequestVar(array('index', 'save', 'preview', 'wmAlign', 'wmName', 'wmSelect'));
---
> list($index, $save, $preview, $wmAlign, $wmName, $wmSelect, $dissolve) =
>     getRequestVar(array('index', 'save', 'preview', 'wmAlign', 'wmName', 'wmSelect', 'dissolve'));
77c77,78
<                     $wmSelect
---
>                     $wmSelect,
>                   $dissolve
97,98c98,102
<                     isset($previewFull) ? $previewFull : 0);
<             }
---
>                     isset($previewFull) ? $previewFull : 0,
>                     '',
>                   ($dissolve > 0) ? $dissolve : 0
>               );
>           }

This was a quick/simple hack, and it works, but Im not guaranteeing this will not open you up to some mailicious buffer overflow in the ImageMagick -dissolve option (should probably check the value the form comes back with).

 
Tim_j
Tim_j's picture

Joined: 2002-08-15
Posts: 6818
Posted: Mon, 2006-10-02 22:37

Can you make a unified diff please ?

Also a good idea is to upload this into the patch tracker.

Assign it to me (JensT)

Jens
--
Last Gallery v1 Developer and v1 translation manager.

 
Tmack

Joined: 2005-06-01
Posts: 5
Posted: Tue, 2006-10-03 21:20

done:

diff -ru gallery/classes/Album.php ../gallery/classes/Album.php
--- gallery/classes/Album.php   2006-05-02 05:59:13.000000000 -0400
+++ ../gallery/classes/Album.php        2006-09-21 02:07:15.000000000 -0400
@@ -1152,7 +1152,7 @@
         }
     }
 
-    function addPhoto($file, $tag, $originalFilename, $caption, $pathToThumb = '', $extraFields = array(), $owner = '', $votes = NULL, $wmName = '', $wmAlign = 0, $wmAlignX = 0, $wmAlignY = 0, $wmSelect = 0, $exifRotate = true) {
+    function addPhoto($file, $tag, $originalFilename, $caption, $pathToThumb = '', $extraFields = array(), $owner = '', $votes = NULL, $wmName = '', $wmAlign = 0, $wmAlignX = 0, $wmAlignY = 0, $wmSelect = 0, $exifRotate = true, $dissolve = 30) {
         global $gallery;
 
         $this->updateSerial = 1;
@@ -1332,7 +1332,7 @@
         if (isImage($tag) && strlen($wmName)) {
             processingMsg("- ". _("Watermarking Image"));
             $photo->watermark($this->getAlbumDir(),
-            $wmName, '', $wmAlign, $wmAlignX, $wmAlignY, 0, 0, $wmSelect);
+            $wmName, '', $wmAlign, $wmAlignX, $wmAlignY, 0, 0, $wmSelect, $dissolve);
         }
 
         $this->fields['guid'] = genGUID();
@@ -1836,7 +1836,7 @@
         }
     }
 
-    function watermarkPhoto($index, $wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $preview=0, $previewSize=0, $wmSelect=0) {
+    function watermarkPhoto($index, $wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $preview=0, $previewSize=0, $wmSelect=0, $dissolve = 0) {
         $this->updateSerial = 1;
         $photo = &$this->getPhoto($index);
         $retval = $photo->watermark(
@@ -1848,7 +1848,8 @@
             $wmAlignY,
             $preview,
             $previewSize,
-            $wmSelect
+            $wmSelect,
+           $dissolve
         );
         if (!$retval) {
             return $retval;
@@ -1857,7 +1858,7 @@
         $this->save(array(), $resetModDate);
     }
 
-    function watermarkAlbum($wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $recursive=0, $wmSelect=0) {
+    function watermarkAlbum($wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $recursive=0, $wmSelect=0, $dissolve = 0) {
         $this->updateSerial = 1;
         $count = $this->numPhotos(1);
         for ($index = 1; $index <= $count; $index++) {
@@ -1868,7 +1869,7 @@
                     $subAlbum = new Album();
                     $subAlbum->load($subAlbumName);
                     $subAlbum->watermarkAlbum($wmName, $wmAlphaName,
-                    $wmAlign, $wmAlignX, $wmAlignY, $recursive, $wmSelect);
+                    $wmAlign, $wmAlignX, $wmAlignY, $recursive, $wmSelect, $dissolve);
                 }
             } else if ($photo->isMovie()) {
                 // Watermarking of movies not supported
@@ -1877,7 +1878,7 @@
                 $wmName, $wmAlphaName,
                 $wmAlign, $wmAlignX, $wmAlignY,
                 0, 0, // Not a preview
-                $wmSelect);
+                $wmSelect, $dissolve);
             }
         } // next $index
     } // end of function
diff -ru gallery/classes/AlbumItem.php ../gallery/classes/AlbumItem.php
--- gallery/classes/AlbumItem.php       2006-03-27 10:32:14.000000000 -0500
+++ ../gallery/classes/AlbumItem.php    2006-09-20 17:09:40.000000000 -0400
@@ -514,7 +514,7 @@
         return 1;
     }
 
-    function watermark($dir, $wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $preview=0, $previewSize=0, $wmSelect=0) {
+    function watermark($dir, $wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $preview=0, $previewSize=0, $wmSelect=0, $dissolve=0) {
         global $gallery;
         $type = $this->image->type;
         if (isMovie($type) || $this->isAlbum()) {
@@ -546,7 +546,7 @@
               "$dir/$name.$previewtag.$type",
               $gallery->app->watermarkDir."/$wmName",
               $gallery->app->watermarkDir."/$wmAlphaName",
-              $wmAlign, $wmAlignX, $wmAlignY
+              $wmAlign, $wmAlignX, $wmAlignY, $dissolve
             );
             if ($retval) {
                 list($w, $h) = getDimensions("$dir/$name.$previewtag.$type");
@@ -564,7 +564,7 @@
               "$dir/$name.$type",
               $gallery->app->watermarkDir."/$wmName",
               $gallery->app->watermarkDir."/$wmAlphaName",
-              $wmAlign, $wmAlignX, $wmAlignY
+              $wmAlign, $wmAlignX, $wmAlignY, $dissolve
             );
             }
             // 2=Only Full Photos
@@ -583,7 +583,7 @@
                       $gallery->app->watermarkDir."/$wmAlphaName",
                       $wmAlign,
                       $wmAlignX,
-                      $wmAlignY
+                      $wmAlignY, $dissolve
                     );
                 }
             }
Only in ../gallery/: config.php
diff -ru gallery/edit_watermark.php ../gallery/edit_watermark.php
--- gallery/edit_watermark.php  2006-04-04 17:58:25.000000000 -0400
+++ ../gallery/edit_watermark.php       2006-09-21 03:07:00.000000000 -0400
@@ -35,8 +35,8 @@
 
 list($index, $save, $preview, $previewFull) = 
        getRequestVar(array('index', 'save', 'preview', 'previewFull'));
-list($wmName, $wmAlign, $wmAlignX, $wmAlignY, $wmSelect) = 
-       getRequestVar(array('wmName', 'wmAlign', 'wmAlignX', 'wmAlignY', 'wmSelect'));
+list($wmName, $wmAlign, $wmAlignX, $wmAlignY, $wmSelect, $dissolve) =
+       getRequestVar(array('wmName', 'wmAlign', 'wmAlignX', 'wmAlignY', 'wmSelect', 'dissolve'));
 
 // Hack check
 if (! $gallery->user->canWriteToAlbum($gallery->album) &&
@@ -60,7 +60,8 @@
                   isset($wmAlignX) ? $wmAlignX : 0,
                   isset($wmAlignY) ? $wmAlignY : 0,
                   0, 0, // Not a preview
-                  isset($wmSelect) ? $wmSelect : 0
+                  isset($wmSelect) ? $wmSelect : 0,
+                 isset($dissolve) ? $dissolve : 0
                 );
                 dismissAndReload();
                 return;
@@ -74,7 +75,9 @@
                   isset($wmAlignX) ? $wmAlignX : 0,
                   isset($wmAlignY) ? $wmAlignY : 0,
                   1, // set as preview
-                  isset($previewFull) ? $previewFull : 0
+                  isset($previewFull) ? $previewFull : 0,
+                 '',
+                 ($dissolve > 0) ? $dissolve : 0
                 );
             }
         } else {
diff -ru gallery/layout/watermarkform.inc ../gallery/layout/watermarkform.inc
--- gallery/layout/watermarkform.inc    2006-04-04 17:58:25.000000000 -0400
+++ ../gallery/layout/watermarkform.inc 2006-09-21 01:58:40.000000000 -0400
@@ -31,6 +31,7 @@
 global $previewFull;
 global $wmName;
 global $wmSelect;
+global $dissolve;
 
 $err = "";
 if (!isset($wmAlignX) || ($wmAlignX <= 0)) {
@@ -52,6 +53,9 @@
 if (!isset($previewFull)) {
     $previewFull = 0;
 }
+if (!isset($dissolve)) {
+    $dissolve = 0;
+}
 
 if (!isset($wmSelect)) {
     $wmSelect = $gallery->app->watermarkSizes;
@@ -143,6 +147,9 @@
     Y=<input type="text" name="wmAlignY" value="<?php echo $wmAlignY ?>">
   </td>
 </tr>
+<tr>
+  <td colspan=3>Dissolve %<input type="text" name="dissolve" value="<?php if ($dissolve>0) echo $dissolve; else echo 0; ?>" size=4 maxlength=3></td>
+</tr>
 </table>
 </div>
 <br>
diff -ru gallery/lib/imageManipulation.php ../gallery/lib/imageManipulation.php
--- gallery/lib/imageManipulation.php   2006-05-02 05:59:13.000000000 -0400
+++ ../gallery/lib/imageManipulation.php        2006-09-21 01:03:20.000000000 -0400
@@ -174,7 +174,7 @@
     return array($overlay, $alpha);
 }
 
-function watermark_image($src, $dest, $wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY) {
+function watermark_image($src, $dest, $wmName, $wmAlphaName, $wmAlign, $wmAlignX, $wmAlignY, $dissolve) {
     global $gallery;
     if (!strcmp($src,$dest)) {
         $useTemp = true;
@@ -313,7 +313,7 @@
     // Execute
     switch($gallery->app->graphics) {
         case 'ImageMagick':
-            $srcOperator = "-geometry +$wmAlignX+$wmAlignY $overlayFile";
+            $srcOperator = "-geometry +$wmAlignX+$wmAlignY -dissolve $dissolve $overlayFile";
             exec_wrapper(ImCmd('composite', $srcOperator, $src, '', $out));
         break;
 
diff -ru gallery/watermark_album.php ../gallery/watermark_album.php
--- gallery/watermark_album.php 2006-03-27 10:32:14.000000000 -0500
+++ ../gallery/watermark_album.php      2006-09-21 03:14:23.000000000 -0400
@@ -33,8 +33,8 @@
  */
 require_once(dirname(__FILE__) . '/init.php');
 
-list($index, $save, $preview, $wmAlign, $wmName, $wmSelect) =
-    getRequestVar(array('index', 'save', 'preview', 'wmAlign', 'wmName', 'wmSelect'));
+list($index, $save, $preview, $wmAlign, $wmName, $wmSelect, $dissolve) =
+    getRequestVar(array('index', 'save', 'preview', 'wmAlign', 'wmName', 'wmSelect', 'dissolve'));
 list($wmAlignX, $wmAlignY, $recursive, $previewFull) =
     getRequestVar(array('wmAlignX', 'wmAlignY', 'recursive', 'previewFull'));
 
@@ -74,7 +74,8 @@
                     $wmAlignX,
                     $wmAlignY,
                     $recursive,
-                    $wmSelect
+                    $wmSelect,
+                   $dissolve
                 );
                 $gallery->album->save();
 ?>
@@ -94,8 +95,11 @@
                     isset($wmAlignX) ? $wmAlignX : 0,
                     isset($wmAlignY) ? $wmAlignY : 0,
                     1, // set as preview
-                    isset($previewFull) ? $previewFull : 0);
-            }
+                    isset($previewFull) ? $previewFull : 0,
+                    '',
+                   ($dissolve > 0) ? $dissolve : 0
+               );
+           }
         } else {
             $err = _("Please select a watermark.");
         }

Same diff, this time with -u, only changes reflected are those for this mod, diff is against stock downloaded 1.5.4. Does not have the NetPBM fix I suggested, but can be easily added, just edit line 321 of lib/imageManipulation.php (after applying the diff) and add -opacity=($dissolve/100) to the value of $args (still untested though). I will also upload to the tracker as you requested...