00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Media_Model_Image extends Mage_Core_Model_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_config;
00043
00044
00045
00046
00047
00048
00049 protected $_image;
00050
00051
00052
00053
00054
00055
00056 protected $_tmpImage;
00057
00058
00059
00060
00061
00062
00063 protected $_params = array();
00064
00065
00066 protected function _construct()
00067 {
00068 $this->_init('media/image');
00069 }
00070
00071
00072
00073
00074
00075
00076
00077 public function setConfig(Mage_Media_Model_Image_Config_Interface $config)
00078 {
00079 $this->_config = $config;
00080 return $this;
00081 }
00082
00083
00084
00085
00086
00087
00088 public function getConfig()
00089 {
00090 return $this->_config;
00091 }
00092
00093 public function getImage()
00094 {
00095 if(is_null($this->_image)) {
00096 $this->_image = $this->_getResource()->getImage($this);
00097 }
00098
00099 return $this->_image;
00100 }
00101
00102 public function getTmpImage()
00103 {
00104 if(is_null($this->_image)) {
00105 $this->_tmpImage = $this->_getResource()->getTmpImage($this);
00106 }
00107
00108 return $this->_tmpImage;
00109 }
00110
00111
00112
00113
00114
00115
00116 public function getDimensions()
00117 {
00118 if(!$this->getData('dimensions')) {
00119 $this->setData('dimensions', $this->_getResource()->getDimensions($this));
00120 }
00121 return $this->getData('dimensions');
00122 }
00123
00124
00125
00126
00127
00128
00129 public function getDestanationDimensions()
00130 {
00131 if(!$this->getData('destanation_dimensions')) {
00132 $this->setData('destanation_dimensions', clone $this->getDimensions());
00133 }
00134
00135 return $this->getData('destanation_dimensions');
00136 }
00137
00138 public function getExtension()
00139 {
00140 return substr($this->getFileName(), strrpos($this->getFileName(), '.')+1);
00141 }
00142
00143 public function getFilePath($useParams=false)
00144 {
00145 if($useParams && sizeof($this->getParams())) {
00146 $changes = '.' . $this->getParamsSum();
00147 } else {
00148 $changes = '';
00149 }
00150
00151 return $this->getConfig()->getBaseMediaPath() . DS . $this->getName() . $changes . '.'
00152 . ( ( $useParams && $this->getParam('extension')) ? $this->getParam('extension') : $this->getExtension() );
00153 }
00154
00155 public function getFileUrl($useParams=false)
00156 {
00157 if($useParams && sizeof($this->getParams())) {
00158 $changes = '.' . $this->getParamsSum();
00159 } else {
00160 $changes = '';
00161 }
00162
00163 return $this->getConfig()->getBaseMediaUrl() . '/' . $this->getName() . $changes . '.'
00164 . ( ( $useParams && $this->getParam('extension')) ? $this->getParam('extension') : $this->getExtension() );
00165 }
00166
00167 public function getName()
00168 {
00169 return substr($this->getFileName(), 0, strrpos($this->getFileName(), '.'));
00170 }
00171
00172 public function addParam($param, $value=null)
00173 {
00174 if(is_array($param)) {
00175 $this->_params = array_merge($this->_params, $param);
00176 } else {
00177 $this->_params[$param] = $value;
00178 }
00179
00180 return $this;
00181 }
00182
00183 public function setParam($param, $value=null)
00184 {
00185 if(is_array($param)) {
00186 $this->_params = $param;
00187 } else {
00188 $this->_params[$param] = $value;
00189 }
00190
00191 return $this;
00192 }
00193
00194 public function getParam($param)
00195 {
00196 if(isset($this->_params[$param])) {
00197 return $this->_params[$param];
00198 }
00199
00200 return null;
00201 }
00202
00203 public function getParams()
00204 {
00205 return $this->_params;
00206 }
00207
00208 public function getParamsSum()
00209 {
00210 return md5(serialize($this->_params));
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 public function getSpecialLink($file, $size, $extension=null, $watermark=null)
00223 {
00224 $this->_removeResources();
00225 $this->setData(array());
00226 $this->setParam(array());
00227 $this->setFileName($file);
00228
00229 $this->addParam('size', $size);
00230 $this->addParam('watermark', $watermark);
00231 $this->addParam('extension', $extension);
00232
00233 if(!$this->hasSpecialImage()) {
00234 if (strpos($size, 'x')!==false) {
00235 list($width, $height) = explode('x', $size);
00236 } else {
00237 $width = $size;
00238 $height = $this->getDimensions()->getHeight();
00239 }
00240
00241 $sizeHRate = $width / $this->getDimensions()->getWidth();
00242 $sizeVRate = $height / $this->getDimensions()->getHeight();
00243
00244 $rate = min($sizeHRate, $sizeVRate);
00245
00246 if ($rate > 1) {
00247 $rate = 1;
00248 }
00249
00250 $this->getDestanationDimensions()
00251 ->setWidth($rate*$this->getDimensions()->getWidth())
00252 ->setHeight($rate*$this->getDimensions()->getHeight());
00253
00254
00255 $this->_getResource()->resize($this);
00256 $this->_getResource()->watermark($this);
00257 $this->_getResource()->saveAs($this, $extension);
00258 $this->_removeResources();
00259 }
00260
00261 return $this->getFileUrl(true);
00262 }
00263
00264 public function hasSpecialImage()
00265 {
00266 return $this->_getResource()->hasSpecialImage($this);
00267 }
00268
00269 protected function _removeResources()
00270 {
00271 if ($this->_image) {
00272 $this->_getResource()->destroyResource($this->_image);
00273 $this->_image = null;
00274 }
00275
00276 if ($this->_tmpImage) {
00277 $this->_getResource()->destroyResource($this->_tmpImage);
00278 $this->_tmpImage = null;
00279 }
00280 }
00281
00282 }