<?php
  /**
   * Copyright 2011 Zazavi - http://www.zazavi.com
   * Zazavi is free software released under the terms of the GNU General Public License.
   * You should have received a copy of the license.  If not, see <http://www.gnu.org/licenses/>.
   */

  define("_VALID_PHP", true);
  
  require_once("../../init.php");
  require_once(ZAZAVILITE . "admin/modules/gallery/admin_class.php");
  $gallery = new Gallery();
  
  $img = sanitize($_GET['image']);
  $folder = sanitize($_GET['folder']);
  
  $imagesource = ZAZAVILITE . $gallery->galpath . $folder.'/'.$img;

  if (!file_exists($imagesource))
      die();
  $filetype = substr($imagesource, strlen($imagesource) - 4, 4);
  if ($filetype == ".gif")
      $image = @imagecreatefromgif($imagesource);
  if ($filetype == ".jpg")
      $image = @imagecreatefromjpeg($imagesource);
  if ($filetype == ".png")
      $image = @imagecreatefrompng($imagesource);
  if (empty($image))
      die();
  $watermark = @imagecreatefrompng(UPLOADS.'watermark.png');
  $imagewidth = imagesx($image);
  $imageheight = imagesy($image);
  $watermarkwidth = imagesx($watermark);
  $watermarkheight = imagesy($watermark);
  /* For centerd watermark */
  //$startwidth = (($imagewidth - $watermarkwidth) / 2);
  //$startheight = (($imageheight - $watermarkheight) / 2);
  
  /* Bottom Right Corner  */
  $startwidth = (($imagewidth - $watermarkwidth));
  $startheight = (($imageheight - $watermarkheight));
  
  imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
  header("Content-type: image/jpeg");
  header('Content-Disposition: inline; filename=' . $imagesource);
  imagejpeg($image);
  imagedestroy($image);
  imagedestroy($watermark);
?>