Varien_Image_Adapter_Gd2 Class Reference

Inheritance diagram for Varien_Image_Adapter_Gd2:

Varien_Image_Adapter_Abstract

List of all members.

Public Member Functions

 open ($filename)
 save ($destination=null, $newName=null)
 display ()
 resize ($frameWidth=null, $frameHeight=null)
 rotate ($angle)
 watermark ($watermarkImage, $positionX=0, $positionY=0, $watermarkImageOpacity=30, $repeat=false)
 crop ($top=0, $bottom=0, $right=0, $left=0)
 checkDependencies ()
 __destruct ()

Protected Attributes

 $_requiredExtensions = Array("gd")


Detailed Description

Definition at line 28 of file Gd2.php.


Constructor & Destructor Documentation

__destruct (  ) 

Definition at line 390 of file Gd2.php.

00391     {
00392         @imagedestroy($this->_imageHandler);
00393     }


Member Function Documentation

checkDependencies (  ) 

Reimplemented from Varien_Image_Adapter_Abstract.

Definition at line 375 of file Gd2.php.

00376     {
00377         foreach( $this->_requiredExtensions as $value ) {
00378             if( !extension_loaded($value) ) {
00379                 throw new Exception("Required PHP extension '{$value}' was not loaded.");
00380             }
00381         }
00382     }

crop ( top = 0,
bottom = 0,
right = 0,
left = 0 
)

Reimplemented from Varien_Image_Adapter_Abstract.

Definition at line 354 of file Gd2.php.

00355     {
00356         if( $left == 0 && $top == 0 && $right == 0 && $bottom == 0 ) {
00357             return;
00358         }
00359 
00360         $newWidth = $this->_imageSrcWidth - $left - $right;
00361         $newHeight = $this->_imageSrcHeight - $top - $bottom;
00362 
00363         $canvas = imagecreatetruecolor($newWidth, $newHeight);
00364 
00365         if ($this->_fileType == IMAGETYPE_PNG) {
00366             $this->_saveAlpha($canvas);
00367         }
00368 
00369         imagecopyresampled($canvas, $this->_imageHandler, $top, $bottom, $right, $left, $this->_imageSrcWidth, $this->_imageSrcHeight, $newWidth, $newHeight);
00370 
00371         $this->_imageHandler = $canvas;
00372         $this->refreshImageDimensions();
00373     }

display (  ) 

Reimplemented from Varien_Image_Adapter_Abstract.

Definition at line 83 of file Gd2.php.

00084     {
00085         header("Content-type: ".$this->getMimeType());
00086         call_user_func($this->_getCallback('output'), $this->_imageHandler);
00087     }

open ( filename  ) 

Reimplemented from Varien_Image_Adapter_Abstract.

Definition at line 39 of file Gd2.php.

00040     {
00041         $this->_fileName = $filename;
00042         $this->getMimeType();
00043         $this->_getFileAttributes();
00044         $this->_imageHandler = call_user_func($this->_getCallback('create'), $this->_fileName);
00045     }

resize ( frameWidth = null,
frameHeight = null 
)

Change the image size

Parameters:
int $frameWidth
int $frameHeight

Reimplemented from Varien_Image_Adapter_Abstract.

Definition at line 191 of file Gd2.php.

