Page 1 of 1

Watermark Image on upload with ADDT (detailed version)

Posted: 2012-12-25 19:41
by mbunal
i edited the file tNG_ImageUpload.class.php. so now i can watermark image on upload.

image upload trigger:

Code: Select all

    //start Trigger_ImageUpload trigger
    function Trigger_ImageUpload(&$tNG) {
      $uploadObj = new tNG_ImageUpload($tNG);
      $uploadObj->setFormFieldName("imgnm");
      $uploadObj->setDbFieldName("images");
      $uploadObj->setFolder("../images/urunler/");
      $uploadObj->setMaxSize(5120);
      $uploadObj->setAllowedExtensions("gif, jpg, jpe, jpeg, png");
      $uploadObj->setRename("custom");
      $uploadObj->setRenameRule("{proid}_{imgid}.{KT_ext}");
//////////////////////////////////////////////////////////////////////////// watermark begin
    $uploadObj->setWatermark(true);
    $uploadObj->setWatermarkAlpha(50);
    $uploadObj->setWatermarkImage("../images/watermark.gif");
    $uploadObj->setWatermarkAlignment("center", "center");
    $uploadObj->setWatermarkResize("none");
//////////////////////////////////////////////////////////////////////////// watermark end
      return $uploadObj->Execute();
    }
    //end Trigger_ImageUpload trigger

tNG_ImageUpload.class.php:

Code: Select all

    <?php
    /*
     * ADOBE SYSTEMS INCORPORATED
     * Copyright 2007 Adobe Systems Incorporated
     * All Rights Reserved
     *
     * NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the
     * terms of the Adobe license agreement accompanying it. If you have received this file from a
     * source other than Adobe, then your use, modification, or distribution of it requires the prior
     * written permission of Adobe.
     */

    /*
       Copyright (c) InterAKT Online 2000-2006. All rights reserved.
    */

    /**
    *   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;

//////////////////////////////////////////////////////////////////////////// watermark begin
       /**
        * 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
        */
//////////////////////////////////////////////////////////////////////////// watermark end
        
        /**
        * 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->resizeProportional = true;
          $this->resizeWidth = 0;
          $this->resizeHeight = 0;

//////////////////////////////////////////////////////////////////////////// watermark begin
          $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;
        }
//////////////////////////////////////////////////////////////////////////// watermark end

       /**
        * 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->resizeProportional = $proportional;
          $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
        */
//////////////////////////////////////////////////////////////////////////// changed execute function for watermark begin
       function Execute() {
          $ret = parent::Execute();
          if ($ret === null && $this->uploadedFileName != '') {
             if($this->resize) {
                $ret = $this->Resize();
             }
             if ($ret === null && $this->watermark) {
                $imageObj = new KT_image();
                $imageObj->setPreferedLib($GLOBALS['tNG_prefered_image_lib']);
                $imageObj->addCommand($GLOBALS['tNG_prefered_imagemagick_path']);
                $imageObj->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]));
                   if ($GLOBALS['tNG_debug_mode'] == 'DEVELOPMENT') {
                      $errMsg = $arrError[1];                        
                      $ret = $relpath . "includes/tng/styles/cannot_thumbnail.gif\" />".$errMsg."<img style=\"display:none\" src=\"".$relpath."includes/tng/styles/cannot_thumbnail.gif";
                   } else {
                      $ret = $relpath . "includes/tng/styles/cannot_thumbnail.gif";
                   }
                }
             }
          }
          return $ret;
       }
//////////////////////////////////////////////////////////////////////////// changed execute function for watermark end

/*/////////////////////////////////////////////////////////////////////////// orijinal execute function begin
	function Execute() {
		$ret = parent::Execute();
		if ($ret === null && $this->resize && $this->uploadedFileName != '') {
			$ret = $this->Resize();
		}
		return $ret;
	}
/*/////////////////////////////////////////////////////////////////////////// orijinal execute function end

       /**
        * 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']);
          $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;
       }
    }
    ?>