/home/coolpkct/www/websites/cake3.cool.rocks/admin/classes/image.php
<?php
/**
 * Part of Showkase web site management package
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright (c) 2012, SimpleViewer Inc.
 */
defined('SK_ACCESS')||die('<h1>403: Forbidden</h1>');
 
/**
 * Image
 *
 * @package Showkase
 */
abstract class Image
{
    /**
     * @var object Page
     */
    protected $page;
    /**
     * @var string Url relative to gallery index file
     */
    protected $imageUrl = '';
    
    /**
     * @var string short image title, not supported by all galleries
     */
    protected $imageTitle = '';
    
    /**
     * @var string caption or short title for the image 
     */
    protected $imageCaption = '';
    
    /**
     * @var string 
     */
    protected $thumbUrl = '';
    
    /**
     * @var string 
     */
    protected $imageLinkUrl = '';
    
    /**
     * @var string 
     */
    protected $imageLinkTarget = IMAGE_LINK_DEFAULT_TARGET;
    
    /**
     * @var string gallery path rel svm
     */
    protected $galleryPathRelSvm = '';
    
    /**
     * @var string image path relative to gallery
     */
    protected $imagePathRelGallery = '';
    
    /**
     * @var string thumb path relative to gallery
     */
    protected $thumbPathRelGallery = '';
    
    /**
     * @var array image size from php getimagesize function
     */
    protected $imageSize = array();
    
    /**
     * @var array image data from second parameter of php getimagesize function
     */
    protected $imageInfo = array();
    
    /**
     * @var boolean flag image for deletion. Used to separate sort and delete.
     */
    protected $imageDeleted = false;
  
