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 class Mage_Catalog_Model_Product_Image extends Mage_Core_Model_Abstract
00035 {
00036 protected $_width;
00037 protected $_height;
00038
00039 protected $_keepAspectRatio = true;
00040 protected $_keepFrame = true;
00041 protected $_keepTransparency = true;
00042 protected $_constrainOnly = false;
00043 protected $_backgroundColor = array(255, 255, 255);
00044
00045 protected $_baseFile;
00046 protected $_newFile;
00047 protected $_processor;
00048 protected $_destinationSubdir;
00049 protected $_angle;
00050 protected $_watermarkPosition;
00051 protected $_watermarkWidth;
00052 protected $_watermarkHeigth;
00053
00054
00055
00056
00057 public function setWidth($width)
00058 {
00059 $this->_width = $width;
00060 return $this;
00061 }
00062
00063 public function getWidth()
00064 {
00065 return $this->_width;
00066 }
00067
00068
00069
00070
00071 public function setHeight($height)
00072 {
00073 $this->_height = $height;
00074 return $this;
00075 }
00076
00077 public function getHeight()
00078 {
00079 return $this->_height;
00080 }
00081
00082
00083
00084
00085 public function setKeepAspectRatio($keep)
00086 {
00087 $this->_keepAspectRatio = (bool)$keep;
00088 return $this;
00089 }
00090
00091
00092
00093
00094 public function setKeepFrame($keep)
00095 {
00096 $this->_keepFrame = (bool)$keep;
00097 return $this;
00098 }
00099
00100
00101
00102
00103 public function setKeepTransparency($keep)
00104 {
00105 $this->_keepTransparency = (bool)$keep;
00106 return $this;
00107 }
00108
00109
00110
00111
00112 public function setConstrainOnly($flag)
00113 {
00114 $this->_constrainOnly = (bool)$flag;
00115 return $this;
00116 }
00117
00118
00119
00120
00121 public function setBackgroundColor(array $rgbArray)
00122 {
00123 $this->_backgroundColor = $rgbArray;
00124 return $this;
00125 }
00126
00127
00128
00129
00130 public function setSize($size)
00131 {
00132
00133 list($width, $height) = explode('x', strtolower($size), 2);
00134 foreach (array('width', 'height') as $wh) {
00135 $$wh = (int)$$wh;
00136 if (empty($$wh))
00137 $$wh = null;
00138 }
00139
00140
00141 $this->setWidth($width)->setHeight($height);
00142
00143 return $this;
00144 }
00145
00146 protected function _checkMemory($file = null)
00147 {
00148
00149
00150
00151
00152 return $this->_getMemoryLimit() > ($this->_getMemoryUsage() + $this->_getNeedMemoryForFile($file));
00153 }
00154
00155 protected function _getMemoryLimit()
00156 {
00157 $memoryLimit = ini_get('memory_limit');
00158
00159 if (!isSet($memoryLimit[0])){
00160 $memoryLimit = "128M";
00161 }
00162
00163 if (substr($memoryLimit, -1) == 'M') {
00164 return (int)$memoryLimit * 1024 * 1024;
00165 }
00166 return $memoryLimit;
00167 }
00168
00169 protected function _getMemoryUsage()
00170 {
00171 if (function_exists('memory_get_usage')) {
00172 return memory_get_usage();
00173 }
00174 return 0;
00175 }
00176
00177 protected function _getNeedMemoryForFile($file = null)
00178 {
00179 $file = is_null($file) ? $this->getBaseFile() : $file;
00180 if (!$file) {
00181 return 0;
00182 }
00183
00184 if (!file_exists($file) || !is_file($file)) {
00185 return 0;
00186 }
00187
00188 $imageInfo = getimagesize($file);
00189
00190 if (!isset($imageInfo['channels'])) {
00191
00192 $imageInfo['channels'] = 4;
00193 }
00194 if (!isset($imageInfo['bits'])) {
00195
00196 $imageInfo['bits'] = 8;
00197 }
00198 return round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.65);
00199 }
00200
00201
00202
00203
00204
00205
00206
00207 private function _rgbToString($rgbArray)
00208 {
00209 $result = array();
00210 foreach ($rgbArray as $value) {
00211 if (null === $value) {
00212 $result[] = 'null';
00213 }
00214 else {
00215 $result[] = sprintf('%02s', dechex($value));
00216 }
00217 }
00218 return implode($result);
00219 }
00220
00221
00222
00223
00224
00225
00226
00227 public function setBaseFile($file)
00228 {
00229 if (($file) && (0 !== strpos($file, '/', 0))) {
00230 $file = '/' . $file;
00231 }
00232 $baseDir = Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath();
00233
00234 if ('/no_selection' == $file) {
00235 $file = null;
00236 }
00237 if ($file) {
00238 if ((!file_exists($baseDir . $file)) || !$this->_checkMemory($baseDir . $file)) {
00239 $file = null;
00240 }
00241 }
00242 if (!$file) {
00243
00244 $isConfigPlaceholder = Mage::getStoreConfig("catalog/placeholder/{$this->getDestinationSubdir()}_placeholder");
00245 $configPlaceholder = '/placeholder/' . $isConfigPlaceholder;
00246 if ($isConfigPlaceholder && file_exists($baseDir . $configPlaceholder)) {
00247 $file = $configPlaceholder;
00248 }
00249 else {
00250
00251 $skinBaseDir = Mage::getDesign()->getSkinBaseDir();
00252 $skinPlaceholder = "/images/catalog/product/placeholder/{$this->getDestinationSubdir()}.jpg";
00253 $file = $skinPlaceholder;
00254 if (file_exists($skinBaseDir . $file)) {
00255 $baseDir = $skinBaseDir;
00256 }
00257 else {
00258 $baseDir = Mage::getDesign()->getSkinBaseDir(array('_theme' => 'default'));
00259 }
00260 }
00261 }
00262
00263 $baseFile = $baseDir . $file;
00264
00265 if ((!$file) || (!file_exists($baseFile))) {
00266 throw new Exception(Mage::helper('catalog')->__('Image file not found'));
00267 }
00268 $this->_baseFile = $baseFile;
00269
00270
00271 $path = array(
00272 Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath(),
00273 'cache',
00274 Mage::app()->getStore()->getId(),
00275 $path[] = $this->getDestinationSubdir()
00276 );
00277 if((!empty($this->_width)) || (!empty($this->_height)))
00278 $path[] = "{$this->_width}x{$this->_height}";
00279
00280 $path[] = md5(
00281 implode('_', array(
00282 ($this->_keepAspectRatio ? '' : 'non') . 'proportional',
00283 ($this->_keepFrame ? '' : 'no') . 'frame',
00284 ($this->_keepTransparency ? '' : 'no') . 'transparency',
00285 ($this->_constrainOnly ? 'do' : 'not') . 'constrainonly',
00286 $this->_rgbToString($this->_backgroundColor),
00287 'angle' . $this->_angle
00288 ))
00289 );
00290
00291 $this->_newFile = implode('/', $path) . $file;
00292
00293 return $this;
00294 }
00295
00296 public function getBaseFile()
00297 {
00298 return $this->_baseFile;
00299 }
00300
00301 public function getNewFile()
00302 {
00303 return $this->_newFile;
00304 }
00305
00306
00307
00308
00309 public function setImageProcessor($processor)
00310 {
00311 $this->_processor = $processor;
00312 return $this;
00313 }
00314
00315
00316
00317
00318 public function getImageProcessor()
00319 {
00320 if( !$this->_processor ) {
00321
00322
00323
00324
00325 $this->_processor = new Varien_Image($this->getBaseFile());
00326 }
00327 $this->_processor->keepAspectRatio($this->_keepAspectRatio);
00328 $this->_processor->keepFrame($this->_keepFrame);
00329 $this->_processor->keepTransparency($this->_keepTransparency);
00330 $this->_processor->constrainOnly($this->_constrainOnly);
00331 $this->_processor->backgroundColor($this->_backgroundColor);
00332 return $this->_processor;
00333 }
00334
00335
00336
00337
00338
00339 public function resize()
00340 {
00341 if (is_null($this->getWidth()) && is_null($this->getHeight())) {
00342 return $this;
00343 }
00344 $this->getImageProcessor()->resize($this->_width, $this->_height);
00345 return $this;
00346 }
00347
00348
00349
00350
00351 public function rotate($angle)
00352 {
00353 $angle = intval($angle);
00354 $this->getImageProcessor()->rotate($angle);
00355 return $this;
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 public function setAngle($angle)
00367 {
00368 $this->_angle = $angle;
00369 return $this;
00370 }
00371
00372
00373
00374
00375 public function setWatermark($file, $position=null, $size=null, $width=null, $heigth=null)
00376 {
00377 $filename = false;
00378
00379 if( !$file ) {
00380 return $this;
00381 }
00382
00383 $baseDir = Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath();
00384
00385 if( file_exists($baseDir . '/watermark/stores/' . Mage::app()->getStore()->getId() . $file) ) {
00386 $filename = $baseDir . '/watermark/stores/' . Mage::app()->getStore()->getId() . $file;
00387 } elseif ( file_exists($baseDir . '/watermark/websites/' . Mage::app()->getWebsite()->getId() . $file) ) {
00388 $filename = $baseDir . '/watermark/websites/' . Mage::app()->getWebsite()->getId() . $file;
00389 } elseif ( file_exists($baseDir . '/watermark/default/' . $file) ) {
00390 $filename = $baseDir . '/watermark/default/' . $file;
00391 } elseif ( file_exists($baseDir . '/watermark/' . $file) ) {
00392 $filename = $baseDir . '/watermark/' . $file;
00393 } else {
00394 $baseDir = Mage::getDesign()->getSkinBaseDir();
00395 if( file_exists($baseDir . $file) ) {
00396 $filename = $baseDir . $file;
00397 }
00398 }
00399
00400 if( $filename ) {
00401 $this->getImageProcessor()
00402 ->setWatermarkPosition( ($position) ? $position : $this->getWatermarkPosition() )
00403 ->setWatermarkWidth( ($width) ? $width : $this->getWatermarkWidth() )
00404 ->setWatermarkHeigth( ($heigth) ? $heigth : $this->getWatermarkHeigth() )
00405 ->watermark($filename);
00406 }
00407
00408 return $this;
00409 }
00410
00411
00412
00413
00414 public function saveFile()
00415 {
00416 $this->getImageProcessor()->save($this->getNewFile());
00417 return $this;
00418 }
00419
00420
00421
00422
00423 public function getUrl()
00424 {
00425 $baseDir = Mage::getBaseDir('media');
00426 $path = str_replace($baseDir . DS, "", $this->_newFile);
00427 return Mage::getBaseUrl('media') . str_replace(DS, '/', $path);
00428 }
00429
00430 public function push()
00431 {
00432 $this->getImageProcessor()->display();
00433 }
00434
00435
00436
00437
00438 public function setDestinationSubdir($dir)
00439 {
00440 $this->_destinationSubdir = $dir;
00441 return $this;
00442 }
00443
00444
00445
00446
00447 public function getDestinationSubdir()
00448 {
00449 return $this->_destinationSubdir;
00450 }
00451
00452 public function isCached()
00453 {
00454 return file_exists($this->_newFile);
00455 }
00456
00457
00458
00459
00460 public function setWatermarkPosition($position)
00461 {
00462 $this->_watermarkPosition = $position;
00463 return $this;
00464 }
00465
00466 public function getWatermarkPosition()
00467 {
00468 return $this->_watermarkPosition;
00469 }
00470
00471
00472
00473
00474 public function setWatermarkSize($size)
00475 {
00476 if( is_array($size) ) {
00477 $this->setWatermarkWidth($size['width'])
00478 ->setWatermarkHeigth($size['heigth']);
00479 }
00480 return $this;
00481 }
00482
00483
00484
00485
00486 public function setWatermarkWidth($width)
00487 {
00488 $this->_watermarkWidth = $width;
00489 return $this;
00490 }
00491
00492 public function getWatermarkWidth()
00493 {
00494 return $this->_watermarkWidth;
00495 }
00496
00497
00498
00499
00500 public function setWatermarkHeigth($heigth)
00501 {
00502 $this->_watermarkHeigth = $heigth;
00503 return $this;
00504 }
00505
00506 public function getWatermarkHeigth()
00507 {
00508 return $this->_watermarkHeigth;
00509 }
00510
00511 public function clearCache()
00512 {
00513 $directory = Mage::getBaseDir('media') . DS.'catalog'.DS.'product'.DS.'cache'.DS;
00514 $io = new Varien_Io_File();
00515 $io->rmdir($directory, true);
00516 }
00517 }