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 class Mage_Catalog_Helper_Image extends Mage_Core_Helper_Abstract
00033 {
00034 protected $_model;
00035 protected $_scheduleResize = false;
00036 protected $_scheduleWatermark = false;
00037 protected $_scheduleRotate = false;
00038 protected $_angle;
00039 protected $_watermark;
00040 protected $_watermarkPosition;
00041 protected $_watermarkSize;
00042 protected $_product;
00043 protected $_imageFile;
00044 protected $_placeholder;
00045
00046
00047
00048
00049 protected function _reset()
00050 {
00051 $this->_model = null;
00052 $this->_scheduleResize = false;
00053 $this->_scheduleWatermark = false;
00054 $this->_scheduleRotate = false;
00055 $this->_angle = null;
00056 $this->_watermark = null;
00057 $this->_watermarkPosition = null;
00058 $this->_watermarkSize = null;
00059 $this->_product = null;
00060 $this->_imageFile = null;
00061 return $this;
00062 }
00063
00064 public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
00065 {
00066 $this->_reset();
00067 $this->_setModel(Mage::getModel('catalog/product_image'));
00068 $this->_getModel()->setDestinationSubdir($attributeName);
00069 $this->setProduct($product);
00070 if ($imageFile) {
00071 $this->setImageFile($imageFile);
00072 }
00073 else {
00074
00075 $this->_getModel()->setBaseFile( $this->getProduct()->getData($this->_getModel()->getDestinationSubdir()) );
00076 }
00077 return $this;
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 public function resize($width, $height = null)
00090 {
00091 $this->_getModel()->setWidth($width)->setHeight($height);
00092 $this->_scheduleResize = true;
00093 return $this;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 public function keepAspectRatio($flag)
00107 {
00108 $this->_getModel()->setKeepAspectRatio($flag);
00109 return $this;
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 public function keepFrame($flag, $position = array('center', 'middle'))
00125 {
00126 $this->_getModel()->setKeepFrame($flag);
00127 return $this;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 public function keepTransparency($flag, $alphaOpacity = null)
00143 {
00144 $this->_getModel()->setKeepTransparency($flag);
00145 return $this;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 public function constrainOnly($flag)
00157 {
00158 $this->_getModel()->setConstrainOnly($flag);
00159 return $this;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 public function backgroundColor($colorRGB)
00173 {
00174
00175 if (!is_array($colorRGB)) {
00176 $colorRGB = func_get_args();
00177 }
00178 $this->_getModel()->setBackgroundColor($colorRGB);
00179 return $this;
00180 }
00181
00182 public function rotate($angle)
00183 {
00184 $this->setAngle($angle);
00185 $this->_getModel()->setAngle($angle);
00186 $this->_scheduleRotate = true;
00187 return $this;
00188 }
00189
00190 public function watermark($fileName, $position, $size=null)
00191 {
00192 $this->setWatermark($fileName)
00193 ->setWatermarkPosition($position)
00194 ->setWatermarkSize($size);
00195 $this->_scheduleWatermark = true;
00196 return $this;
00197 }
00198
00199 public function placeholder($fileName)
00200 {
00201 $this->_placeholder = $fileName;
00202 }
00203
00204 public function getPlaceholder()
00205 {
00206 if (!$this->_placeholder) {
00207 $attr = $this->_getModel()->getDestinationSubdir();
00208 $this->_placeholder = 'images/catalog/product/placeholder/'.$attr.'.jpg';
00209 }
00210 return $this->_placeholder;
00211 }
00212
00213 public function __toString()
00214 {
00215 try {
00216 if( $this->getImageFile() ) {
00217 $this->_getModel()->setBaseFile( $this->getImageFile() );
00218 } else {
00219 $this->_getModel()->setBaseFile( $this->getProduct()->getData($this->_getModel()->getDestinationSubdir()) );
00220 }
00221
00222 if( $this->_getModel()->isCached() ) {
00223 return $this->_getModel()->getUrl();
00224 } else {
00225 if( $this->_scheduleRotate ) {
00226 $this->_getModel()->rotate( $this->getAngle() );
00227 }
00228
00229 if ($this->_scheduleResize) {
00230 $this->_getModel()->resize();
00231 }
00232
00233 if( $this->_scheduleWatermark ) {
00234 $this->_getModel()
00235 ->setWatermarkPosition( $this->getWatermarkPosition() )
00236 ->setWatermarkSize($this->parseSize($this->getWatermarkSize()))
00237 ->setWatermark($this->getWatermark(), $this->getWatermarkPosition());
00238 } else {
00239 if( $watermark = Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_image") ) {
00240 $this->_getModel()
00241 ->setWatermarkPosition( $this->getWatermarkPosition() )
00242 ->setWatermarkSize($this->parseSize($this->getWatermarkSize()))
00243 ->setWatermark($watermark, $this->getWatermarkPosition());
00244 }
00245 }
00246
00247 $url = $this->_getModel()->saveFile()->getUrl();
00248 }
00249 } catch( Exception $e ) {
00250 $url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
00251 }
00252 return $url;
00253 }
00254
00255
00256
00257
00258
00259
00260 protected function _setModel($model)
00261 {
00262 $this->_model = $model;
00263 return $this;
00264 }
00265
00266
00267
00268
00269
00270
00271 protected function _getModel()
00272 {
00273 return $this->_model;
00274 }
00275
00276 protected function setAngle($angle)
00277 {
00278 $this->_angle = $angle;
00279 return $this;
00280 }
00281
00282 protected function getAngle()
00283 {
00284 return $this->_angle;
00285 }
00286
00287 protected function setWatermark($watermark)
00288 {
00289 $this->_watermark = $watermark;
00290 return $this;
00291 }
00292
00293 protected function getWatermark()
00294 {
00295 return $this->_watermark;
00296 }
00297
00298 protected function setWatermarkPosition($position)
00299 {
00300 $this->_watermarkPosition = $position;
00301 return $this;
00302 }
00303
00304 protected function getWatermarkPosition()
00305 {
00306 if( $this->_watermarkPosition ) {
00307 return $this->_watermarkPosition;
00308 } else {
00309 return Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_position");
00310 }
00311 }
00312
00313 public function setWatermarkSize($size)
00314 {
00315 $this->_watermarkSize = $size;
00316 return $this;
00317 }
00318
00319 protected function getWatermarkSize()
00320 {
00321 if( $this->_watermarkSize ) {
00322 return $this->_watermarkSize;
00323 } else {
00324 return Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_size");
00325 }
00326 }
00327
00328 protected function setProduct($product)
00329 {
00330 $this->_product = $product;
00331 return $this;
00332 }
00333
00334 protected function getProduct()
00335 {
00336 return $this->_product;
00337 }
00338
00339 protected function setImageFile($file)
00340 {
00341 $this->_imageFile = $file;
00342 return $this;
00343 }
00344
00345 protected function getImageFile()
00346 {
00347 return $this->_imageFile;
00348 }
00349
00350
00351
00352
00353
00354
00355 protected function parseSize($string)
00356 {
00357 $size = explode('x', strtolower($string));
00358 if( sizeof($size) == 2 ) {
00359 return array(
00360 'width' => ($size[0] > 0) ? $size[0] : null,
00361 'heigth' => ($size[1] > 0) ? $size[1] : null,
00362 );
00363 }
00364 return false;
00365 }
00366
00367
00368
00369
00370
00371
00372 public function getOriginalWidth()
00373 {
00374 return $this->_getModel()->getImageProcessor()->getOriginalWidth();
00375 }
00376
00377
00378
00379
00380
00381
00382 public function getOriginalHeigh()
00383 {
00384 return $this->_getModel()->getImageProcessor()->getOriginalHeight();
00385 }
00386
00387
00388
00389
00390
00391
00392
00393 public function getOriginalSizeArray()
00394 {
00395 return array(
00396 $this->getOriginalWidth(),
00397 $this->getOriginalHeigh()
00398 );
00399 }
00400 }