00192     {
00193         if (empty($frameWidth) && empty($frameHeight)) {
00194             throw new Exception('Invalid image dimensions.');
00195         }
00196 
00197         // calculate lacking dimension
00198         if (!$this->_keepFrame) {
00199             if (null === $frameWidth) {
00200                 $frameWidth = round($frameHeight * ($this->_imageSrcWidth / $this->_imageSrcHeight));
00201             }
00202             elseif (null === $frameHeight) {
00203                 $frameHeight = round($frameWidth * ($this->_imageSrcHeight / $this->_imageSrcWidth));
00204             }
00205         }
00206         else {
00207             if (null === $frameWidth) {
00208                 $frameWidth = $frameHeight;
00209             }
00210             elseif (null === $frameHeight) {
00211                 $frameHeight = $frameWidth;
00212             }
00213         }
00214 
00215         // define coordinates of image inside new frame
00216         $srcX = 0;
00217         $srcY = 0;
00218         $dstX = 0;
00219         $dstY = 0;
00220         $dstWidth  = $frameWidth;
00221         $dstHeight = $frameHeight;
00222         if ($this->_keepAspectRatio) {
00223             // do not make picture bigger, than it is, if required
00224             if ($this->_constrainOnly) {
00225                 if (($frameWidth >= $this->_imageSrcWidth) && ($frameHeight >= $this->_imageSrcHeight)) {
00226                     $dstWidth  = $this->_imageSrcWidth;
00227                     $dstHeight = $this->_imageSrcHeight;
00228                 }
00229             }
00230             // keep aspect ratio
00231             if ($this->_imageSrcWidth / $this->_imageSrcHeight >= $frameWidth / $frameHeight) {
00232                 $dstHeight = round(($dstWidth / $this->_imageSrcWidth) * $this->_imageSrcHeight);
00233             } else {
00234                 $dstWidth = round(($dstHeight / $this->_imageSrcHeight) * $this->_imageSrcWidth);
00235             }
00236         }
00237         // define position in center (TODO: add positions option)
00238         $dstY = round(($frameHeight - $dstHeight) / 2);
00239         $dstX = round(($frameWidth - $dstWidth) / 2);
00240 
00241         // get rid of frame (fallback to zero position coordinates)
00242         if (!$this->_keepFrame) {
00243             $frameWidth  = $dstWidth;
00244             $frameHeight = $dstHeight;
00245             $dstY = 0;
00246             $dstX = 0;
00247         }
00248 
00249         // create new image
00250         $isAlpha     = false;
00251         $isTrueColor = false;
00252         $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha, $isTrueColor);
00253         if ($isTrueColor) {
00254             $newImage = imagecreatetruecolor($frameWidth, $frameHeight);
00255         }
00256         else {
00257             $newImage = imagecreate($frameWidth, $frameHeight);
00258         }
00259 
00260         // fill new image with required color
00261         $this->_fillBackgroundColor($newImage);
00262 
00263         // resample source image and copy it into new frame
00264         imagecopyresampled($newImage, $this->_imageHandler, $dstX, $dstY, $srcX, $srcY, $dstWidth, $dstHeight, $this->_imageSrcWidth, $this->_imageSrcHeight);
00265         $this->_imageHandler = $newImage;
00266         $this->refreshImageDimensions();
00267     }

rotate ( angle  ) 

Reimplemented from Varien_Image_Adapter_Abstract.

Definition at line 269 of file Gd2.php.

00270     {
00271 /*
00272         $isAlpha = false;
00273         $backgroundColor = $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha);
00274         list($r, $g, $b) = $this->_backgroundColor;
00275         if ($isAlpha) {
00276             $backgroundColor = imagecolorallocatealpha($this->_imageHandler, 0, 0, 0, 127);
00277         }
00278         elseif (false === $backgroundColor) {
00279             $backgroundColor = imagecolorallocate($this->_imageHandler, $r, $g, $b);
00280         }
00281         $this->_imageHandler = imagerotate($this->_imageHandler, $angle, $backgroundColor);
00282 //*/
00283         $this->_imageHandler = imagerotate($this->_imageHandler, $angle, $this->imageBackgroundColor);
00284         $this->refreshImageDimensions();
00285     }

save ( destination = null,
newName = null 
)

Reimplemented from Varien_Image_Adapter_Abstract.

Definition at line 47 of file Gd2.php.

00048     {
00049         $fileName = ( !isset($destination) ) ? $this->_fileName : $destination;
00050 
00051         if( isset($destination) && isset($newName) ) {
00052             $fileName = $destination . "/" . $fileName;
00053         } elseif( isset($destination) && !isset($newName) ) {
00054             $info = pathinfo($destination);
00055             $fileName = $destination;
00056             $destination = $info['dirname'];
00057         } elseif( !isset($destination) && isset($newName) ) {
00058             $fileName = $this->_fileSrcPath . "/" . $newName;
00059         } else {
00060             $fileName = $this->_fileSrcPath . $this->_fileSrcName;
00061         }
00062 
00063         $destinationDir = ( isset($destination) ) ? $destination : $this->_fileSrcPath;
00064 
00065         if( !is_writable($destinationDir) ) {
00066             try {
00067                 $io = new Varien_Io_File();
00068                 $io->mkdir($destination);
00069             } catch (Exception $e) {
00070                 throw new Exception("Unable to write file into directory '{$destinationDir}'. Access forbidden.");
00071             }
00072         }
00073 
00074         // keep alpha transparency
00075         $isAlpha     = false;
00076         $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha);
00077         if ($isAlpha) {
00078             $this->_fillBackgroundColor($this->_imageHandler);
00079         }
00080         call_user_func($this->_getCallback('output'), $this->_imageHandler, $fileName);
00081     }

watermark ( watermarkImage,
positionX = 0,
positionY = 0,
watermarkImageOpacity = 30,
repeat = false 
)

Reimplemented from Varien_Image_Adapter_Abstract.

Definition at line 287 of file Gd2.php.

