Skip to content

Commit

Permalink
Improved performance and keepFrame support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl Spies authored and Karl Spies committed Jan 16, 2013
1 parent 3ae918a commit 670423a
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ skin/frontend/default/modern/
skin/frontend/enterprise
skin/install/
var/

.idea/*
data
1 change: 1 addition & 0 deletions modman
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/app/code/community/FireGento/PerfectWatermarks/ app/code/community/FireGento/PerfectWatermarks/
src/app/etc/modules/FireGento_PerfectWatermarks.xml app/etc/modules/FireGento_PerfectWatermarks.xml
src/app/code/community/Varien/Image/Adapter/Imagemagic.php app/code/community/Varien/Image/Adapter/Imagemagic.php
src/shell/* shell
110 changes: 95 additions & 15 deletions src/app/code/community/Varien/Image/Adapter/Imagemagic.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Varien_Image_Adapter_Imagemagic extends Varien_Image_Adapter_Abstract
*/
protected function getImageMagick()
{
if ($this->_imageHandler === null && $this->checkDependencies()) {
if ($this->_imageHandler === null) {
$this->_imageHandler = new Imagick();
}
return $this->_imageHandler;
Expand All @@ -26,7 +26,6 @@ protected function getImageMagick()
*/
public function open($fileName)
{
$this->checkDependencies();
$this->_fileName = $fileName;
$this->getMimeType();
$this->_getFileAttributes();
Expand Down Expand Up @@ -63,13 +62,20 @@ public function save($destination = null, $newName = null)
$io->mkdir($destination);
} catch (Exception $e) {
throw
new Exception(
"Unable to write into directory '{$destinationDir}'."
);
new Exception(
"Unable to write into directory '{$destinationDir}'."
);
}
}

$this->getImageMagick()->writeimage($fileName);
//set compression quality
$this->getImageMagick()->setImageCompressionQuality($this->_quality);
//remove all underlying information
$this->getImageMagick()->stripImage();
//write to file system
$this->getImageMagick()->writeImage($fileName);
//clear data and free resources
$this->getImageMagick()->clear();
$this->getImageMagick()->destroy();
}

public function display()
Expand All @@ -80,7 +86,45 @@ public function display()

public function resize($width = null, $height = null)
{
$this->getImageMagick()->adaptiveresizeimage($width, $height);
$widthFrame = $width;
$heightFrame = $height;
if ($width == null && $height == null) {
return;
}

if ($height == null || $this->_keepAspectRatio == true) {
$height = 0;
}

if ($width == null) {
$width = 0;
}

$this->getImageMagick()->scaleImage($width, $height);
//do only if we want a frame and the aspect ratio changed
if ($this->_keepFrame
&& ($widthFrame != $this->getImageMagick()->getImageWidth()
|| $height != $this->getImageMagick()->getImageHeight())
) {
$newFrameImage = new Imagick();
$color = 'rgb(' . implode(',', $this->backgroundColor()) . ')';
$newFrameImage->newImage(
$widthFrame,
$heightFrame,
new ImagickPixel($color)
);
$imageHeight = $this->getImageMagick()->getImageHeight();
$yPos = (($heightFrame - $imageHeight) / 2);
$newFrameImage->compositeImage(
$this->getImageMagick(),
Imagick::COMPOSITE_OVER,
0,
$yPos
);
$this->getImageMagick()->clear();
$this->getImageMagick()->destroy();
$this->_imageHandler = $newFrameImage;
}
}

public function rotate($angle)
Expand All @@ -90,24 +134,54 @@ public function rotate($angle)

public function crop($top = 0, $left = 0, $right = 0, $bottom = 0)
{
if ($left == 0 && $top == 0 && $right == 0 && $bottom == 0) {
return;
}
/* because drlrdsen said so! */
$this->getImageMagick()->cropimage($right - $left, $bottom - $top, $left, $top);
$this->getImageMagick()->cropImage(
$right - $left,
$bottom - $top,
$left,
$top
);
}

