Skip to content

Commit

Permalink
version 0.0.7
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Crawford <[email protected]>
  • Loading branch information
engram-design committed Oct 3, 2015
1 parent 96c6bbd commit 419e494
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ Aspect ratio options are:

## Changelog

#### 0.0.7

- Fix to ensure images uploaded are both an image, and manipulatable.
- Better error-catching for resizing.

#### 0.0.6

- Added cropping option to Element Actions.
Expand Down
12 changes: 8 additions & 4 deletions imageresizer/ImageResizerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getName()

public function getVersion()
{
return '0.0.6';
return '0.0.7';
}

public function getDeveloper()
Expand Down Expand Up @@ -64,9 +64,13 @@ public function init()

if (craft()->imageResizer->getSettings()->enabled) {

// Only process if it's a new asset being saved - and if its actually an image
if ($event->params['isNewAsset'] && $asset->kind == 'image') {
craft()->imageResizer->resize($asset);
// Only process if it's a new asset being saved.
if ($event->params['isNewAsset']) {

// Is this a manipulatable image?
if (ImageHelper::isImageManipulatable(IOHelper::getExtension($asset->filename))) {
craft()->imageResizer->resize($asset);
}
}
}
});
Expand Down
66 changes: 36 additions & 30 deletions imageresizer/services/ImageResizerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,52 @@ public function getSettings()

public function resize($asset)
{
// Get the full path of the asset we want to resize
$path = $this->_getImagePath($asset);
try {
// Get the full path of the asset we want to resize
$path = $this->_getImagePath($asset);

// If the path is false, we're not allowed to modify images in the source - kill it!
if (!$path) {
return true;
}
// If the path is false, we're not allowed to modify images in the source - kill it!
if (!$path) {
return true;
}

$image = craft()->images->loadImage($path);
$image = craft()->images->loadImage($path);

// Our maximum width/height for assets from plugin settings
$imageWidth = $this->getSettings()->imageWidth;
$imageHeight = $this->getSettings()->imageHeight;
// Our maximum width/height for assets from plugin settings
$imageWidth = $this->getSettings()->imageWidth;
$imageHeight = $this->getSettings()->imageHeight;

// Lets check to see if this image needs resizing. Split into two steps to ensure
// proper aspect ratio is preserved and no upscaling occurs.
$hasResized = false;
// Lets check to see if this image needs resizing. Split into two steps to ensure
// proper aspect ratio is preserved and no upscaling occurs.
$hasResized = false;

if ($image->getWidth() > $imageWidth) {
$hasResized = true;
$this->_resizeImage($image, $imageWidth, null);
}
if ($image->getWidth() > $imageWidth) {
$hasResized = true;
$this->_resizeImage($image, $imageWidth, null);
}

if ($image->getHeight() > $imageHeight) {
$hasResized = true;
$this->_resizeImage($image, null, $imageHeight);
}
if ($image->getHeight() > $imageHeight) {
$hasResized = true;
$this->_resizeImage($image, null, $imageHeight);
}

if ($hasResized) {
// Set image quality - but normalise (for PNG)!
$quality = $this->_getImageQuality($asset);
$image->setQuality($quality);
if ($hasResized) {
// Set image quality - but normalise (for PNG)!
$quality = $this->_getImageQuality($asset);
$image->setQuality($quality);

$image->saveAs($path);
$image->saveAs($path);

// Update the asset record to reflect changes
$this->_updateAsset($asset, $image, $path);
}
// Update the asset record to reflect changes
$this->_updateAsset($asset, $image, $path);
}

return $asset;
} catch (\Exception $e) {
ImageResizerPlugin::log($e->getMessage(), LogLevel::Error, true);

return $asset;
return false;
}
}

public function crop($asset, $x1, $x2, $y1, $y2)
Expand Down

0 comments on commit 419e494

Please sign in to comment.