Adding extra charsets for ActionMailer unit tests, if you're looking to parse incomin...
[akelos.git] / lib / AkImage.php
blobb68ebb24cc487287418a73ac749a1afeb83f8131
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
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 // +----------------------------------------------------------------------+
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>
19 defined('AK_IMAGE_DRIVER') ? null : define('AK_IMAGE_DRIVER', 'GD');
21 require_once(AK_VENDOR_DIR.DS.'pear'.DS.'Image'.DS.'Transform.php');
22 require_once(AK_LIB_DIR.DS.'AkImage'.DS.'AkImageFilter.php');
24 class AkImage extends Image_Transform
26 var $image_path;
27 var $Transform;
28 var $filters = array();
30 function AkImage($image_path = null, $tranform_using = AK_IMAGE_DRIVER)
32 $this->Transform =& Image_Transform::factory($tranform_using);
34 if(PEAR::isError($this->Transform)){
35 trigger_error($this->Transform->getMessage(), E_USER_ERROR);
37 if(!empty($image_path)){
38 $this->load($image_path);
42 function load($image_path)
44 $this->image_path = $image_path;
45 $this->Transform->load($image_path);
48 function save($path = null, $quality = 90, $options = array())
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);
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);
60 function transform($transformation, $options = array())
62 $this->filters = array();
63 $this->addFilter($transformation, $options);
64 $this->applyFilters();
67 function getWidth()
69 return $this->Transform->getImageWidth();
72 function getHeight()
74 return $this->Transform->getImageHeight();
77 function getExtension($path = null)
79 return substr(strrchr(empty($path) ? $this->image_path : $path, '.'), 1);
82 function addFilter($filter_name, $options = array())
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();
90 $filter->setImage($this);
91 $filter->setOptions($options);
92 $this->filters[] =& $filter;
93 return true;
95 return false;
98 function _filterExists($filter_name)
100 if(class_exists($filter_name)){
101 return true;
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);
112 $success = class_exists($class_name) ? $success : false;
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);
117 return $success;
120 function _getFilterClassName($filter_name)
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;
130 function _getFilterChainPath($name, $path)
132 return (empty($path) ? AK_APP_DIR.DS.'image_filters' : rtrim($path,DS.'/')).DS.$name.'_filter.php';
135 function applyFilters()
137 foreach (array_keys($this->filters) as $k){
138 $this->filters[$k]->apply();
142 function saveFilterChain($name, $filter_chain = null, $filters_directory = null, $options = array())
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);
149 function getFilterChain()
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());
155 return $filter_chain;
158 function setFilterChain($filter_chain)
160 $this->filters = array();
161 $this->appendFilterChain($filter_chain);
164 function appendFilterChain($filter_chain)
166 foreach ($filter_chain as $filter){
167 $this->addFilter($filter['name'],$filter['options']);
171 function loadFilterChain($name, $filters_directory = null)
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);
179 $this->setFilterChain($filter_chain);
182 function applyFilterChain($name, $filters_directory = null)
184 $this->loadFilterChain($name, $filters_directory);
185 $this->applyFilters();