public function watermark($watermarkImage, $positionX = 0, $positionY = 0, $watermarkImageOpacity = 30, $repeat = false)
public function watermark(
$watermarkImage,
$positionX = 0,
$positionY = 0,
$watermarkImageOpacity = 30,
$repeat = false)
{
/** @var $watermark Imagick */
$watermark = new Imagick($watermarkImage);

if ($watermark->getImageAlphaChannel() == 0) {
$watermarkImageOpacity =
$this->getWatermarkImageOpacity() != null ?
$this->getWatermarkImageOpacity() : $watermarkImageOpacity;
$watermark->setImageOpacity($watermarkImageOpacity / 100);
}
// how big are the images?
$iWidth = $this->getImageMagick()->getImageWidth();
$iHeight = $this->getImageMagick()->getImageHeight();

//resize watermark to configuration size
if ($this->getWatermarkWidth() &&
$this->getWatermarkHeigth() &&
($this->getWatermarkPosition() != self::POSITION_STRETCH)
) {
$watermark->scaleImage(
$this->getWatermarkWidth(),
$this->getWatermarkHeigth()
);
}

// get watermark size
$wWidth = $watermark->getImageWidth();
$wHeight = $watermark->getImageHeight();

//check if watermark is still bigger then image.
if ($iHeight < $wHeight || $iWidth < $wWidth) {
// resize the watermark
$watermark->scaleImage($iWidth, $iHeight);
Expand Down Expand Up @@ -144,11 +218,11 @@ public function watermark($watermarkImage, $positionX = 0, $positionY = 0, $wate

$this->getImageMagick()->compositeImage(
$watermark,
Imagick::COMPOSITE_OVERLAY,
Imagick::COMPOSITE_OVER,
$x,
$y
);

$watermark->clear();
$watermark->destroy();
}

Expand All @@ -157,11 +231,17 @@ public function checkDependencies()
foreach ($this->_requiredExtensions as $value) {
if (!extension_loaded($value)) {
throw
new Exception(
"Required PHP extension '{$value}' was not loaded."
);
new Exception(
"Required PHP extension '{$value}' was not loaded."
);
}
}
return true;
}

public function __destruct()
{
@$this->getImageMagick()->clear();
@$this->getImageMagick()->destroy();
}
}
77 changes: 77 additions & 0 deletions src/shell/imagemagicktest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
require_once 'abstract.php';

class Image_Batch_Performance extends Mage_Shell_Abstract
{

/**
* Run script
*
*/
public function run()
{

if (!$this->getArg('d')) {
return $this->usageHelp();
}
/** @var $iterator SplFileObject[] */
$iterator = new DirectoryIterator($this->getArg('d'));
$start = microtime(true);
$i = 0;
$max = 0;
do {
foreach ($iterator as $file) {
if (!$file->isDir()) {
$image = new Varien_Image(
$this->getArg('d') . DS . $file->getFilename(),
Varien_Image_Adapter::ADAPTER_IM
);
$image->keepFrame(true);
$image->keepAspectRatio(true);
$image->keepTransparency(true);
$image->backgroundColor('000', '000', '000');
$image->resize(186, 500);
$image->setWatermarkImageOpacity(100);
$image->setWatermarkPosition(
Varien_Image_Adapter_Abstract::POSITION_TOP_LEFT
);
$image->setWatermarkHeigth(100);
$image->setWatermarkWidth(100);
$image->quality(80);
$watermark =
$this->getArg('d') . DS . 'watermark' . DS . 'watermark.png';
if (is_readable($watermark)) {
$image->watermark($watermark);
}
$image->save(
$this->getArg('d') . DS . 'result', $file->getFilename()
);
}
}
} while ($i++ < $max);
$endMem = memory_get_usage(true);
$end = microtime(true);

echo "Duration in seconds: " . (($end - $start)) . PHP_EOL;
echo "Memory usage in MB: " . (($endMem / 1024) / 1024) . PHP_EOL;
}

/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f imagemagicktest.php -- [options]
-h Short alias for help
-d directory with sample data
help This help
USAGE;
}
}

$imageBatch = new Image_Batch_Performance();
$imageBatch->run();

0 comments on commit 670423a

Please sign in to comment.