Mage_Catalog_Model_Product_Image Class Reference

Inheritance diagram for Mage_Catalog_Model_Product_Image:

Mage_Core_Model_Abstract Varien_Object

List of all members.

Public Member Functions

 setWidth ($width)
 getWidth ()
 setHeight ($height)
 getHeight ()
 setKeepAspectRatio ($keep)
 setKeepFrame ($keep)
 setKeepTransparency ($keep)
 setConstrainOnly ($flag)
 setBackgroundColor (array $rgbArray)
 setSize ($size)
 setBaseFile ($file)
 getBaseFile ()
 getNewFile ()
 setImageProcessor ($processor)
 getImageProcessor ()
 resize ()
 rotate ($angle)
 setAngle ($angle)
 setWatermark ($file, $position=null, $size=null, $width=null, $heigth=null)
 saveFile ()
 getUrl ()
 push ()
 setDestinationSubdir ($dir)
 getDestinationSubdir ()
 isCached ()
 setWatermarkPosition ($position)
 getWatermarkPosition ()
 setWatermarkSize ($size)
 setWatermarkWidth ($width)
 getWatermarkWidth ()
 setWatermarkHeigth ($heigth)
 getWatermarkHeigth ()
 clearCache ()

Protected Member Functions

 _checkMemory ($file=null)
 _getMemoryLimit ()
 _getMemoryUsage ()
 _getNeedMemoryForFile ($file=null)

Protected Attributes

 $_width
 $_height
 $_keepAspectRatio = true
 $_keepFrame = true
 $_keepTransparency = true
 $_constrainOnly = false
 $_backgroundColor = array(255, 255, 255)
 $_baseFile
 $_newFile
 $_processor
 $_destinationSubdir
 $_angle
 $_watermarkPosition
 $_watermarkWidth
 $_watermarkHeigth


Detailed Description

Definition at line 34 of file Image.php.


Member Function Documentation

_checkMemory ( file = null  )  [protected]

Definition at line 146 of file Image.php.

00147     {
00148 //        print '$this->_getMemoryLimit() = '.$this->_getMemoryLimit();
00149 //        print '$this->_getMemoryUsage() = '.$this->_getMemoryUsage();
00150 //        print '$this->_getNeedMemoryForBaseFile() = '.$this->_getNeedMemoryForBaseFile();
00151 
00152         return $this->_getMemoryLimit() > ($this->_getMemoryUsage() + $this->_getNeedMemoryForFile($file));
00153     }

_getMemoryLimit (  )  [protected]

Definition at line 155 of file Image.php.

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     }

_getMemoryUsage (  )  [protected]

Definition at line 169 of file Image.php.

00170     {
00171         if (function_exists('memory_get_usage')) {
00172             return memory_get_usage();
00173         }
00174         return 0;
00175     }

_getNeedMemoryForFile ( file = null  )  [protected]

Definition at line 177 of file Image.php.

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             // if there is no info about this parameter lets set it for maximum
00192             $imageInfo['channels'] = 4;
00193         }
00194         if (!isset($imageInfo['bits'])) {
00195             // if there is no info about this parameter lets set it for maximum
00196             $imageInfo['bits'] = 8;
00197         }
00198         return round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.65);
00199     }

clearCache (  ) 

Definition at line 511 of file Image.php.

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     }

getBaseFile (  ) 

Definition at line 296 of file Image.php.

00297     {
00298         return $this->_baseFile;
00299     }

getDestinationSubdir (  ) 

Returns:
string

Definition at line 447 of file Image.php.

00448     {
00449         return $this->_destinationSubdir;
00450     }

getHeight (  ) 

Definition at line 77 of file Image.php.

00078     {
00079         return $this->_height;
00080     }

getImageProcessor (  ) 

Returns:
Varien_Image

Definition at line 318 of file Image.php.

00319     {
00320         if( !$this->_processor ) {
00321 //            var_dump($this->_checkMemory());
00322 //            if (!$this->_checkMemory()) {
00323 //                $this->_baseFile = null;
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     }

getNewFile (  ) 

Definition at line 301 of file Image.php.

00302     {
00303         return $this->_newFile;
00304     }

getUrl (  ) 

Returns:
string

Definition at line 423 of file Image.php.

00424     {
00425         $baseDir = Mage::getBaseDir('media');
00426         $path = str_replace($baseDir . DS, "", $this->_newFile);
00427         return Mage::getBaseUrl('media') . str_replace(DS, '/', $path);
00428     }

getWatermarkHeigth (  ) 

Definition at line 506 of file Image.php.

00507     {
00508         return $this->_watermarkHeigth;
00509     }

getWatermarkPosition (  ) 

Definition at line 466 of file Image.php.

00467     {
00468         return $this->_watermarkPosition;
00469     }

getWatermarkWidth (  ) 

Definition at line 492 of file Image.php.

00493     {
00494         return $this->_watermarkWidth;
00495     }

getWidth (  ) 

Definition at line 63 of file Image.php.

00064     {
00065         return $this->_width;
00066     }

isCached (  ) 

Definition at line 452 of file Image.php.

00453     {
00454         return file_exists($this->_newFile);
00455     }

push (  ) 

Definition at line 430 of file Image.php.

00431     {
00432         $this->getImageProcessor()->display();
00433     }

resize (  ) 

See also:
Varien_Image_Adapter_Abstract
Returns:
Mage_Catalog_Model_Product_Image

Definition at line 339 of file Image.php.

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     }