00288     {
00289         list($watermarkSrcWidth, $watermarkSrcHeight, $watermarkFileType, ) = getimagesize($watermarkImage);
00290         $this->_getFileAttributes();
00291         $watermark = call_user_func($this->_getCallback('create', $watermarkFileType, 'Unsupported watermark image format.'), $watermarkImage);
00292 
00293         $merged = false;
00294 
00295         if( $this->getWatermarkWidth() && $this->getWatermarkHeigth() && ($this->getWatermarkPosition() != self::POSITION_STRETCH) ) {
00296             $newWatermark = imagecreatetruecolor($this->getWatermarkWidth(), $this->getWatermarkHeigth());
00297             imagealphablending($newWatermark, false);
00298             $col = imagecolorallocate($newWatermark, 255, 255, 255);
00299             imagecolortransparent($newWatermark, $col);
00300             imagefilledrectangle($newWatermark, 0, 0, $this->getWatermarkWidth(), $this->getWatermarkHeigth(), $col);
00301             imagealphablending($newWatermark, true);
00302 
00303             imagecopyresampled($newWatermark, $watermark, 0, 0, 0, 0, $this->getWatermarkWidth(), $this->getWatermarkHeigth(), imagesx($watermark), imagesy($watermark));
00304             $watermark = $newWatermark;
00305         }
00306 
00307         if( $this->getWatermarkPosition() == self::POSITION_TILE ) {
00308             $repeat = true;
00309         } elseif( $this->getWatermarkPosition() == self::POSITION_STRETCH ) {
00310             $newWatermark = imagecreatetruecolor($this->_imageSrcWidth, $this->_imageSrcHeight);
00311             imagealphablending($newWatermark, false);
00312             $col = imagecolorallocate($newWatermark, 255, 255, 255);
00313             imagecolortransparent($newWatermark, $col);
00314             imagefilledrectangle($newWatermark, 0, 0, $this->_imageSrcWidth, $this->_imageSrcHeight, $col);
00315             imagealphablending($newWatermark, true);
00316             imagecopyresampled($newWatermark, $watermark, 0, 0, 0, 0, $this->_imageSrcWidth, $this->_imageSrcHeight, imagesx($watermark), imagesy($watermark));
00317             $watermark = $newWatermark;
00318         } elseif( $this->getWatermarkPosition() == self::POSITION_TOP_RIGHT ) {
00319             $positionX = ($this->_imageSrcWidth - imagesx($watermark));
00320             imagecopy($this->_imageHandler, $watermark, $positionX, $positionY, 0, 0, imagesx($watermark), imagesy($watermark));
00321             $merged = true;
00322         } elseif( $this->getWatermarkPosition() == self::POSITION_TOP_LEFT  ) {
00323             imagecopy($this->_imageHandler, $watermark, $positionX, $positionY, 0, 0, imagesx($watermark), imagesy($watermark));
00324             $merged = true;
00325         } elseif( $this->getWatermarkPosition() == self::POSITION_BOTTOM_RIGHT ) {
00326             $positionX = ($this->_imageSrcWidth - imagesx($watermark));
00327             $positionY = ($this->_imageSrcHeight - imagesy($watermark));
00328             imagecopy($this->_imageHandler, $watermark, $positionX, $positionY, 0, 0, imagesx($watermark), imagesy($watermark));
00329             $merged = true;
00330         } elseif( $this->getWatermarkPosition() == self::POSITION_BOTTOM_LEFT ) {
00331             $positionY = ($this->_imageSrcHeight - imagesy($watermark));
00332             imagecopy($this->_imageHandler, $watermark, $positionX, $positionY, 0, 0, imagesx($watermark), imagesy($watermark));
00333             $merged = true;
00334         }
00335 
00336         if( $repeat === false && $merged === false ) {
00337             imagecopymerge($this->_imageHandler, $watermark, $positionX, $positionY, 0, 0, imagesx($watermark), imagesy($watermark), $watermarkImageOpacity);
00338         } else {
00339             $offsetX = $positionX;
00340             $offsetY = $positionY;
00341             while( $offsetY <= ($this->_imageSrcHeight+imagesy($watermark)) ) {
00342                 while( $offsetX <= ($this->_imageSrcWidth+imagesx($watermark)) ) {
00343                     imagecopy($this->_imageHandler, $watermark, $offsetX, $offsetY, 0, 0, imagesx($watermark), imagesy($watermark));
00344                     $offsetX += imagesx($watermark);
00345                 }
00346                 $offsetX = $positionX;
00347                 $offsetY += imagesy($watermark);
00348             }
00349         }
00350         imagedestroy($watermark);
00351         $this->refreshImageDimensions();
00352     }


Member Data Documentation

$_requiredExtensions = Array("gd") [protected]

Reimplemented from Varien_Image_Adapter_Abstract.

Definition at line 30 of file Gd2.php.


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

Generated on Sat Jul 4 17:25:03 2009 for Magento by  doxygen 1.5.8