Watermark Image on upload

Watermark Image on upload

Want to place a watermark on your image when you upload it to your server with Interakt File Upload SB
divider

Want to place a watermark on your image when you upload it to your server?

Here is a solution using Interakt File Upload SB

It is done by taking some of the code from tNG_DynamicThumbnail and putting it into tNG_ImageUpload.

One thing I noticed is that if you use the GD image library ... it is really grainy and pure black is being replaced by white on small thumbnails. This is easily sorted by changing to the ImageMagick library.

Code to be used to add the watermark on upload.

//start Trigger_ImageUpload trigger
//remove this line if you want to edit the code by hand
function Trigger_ImageUpload(&$tNG) {
      $uploadObj = new tNG_ImageUpload($tNG);
      $uploadObj->setFormFieldName("Filedata");
      $uploadObj->setDbFieldName("photo_url_pho");
      $uploadObj->setFolder("../../images/screenplay/images/{getAlbumDetails.id_usr_alb}/{getAlbumDetails.id_alb}/");
      $uploadObj->setResize("true", 600, 600);
      $uploadObj->setMaxSize(1024);
      $uploadObj->setAllowedExtensions("gif, jpg, jpe, jpeg, png");
      $uploadObj->setRename("custom");
      $uploadObj->setRenameRule("{id_alb_pho}_{id_pho}.{KT_ext}");

      $uploadObj->setWatermark(true);
      $uploadObj->setWatermarkAlpha(50);
      $uploadObj->setWatermarkImage("../../resources/images/misc/watermark.gif");
      $uploadObj->setWatermarkAlignment("bottom", "center");
      $uploadObj->setWatermarkResize("none");

      return $uploadObj->Execute();
}
//end Trigger_ImageUpload trigger

Below you will find the full tNG_ImageUpload.Class.php file (remember to back up the old version)...

<?php
/*
      Copyright (c) InterAKT Online 2000-2006. All rights reserved.
      Edited by Zinc Digital Ltd (Gareth Shaw) to allow watermarks to be
      applied to images on upload
*/

/**
*      Provides functionalities for handling tNG based file uploads.
*      Extends tNG_FileUpload class;
* @access public
*/
class tNG_ImageUpload extends tNG_FileUpload{
      /**
            * if the image will be resized
            * @var boolena
            * @access public
            */
      var $resize;
      /**
            * If the proportions must be kept in case of a resize
            * @var boolean
            * @access public
            */
      var $resizeProportional;
      /**
            * width for resize
            * @var integer
            * @access public
            */
      var $resizeWidth;
      /**
            * height for resize
            * @var integer
            * @access public
            */
      var $resizeHeight;
      /**
            * relpath to siteroot
            * @var string
            * @access public
            */
      var $relpath;
      /**
            * folder
            * @var string
            * @access public
            */
      var $folder;
      /**
            * file name
            * @var string
            * @access public
            */
      var $renameRule;
      /**
            * width to resize
            * @var int
            * @access public
            */
      var $width;
      /**
            * height to resize
            * @var int
            * @access public
            */
      var $height;
      /**
            * to keep the proportions or not
            * @var boolean
            * @access public
            */
      var $keepProportion;
      /**
            * path to watermarg image
            * @var string
            * @access public
            */
      var $watermarkImage;
      /**
            * id unique for thumbnail
            * @var string
            * @access public
            */
      var $id;
      /**
            * popup width
            * @var int
            * @access public
            */
      var $popupWidth;
      /**
            * popup height
            * @var int
            * @access public
            */
      var $popupHeight;
      /**
            * if the popup page has navigation
            * @var boolean
            * @access public
            */
      var $popupNavigation;
      /**
            * flag if common properties were setted or not;
            * @var boolean
            * @access public
            */
      var $isSetted;
      /**
            * flag if common properties were setted or not;
            * @var boolean
            * @access public
            */
      var $fitToWindow;
      /**
            * apply watermark on thumbnail;
            * @var boolean
            * @access public
            */
      var $watermark;
      /**
            * watermark alpha 0-100;
            * @var integer
            * @access public
            */
      var $watermarkAlpha;
      /**
            * alignment for watermark;
            * @var array
            * @access public
            */
      var $watermarkAlignment;
      /**
            * resiza mode for watermark;
            * @var array
            * @access public
            */
      var $watermarkResize;
      /**
            * apply watermark on popupimage;
            * @var boolean
            * @access public
            */
      var $popupWatermark;
      /**
            * current working folder (folder in which the calling script resides);
            * @var string
            * @access public
            */
      var $currentfolder;
      /**
            * Constructor. Sets the relative path to siteroot
            * @param string
            * @access public
            */

