-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Josh Crawford <[email protected]>
- Loading branch information
0 parents
commit b29117d
Showing
4 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Image Resizer | ||
|
||
Image Resizer is a Craft plugin that resizes your assets when they are uploaded. This allows huge images to be resized so as not to use up unnecessary disk space, but still kept at a reasonable resolution. This plugin is not a replacement for using image transforms throughout your site. | ||
|
||
The aspect ratio of images are maintained, and will always match the maximum width/height options in your plugin settings. For example, given a 4000 x 2500px image and a max width/height of 1024px, the resulting image would be 1024 x 640px. | ||
|
||
## Install | ||
|
||
- Add the `imageresize` directory into your `craft/plugins` directory. | ||
- Navigate to Settings -> Plugins and click the "Install" button. | ||
|
||
**Plugin options** | ||
|
||
- Enable/Disable resizing images on upload. | ||
- Set the maximum width/height (in pixels) for uploaded images. Set to 2048px by default. | ||
|
||
|
||
## Roadmap | ||
|
||
- Batch processing of existing assets. | ||
- Update assetRecord after image resize, to reflect new size. | ||
- Restrict to specific Assets sources. | ||
- Add image quality option. | ||
|
||
|
||
## Changelog | ||
|
||
#### 0.0.1 | ||
|
||
- Initial release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
namespace Craft; | ||
|
||
class ImageResizerPlugin extends BasePlugin | ||
{ | ||
/* -------------------------------------------------------------- | ||
* PLUGIN INFO | ||
* ------------------------------------------------------------ */ | ||
|
||
public function getName() | ||
{ | ||
return Craft::t('Image Resizer'); | ||
} | ||
|
||
public function getVersion() | ||
{ | ||
return '0.0.1'; | ||
} | ||
|
||
public function getDeveloper() | ||
{ | ||
return 'S. Group'; | ||
} | ||
|
||
public function getDeveloperUrl() | ||
{ | ||
return 'http://sgroup.com.au'; | ||
} | ||
|
||
public function getSettingsHtml() | ||
{ | ||
return craft()->templates->render('imageresizer/settings', array( | ||
'settings' => $this->getSettings(), | ||
)); | ||
} | ||
|
||
protected function defineSettings() | ||
{ | ||
return array( | ||
'enabled' => array( AttributeType::Bool, 'default' => true ), | ||
'imageWidth' => array( AttributeType::Number, 'default' => '2048' ), | ||
'imageHeight' => array( AttributeType::Number, 'default' => '2048' ), | ||
); | ||
} | ||
|
||
|
||
/* -------------------------------------------------------------- | ||
* HOOKS | ||
* ------------------------------------------------------------ */ | ||
|
||
public function init() | ||
{ | ||
craft()->on('assets.onBeforeSaveAsset', function(Event $event) { | ||
if (craft()->imageResizer->getSettings()->enabled) { | ||
$asset = $event->params['asset']; | ||
|
||
// 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); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
namespace Craft; | ||
|
||
class ImageResizerService extends BaseApplicationComponent | ||
{ | ||
// Public Methods | ||
// ========================================================================= | ||
|
||
public function getPlugin() | ||
{ | ||
return craft()->plugins->getPlugin('imageResizer'); | ||
} | ||
|
||
public function getSettings() | ||
{ | ||
return $this->getPlugin()->getSettings(); | ||
} | ||
|
||
public function resize($asset) | ||
{ | ||
// Get the full path of the asset we want to resize | ||
$path = $this->_getImagePath($asset); | ||
$image = craft()->images->loadImage($path); | ||
|
||
// 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. | ||
|
||
if ($image->getWidth() > $imageWidth) { | ||
$this->_resizeImage($image, $imageWidth, null); | ||
} | ||
|
||
if ($image->getHeight() > $imageHeight) { | ||
$this->_resizeImage($image, null, $imageHeight); | ||
} | ||
|
||
$image->saveAs($path); | ||
} | ||
|
||
|
||
// Private Methods | ||
// ========================================================================= | ||
|
||
private function _getImagePath($asset) | ||
{ | ||
// Get the full path for the asset being uploaded | ||
$source = $asset->getSource(); | ||
|
||
// Can only deal with local assets for now | ||
if ($source->type != 'Local') { | ||
return true; | ||
} | ||
|
||
$sourcePath = $source->settings['path']; | ||
$folderPath = $asset->getFolder()->path; | ||
|
||
return $sourcePath . $folderPath . $asset->filename; | ||
} | ||
|
||
private function _resizeImage(&$image, $width, $height) | ||
{ | ||
// Calculate the missing width/height for the asset - ensure aspect ratio is maintained | ||
$dimensions = ImageHelper::calculateMissingDimension($width, $height, $image->getWidth(), $image->getHeight()); | ||
|
||
$image->resize($dimensions[0], $dimensions[1]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{% import "_includes/forms" as forms %} | ||
|
||
{{ forms.lightswitchField({ | ||
label: 'Enabled' | t, | ||
instructions: 'Whether uploaded images should be resized or not.' | t, | ||
id: 'enabled', | ||
name: 'enabled', | ||
on: settings.enabled, | ||
first: true, | ||
}) }} | ||
|
||
{{ forms.textField({ | ||
label: 'Image Width' | t, | ||
instructions: 'The maximum width in pixels allowed for uploaded images.' | t, | ||
id: 'imageWidth', | ||
name: 'imageWidth', | ||
value: settings.imageWidth, | ||
size: 10, | ||
}) }} | ||
|
||
{{ forms.textField({ | ||
label: 'Image Height' | t, | ||
instructions: 'The maximum height in pixels allowed for uploaded images.' | t, | ||
id: 'imageHeight', | ||
name: 'imageHeight', | ||
value: settings.imageHeight, | ||
size: 10, | ||
}) }} | ||
|
||
<hr> |