rotate ( angle  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 351 of file Image.php.

00352     {
00353         $angle = intval($angle);
00354         $this->getImageProcessor()->rotate($angle);
00355         return $this;
00356     }

saveFile (  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 414 of file Image.php.

00415     {
00416         $this->getImageProcessor()->save($this->getNewFile());
00417         return $this;
00418     }

setAngle ( angle  ) 

Set angle for rotating

This func actually affects only the cache filename.

Parameters:
int $angle
Returns:
Mage_Catalog_Model_Product_Image

Definition at line 366 of file Image.php.

00367     {
00368         $this->_angle = $angle;
00369         return $this;
00370     }

setBackgroundColor ( array rgbArray  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 121 of file Image.php.

00122     {
00123         $this->_backgroundColor = $rgbArray;
00124         return $this;
00125     }

setBaseFile ( file  ) 

Set filenames for base file and new file

Parameters:
string $file
Returns:
Mage_Catalog_Model_Product_Image

Definition at line 227 of file Image.php.

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             // check if placeholder defined in config
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                 // replace file with skin or default skin placeholder
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         // build new filename (most important params)
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         // add misc params as a hash
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         // append prepared filename
00291         $this->_newFile = implode('/', $path) . $file; // the $file contains heading slash
00292 
00293         return $this;
00294     }

setConstrainOnly ( flag  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 112 of file Image.php.

00113     {
00114         $this->_constrainOnly = (bool)$flag;
00115         return $this;
00116     }

setDestinationSubdir ( dir  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 438 of file Image.php.

00439     {
00440         $this->_destinationSubdir = $dir;
00441         return $this;
00442     }

setHeight ( height  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 71 of file Image.php.

00072     {
00073         $this->_height = $height;
00074         return $this;
00075     }

setImageProcessor ( processor  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 309 of file Image.php.

00310     {
00311         $this->_processor = $processor;
00312         return $this;
00313     }

setKeepAspectRatio ( keep  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 85 of file Image.php.

00086     {
00087         $this->_keepAspectRatio = (bool)$keep;
00088         return $this;
00089     }

setKeepFrame ( keep  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 94 of file Image.php.

00095     {
00096         $this->_keepFrame = (bool)$keep;
00097         return $this;
00098     }

setKeepTransparency ( keep  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 103 of file Image.php.

00104     {
00105         $this->_keepTransparency = (bool)$keep;
00106         return $this;
00107     }

setSize ( size  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 130 of file Image.php.

00131     {
00132         // determine width and height from string
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         // set sizes
00141         $this->setWidth($width)->setHeight($height);
00142 
00143         return $this;
00144     }

setWatermark ( file,
position = null,
size = null,
width = null,
heigth = null 
)

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 375 of file Image.php.

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     }

setWatermarkHeigth ( heigth  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 500 of file Image.php.

00501     {
00502         $this->_watermarkHeigth = $heigth;
00503         return $this;
00504     }

setWatermarkPosition ( position  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 460 of file Image.php.

00461     {
00462         $this->_watermarkPosition = $position;
00463         return $this;
00464     }

setWatermarkSize ( size  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 474 of file Image.php.

00475     {
00476         if( is_array($size) ) {
00477             $this->setWatermarkWidth($size['width'])
00478                 ->setWatermarkHeigth($size['heigth']);
00479         }
00480         return $this;
00481     }

setWatermarkWidth ( width  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 486 of file Image.php.

00487     {
00488         $this->_watermarkWidth = $width;
00489         return $this;
00490     }

setWidth ( width  ) 

Returns:
Mage_Catalog_Model_Product_Image

Definition at line 57 of file Image.php.

00058     {
00059         $this->_width = $width;
00060         return $this;
00061     }


Member Data Documentation

$_angle [protected]

Definition at line 49 of file Image.php.

$_backgroundColor = array(255, 255, 255) [protected]

Definition at line 43 of file Image.php.

$_baseFile [protected]

Definition at line 45 of file Image.php.

$_constrainOnly = false [protected]

Definition at line 42 of file Image.php.

$_destinationSubdir [protected]

Definition at line 48 of file Image.php.

$_height [protected]

Definition at line 37 of file Image.php.

$_keepAspectRatio = true [protected]

Definition at line 39 of file Image.php.

$_keepFrame = true [protected]

Definition at line 40 of file Image.php.

$_keepTransparency = true [protected]

Definition at line 41 of file Image.php.

$_newFile [protected]

Definition at line 46 of file Image.php.

$_processor [protected]

Definition at line 47 of file Image.php.

$_watermarkHeigth [protected]

Definition at line 52 of file Image.php.

$_watermarkPosition [protected]

Definition at line 50 of file Image.php.

$_watermarkWidth [protected]

Definition at line 51 of file Image.php.

$_width [protected]

Definition at line 36 of file Image.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:41 2009 for Magento by  doxygen 1.5.8