[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/ -> AkImage.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package ActiveSupport
  13   * @subpackage ImageManipulation
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19   defined('AK_IMAGE_DRIVER') ? null : define('AK_IMAGE_DRIVER', 'GD');
  20  
  21   require_once(AK_VENDOR_DIR.DS.'pear'.DS.'Image'.DS.'Transform.php');
  22   require_once (AK_LIB_DIR.DS.'AkImage'.DS.'AkImageFilter.php');
  23  
  24   class AkImage extends Image_Transform
  25   {
  26       var $image_path;
  27       var $Transform;
  28       var $filters = array();
  29  
  30       function AkImage($image_path = null, $tranform_using = AK_IMAGE_DRIVER)
  31       {         
  32           $this->Transform =& Image_Transform::factory($tranform_using);
  33           
  34           if(PEAR::isError($this->Transform)){
  35               trigger_error($this->Transform->getMessage(), E_USER_ERROR);
  36           }
  37           if(!empty($image_path)){
  38               $this->load($image_path);
  39           }
  40       }
  41  
  42       function load($image_path)
  43       {
  44           $this->image_path = $image_path;
  45           $this->Transform->load($image_path);
  46       }
  47  
  48       function save($path = null, $quality = 90, $options = array())
  49       {
  50           if(!$tmp_image_name = tempnam(AK_TMP_DIR,'ak_image_')){
  51               trigger_error(Ak::t('Could not create the temporary file %tmp_image_name for apliying changes and saving', array('%tmp_image_name'=>$tmp_image_name)), E_USER_ERROR);
  52           }
  53           
  54           $path = empty($path) ? $this->image_path : $path;
  55           $this->Transform->save($tmp_image_name, $this->getExtension($path), $quality);         
  56           Ak::file_put_contents($path, file_get_contents($tmp_image_name), $options);
  57           @unlink($tmp_image_name);
  58       }
  59  
  60       function transform($transformation, $options = array())
  61       {
  62           $this->filters = array();
  63           $this->addFilter($transformation, $options);
  64           $this->applyFilters();
  65       }
  66  
  67       function getWidth()
  68       {
  69           return $this->Transform->getImageWidth();
  70       }
  71  
  72       function getHeight()
  73       {
  74           return $this->Transform->getImageHeight();
  75       }
  76  
  77       function getExtension($path = null)
  78       {
  79           return substr(strrchr(empty($path) ? $this->image_path : $path, '.'), 1);
  80       }
  81  
  82       function addFilter($filter_name, $options = array())
  83       {
  84           if($this->_filterExists($filter_name)){
  85               $class_name = $this->_getFilterClassName($filter_name);
  86               $filter =& new $class_name();
  87               if(method_exists($filter,'init')){
  88                   $filter->init();
  89               }
  90               $filter->setImage($this);
  91               $filter->setOptions($options);
  92               $this->filters[] =& $filter;
  93               return true;
  94           }
  95           return false;
  96       }
  97  
  98       function _filterExists($filter_name)
  99       {
 100           if(class_exists($filter_name)){
 101               return true;
 102           }
 103  
 104           $class_name = $this->_getFilterClassName($filter_name);
 105           $file_name = AK_LIB_DIR.DS.'AkImage'.DS.'AkImageFilter'.DS.$class_name.'.php';
 106           $success = true;
 107           if(!file_exists($file_name)){
 108               $success = false;
 109           }else{
 110               require_once($file_name);
 111           }
 112           $success = class_exists($class_name) ? $success : false;
 113  
 114           if(!$success){
 115               trigger_error(Ak::t('Could not find image filter %class_name at %file_name', array('%class_name'=>$class_name, '%file_name'=>$file_name)), E_USER_ERROR);
 116           }
 117           return $success;
 118       }
 119  
 120       function _getFilterClassName($filter_name)
 121       {
 122           // We might allow other classes to be created as filters in order to create image filter plugins from outside the framework
 123           if(!class_exists($filter_name)){
 124               return 'AkImage'.AkInflector::classify($filter_name).'Filter';
 125           }else{
 126               return $filter_name;
 127           }
 128       }
 129  
 130       function _getFilterChainPath($name, $path)
 131       {
 132           return (empty($path) ? AK_APP_DIR.DS.'image_filters' : rtrim($path,DS.'/')).DS.$name.'_filter.php';
 133       }
 134  
 135       function applyFilters()
 136       {
 137           foreach (array_keys($this->filters) as $k){
 138               $this->filters[$k]->apply();
 139           }
 140       }
 141  
 142       function saveFilterChain($name, $filter_chain = null, $filters_directory = null, $options = array())
 143       {
 144           $path = $this->_getFilterChainPath($name, $filters_directory);
 145           $filter_chain = empty($filter_chain) ? $this->getFilterChain() : $filter_chain;
 146           return Ak::file_put_contents($path, '<?php $filter_chain = '.var_export($filter_chain, true).'; ?>', $options);
 147       }
 148  
 149       function getFilterChain()
 150       {
 151           $filter_chain = array();
 152           foreach (array_keys($this->filters) as $k){
 153               $filter_chain[] = array('name'=>$this->filters[$k]->getName(),'options'=>$this->filters[$k]->getOptions());
 154           }
 155           return $filter_chain;
 156       }
 157  
 158       function setFilterChain($filter_chain)
 159       {
 160           $this->filters = array();
 161           $this->appendFilterChain($filter_chain);
 162       }
 163  
 164       function appendFilterChain($filter_chain)
 165       {
 166           foreach ($filter_chain as $filter){
 167               $this->addFilter($filter['name'],$filter['options']);
 168           }
 169       }
 170  
 171       function loadFilterChain($name, $filters_directory = null)
 172       {
 173           $path = $this->_getFilterChainPath($name, $filters_directory);
 174           $success = file_exists($path);
 175           @include($path);
 176           if(!$success || empty($filter_chain)){
 177               trigger_error(Ak::t('Could not find a valid %name filer chain at %path.', array('%name'=>$name,'%path'=>$path)), E_USER_ERROR);
 178           }
 179           $this->setFilterChain($filter_chain);
 180       }
 181  
 182       function applyFilterChain($name, $filters_directory = null)
 183       {
 184           $this->loadFilterChain($name, $filters_directory);
 185           $this->applyFilters();
 186       }
 187  
 188   }
 189  
 190  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6