      /**
            * Constructor. Sets the reference to transaction. initialize some vars;
            * @param object tNG
            * @access public
            */
      function tNG_ImageUpload(&$tNG) {
                  parent::tNG_FileUpload($tNG);
                  $this->resize = false;
                  $this->resizeProporti class="code">
                  $this->resizeWidth = 0;
                  $this->resizeHeight = 0;

                  $this->watermarkImage = '';
                  //$this->relpath = $relpath;
                  //$this->id = $id;
                  $this->folder = '';
                  $this->renameRule = '';
                  $this->popupNavigation = false;
                  $this->isSetted = false;
                  $this->fitToWindow = false;
                  $this->watermark = false;
                  $this->popupWatermark = false;
                  $this->watermarkAlpha = 20;
                  $this->watermarkResize = array();
                  $this->watermarkAlignment = array();
                  $this->currentfolder = KT_TransformToUrlPath(getcwd());
      }
      /**
           * Setter. Apply watermark on thumbnail;
           * @param boolean
           * @access public
           */
      function setWatermark($watermark) {
                    $this->watermark = $watermark;
     }
     /**
          * Setter. Sets watermark alpha;
          * @param integer
          * @access public
          */
     function setWatermarkAlpha($watermarkAlpha) {
               $this->watermarkAlpha = $watermarkAlpha;
     }
     /**
          * Setter. Sets alignments for watermark;
          * @param string horizontal
          * @param string vertical
          * @access public
          */
     function setWatermarkAlignment($vertical, $horizontal) {
               $this->watermarkAlignment['vertical'] = strtolower($vertical);
               $this->watermarkAlignment['horizontal'] = strtolower($horizontal);
     }
     /**
          * Setter. Sets watermark resize mode;
          * @param string none|stretch|resize
          * @param string width for resize
          * @param string height for resize
          * @access public
          */
     function setWatermarkResize($watermarkResize) {
               $this->watermarkResize['mode'] = strtolower($watermarkResize);
               $this->watermarkResize['width'] = 0;
               $this->watermarkResize['height'] = 0;
     }
     /**
          * Setter. Sets watermark resize mode;
          * @param string none|stretch|resize
          * @param string width for resize
          * @param string height for resize
          * @access public
          */
     function setWatermarkSize($width, $height) {
               $this->watermarkResize['width'] = (int)$width;
               $this->watermarkResize['height'] = (int)$height;
     }
     /**
          * Setter. Sets the watermark image to used;
          * @param string
          * @access public
          */
     function setWatermarkImage($watermarkImage) {
               $this->watermarkImage = $watermarkImage;
     }
     /**
          * setter. set the sizes for the resize and proportional resize flag;
          * @var boolean proportional make the resize proportional
          * @var integer width of the resize
          * @var integer height
          * @access public
          */
     function setResize($proportional, $width, $height) {
               $this->resize = true;
               $this->resizeProporti class="code">
               $this->resizeWidth = (int)$width;
               $this->resizeHeight = (int)$height;
     }
     /**
          * in case of an update, the old thumbnail are deleted;
          * @var string the name of the folder
          * @var string the old name of the file
          * @access public
          */
     function deleteThumbnails($folder, $oldName) {
               tNG_deleteThumbnails($folder, $oldName, '');
     }
     /**
          * the main method, execute the code of the class;
          * Upload the file, set the file name in transaction;
          * return mix null or error object
          * @access public
          */
     function Execute() {
               $ret = parent::Execute();
               if ($ret === null && $this->resize && $this->uploadedFileName != '') {
                    $ret = $this->Resize();
               }
               return $ret;
     }
     /**
          * Make the resize on the saved file;
          * return mix null or error object
          * @access public
          */
     function Resize() {
                    $ret = NULL;
                    $image = new KT_image();
                    $image->setPreferedLib($GLOBALS['tNG_prefered_image_lib']);
                    $image->addCommand($GLOBALS['tNG_prefered_imagemagick_path']);
                    if ($this->watermark) {
                         // watermark the image
                         //$imageObj->thumbnail($fullFileName, $thumbnailFolder, $thumbnailName, (int)$width, (int)$height, $proportional);
                         //$image->thumbnail($this->dynamicFolder.$this->uploadedFileName, $this->dynamicFolder, $this->uploadedFileName, 9999, 9999, $this->resizeProportional);
                         $image->resize($this->dynamicFolder.$this->uploadedFileName, $this->dynamicFolder, $this->uploadedFileName, $this->resizeWidth, $this->resizeHeight, $this->resizeProportional);
                    if (!$image->hasError()) {
                         $image = new KT_image();
                         $image->setPreferedLib($GLOBALS['tNG_prefered_image_lib']);
                         $image->addCommand($GLOBALS['tNG_prefered_imagemagick_path']);
                         $image->watermark($this->dynamicFolder.$this->uploadedFileName, $this->dynamicFolder.$this->uploadedFileName, KT_realpath($this->watermarkImage, false), $this->watermarkAlpha, $this->watermarkResize, $this->watermarkAlignment);

                         if ($imageObj->hasError()) {
                              @unlink($this->dynamicFolder.$this->uploadedFileName);
                              $arrError = $imageObj->getError();
                              $errObj = new tNG_error('IMG_WATERMARK', array(), array($arrError[1]));
                         return $ret;
                         }
                    }
               } else {
                         // don;t watermark the image
                         $image->resize($this->dynamicFolder.$this->uploadedFileName, $this->dynamicFolder, $this->uploadedFileName, $this->resizeWidth, $this->resizeHeight, $this->resizeProportional);
                    }

                    if ($image->hasError()) {
                         $arrError = $image->getError();
                         $errObj = new tNG_error('IMG_RESIZE', array(), array($arrError[1]));
                         if ($this->dbFieldName != '') {
                              $errObj->addFieldError($this->dbFieldName, 'IMG_RESIZE', array());
                         }
                              $ret = $errObj;
                         }

                    return $ret;
     }
}
?>
Written by:  - 4 Aug, 2010