    /**
     * constructor
     *
     * @param object Page
     * @param string  gallery path rel svm
     * @param string image path rel gallery or null if not known
     * @param object xml image element
     */
    public function __construct(Page $page, $galleryPathRelSvm, $imagePathRelGallery, $imageData=null)
    {
        $this->page = $page;
        $this->galleryPathRelSvm = $galleryPathRelSvm;
        if (is_null($imageData)) {
            $this->imagePathRelGallery = $imagePathRelGallery;
            $this->imageUrl = str_replace(array('\\', '/'), '/', $this->imagePathRelGallery);
        }
        else {
            $this->imageUrl = $imageData['imageUrl'];
            if ($this->imageUrl == '') {
                throw new Exception('missing imageUrl attribute');
            }
            $this->imagePathRelGallery = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $this->imageUrl);
            $this->thumbUrl = isset($imageData['thumbUrl']) ? $imageData['thumbUrl'] : '';
            $this->imageTitle = isset($imageData['imageTitle']) ? $imageData['imageTitle'] : '';
            $this->imageCaption = isset($imageData['imageCaption']) ? $imageData['imageCaption'] : '';
            $this->imageLinkUrl = isset($imageData['imageLinkUrl']) ? $imageData['imageLinkUrl'] : '';
            if (!empty($imageData['imageLinkTarget'])) $this->imageLinkTarget = $imageData['imageLinkTarget'];
        }
        $this->imageSize = getimagesize(
            $this->galleryPathRelSvm.DIRECTORY_SEPARATOR.$this->imagePathRelGallery,
            $this->imageInfo
        );
        if ($this->imageSize === false) {
            throw new Exception('cannot get image size for '.$this->imageUrl);
        }
        $this->getIptc(); 
    }
  
    /**
    * Magic getter to make things easy for themers
    *
    * @return mixed
    * @param string name
    */
    public function __get($name) 
    {
      $name = strtolower($name);
      switch ($name) {
          case ('imageurl') :
              // relative url
              return $this->imageUrl;
          case ('imageabsurl') :
              // absolute url
              return $this->page->pageUrl.'/'.$this->imageUrl;
          case ('thumburl') :
              return $this->thumbUrl;
          case ('width') :
              return $this->getWidth();
          case ('height') :
              return $this->getHeight();
          case ('format') :
              return $this->getFormat();
          case ('title') :
              return $this->imageTitle;
          case ('caption') :
              return $this->imageCaption;
          case ('linkurl') :
              return $this->imageLinkUrl;
          case ('linktarget') :
              return $this->imageLinkTarget;
          default :
              return '';
      }
    }

    /**
     * Return image data in gallery exchange format
     *
     * @return array
     */
    public function getImageData()
    {
        return array(
            'imageTitle'=>$this->imageTitle,
            'imageCaption'=>$this->imageCaption,
            'imageUrl'=>$this->imageUrl,
            'thumbUrl'=>$this->thumbUrl,
            'imageLinkUrl'=>$this->imageLinkUrl,
            'imageLinkTarget'=>$this->imageLinkTarget
        );
    }

    /**
     * format image attributes replacing {variables}
     *
     * @param string title, caption etc
     * @param integer key to image objects array
     * @return string
     */
    protected function formatImageAttributes($att, $count)
    {
        $iptc = $this->getIptc();
        $att = str_replace('%',             '%%',   $att);
        $att = str_replace('%%1$',          '%1$',  $att);
        $att = str_replace('%%2$',          '%2$',  $att);
        $att = str_replace('{count}',       '%1$s', $att);
        $att = str_replace('{file}',        '%2$s', $att);
        $att = str_replace('{iptctitle}',   '%3$s', $att);
        $att = str_replace('{iptcdescription}', '%4$s', $att);
        return sprintf($att, $count+1, $this->getCleanImageFileName(), $iptc['title'], $iptc['description']);
    }
     
    /**
     * get url relative to document root
     *
     * @return string
     */
    public function getRelativeUrl()
    {
        return $this->imageUrl;
    }
    
    /**
     * set image path relative to gallery
     *
     * @access public
     * @return void
     * @param string image path
     */
    public function setImagePathRelGallery($path)
    {
        $this->imagePathRelGallery = $path;
    }
    
    /**
     * get image path relative to gallery
     *
     * @access public
     * @return string image path
     */
    public function getImagePathRelGallery()
    {
        return $this->imagePathRelGallery;
    }
    
    /**
     * get file name
     *
     * @access public
     * @return string file name
     */
    public function getImageFileName()
    {
        return basename($this->imagePathRelGallery);
    }
    
    /**
     * get file name with no suffix
     *
     * @access public
     * @return string file name
     */
    public function getCleanImageFileName()
    {
        $info = pathinfo($this->imagePathRelGallery);
        return basename($this->imagePathRelGallery,'.'.$info['extension']);
    }
    
    /**
     * get image size
     *
     * @access public
     * @return array
     */
    public function getImageSizeArray()
    {
        return $this->imageSize;
    }
    
    /**
     * get width
     *
     * @return integer
     */
    public function getWidth()
    {
        return $this->imageSize[0];
    }
    
    /**
     * get height
     *
     * @return integer
     */
    public function getHeight()
    {
        return $this->imageSize[1];
    }
    
    /**
     * get IPTC data
     *
     * @return array
     */
    public function getIptc()
    {
        if (!isset($this->imageInfo['APP13'])) {
            return array(
                'title'=>'',
                'description'=>''
            );
        }
        $iptc = iptcparse($this->imageInfo['APP13']);
        $title   = isset($iptc['2#005']) ? $iptc['2#005'][0] : '';
        $description = isset($iptc['2#120']) ? $iptc['2#120'][0] : '';
        return array (
            'title'=>$title,
            'description'=>$description
        );
    }
    
    /**
     * Get format height/width as percent
     *
     * @return float
     */
    public function getFormat()
    {
        if ($this->imageSize[0] == 0) return 100;
        return 100*$this->imageSize[1]/$this->imageSize[0];
    }
    
    /**
     * get caption
     *
     * @access public
     * @return string file name
     */
    public function getCaption()
    {
        return $this->imageCaption;
    }
    
    /**
     * get image title
     *
     * @access public
     * @return string description
     */
    public function getTitle()
    {
        return $this->imageTitle;
    }
    
    /**
     * Get image link url
     *
     * @return string
     */
    public function getImageLinkUrl()
    {
        return $this->imageLinkUrl;
    }
    
    /**
     * Get image link target
     *
     * @return string
     */
    public function getImageLinkTarget()
    {
        return $this->imageLinkTarget;
    }
    
    /**
     * Get image flagged for deletion
     *
     * @return boolean
     */
     public function isDeleted()
     {
        return $this->imageDeleted;
     }
     
     /**
      * Flag image for deletion
      *
      * @return void
      * @param boolean
      */
     public function setImageDeleted($deleted)
     {
        $this->imageDeleted = $deleted;
     }
  
}