diff --git a/README.md b/README.md index 35429d3..a0dcda4 100644 --- a/README.md +++ b/README.md @@ -13,18 +13,24 @@ The aspect ratio of images are maintained, and will always match the maximum wid - Enable/Disable resizing images on upload. - Set the maximum width/height (in pixels) for uploaded images. Set to 2048px by default. +- Select which Asset sources you want resizing to be performed on. ## Roadmap - Batch processing of existing assets. -- Update assetRecord after image resize, to reflect new size. -- Restrict to specific Assets sources. - Add image quality option. +- Provide cropping options for uploaded images. ## Changelog +#### 0.0.2 + +- Moved hook from `onBeforeSaveAsset` to `onSaveAsset`. +- Asset record is updated after resize, reflecting new image width/height/size. +- Added option to restrict resizing to specific Asset sources. + #### 0.0.1 - Initial release. \ No newline at end of file diff --git a/imageresizer/ImageResizerPlugin.php b/imageresizer/ImageResizerPlugin.php index 356c393..b6f47de 100644 --- a/imageresizer/ImageResizerPlugin.php +++ b/imageresizer/ImageResizerPlugin.php @@ -14,7 +14,7 @@ public function getName() public function getVersion() { - return '0.0.1'; + return '0.0.2'; } public function getDeveloper() @@ -29,8 +29,15 @@ public function getDeveloperUrl() public function getSettingsHtml() { + $sourceOptions = array(); + + foreach (craft()->assetSources->getAllSources() as $source) { + $sourceOptions[] = array('label' => $source->name, 'value' => $source->id); + } + return craft()->templates->render('imageresizer/settings', array( 'settings' => $this->getSettings(), + 'sourceOptions' => $sourceOptions, )); } @@ -40,6 +47,7 @@ protected function defineSettings() 'enabled' => array( AttributeType::Bool, 'default' => true ), 'imageWidth' => array( AttributeType::Number, 'default' => '2048' ), 'imageHeight' => array( AttributeType::Number, 'default' => '2048' ), + 'assetSources' => array( AttributeType::Mixed, 'default' => '*' ), ); } @@ -50,7 +58,7 @@ protected function defineSettings() public function init() { - craft()->on('assets.onBeforeSaveAsset', function(Event $event) { + craft()->on('assets.onSaveAsset', function(Event $event) { if (craft()->imageResizer->getSettings()->enabled) { $asset = $event->params['asset']; diff --git a/imageresizer/services/ImageResizerService.php b/imageresizer/services/ImageResizerService.php index 3480869..202bf14 100644 --- a/imageresizer/services/ImageResizerService.php +++ b/imageresizer/services/ImageResizerService.php @@ -20,6 +20,12 @@ public function resize($asset) { // 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; + } + $image = craft()->images->loadImage($path); // Our maximum width/height for assets from plugin settings @@ -38,6 +44,14 @@ public function resize($asset) } $image->saveAs($path); + + // Then, make sure we update the asset info as stored in the database + $fileRecord = AssetFileRecord::model()->findById($asset->id); + $fileRecord->size = IOHelper::getFileSize($path); + $fileRecord->width = $image->getWidth(); + $fileRecord->height = $image->getHeight(); + + $fileRecord->save(false); } @@ -51,7 +65,16 @@ private function _getImagePath($asset) // Can only deal with local assets for now if ($source->type != 'Local') { - return true; + return false; + } + + // Should we be modifying images in this source? + $assetSources = $this->getSettings()->assetSources; + + if ($assetSources != '*') { + if (!in_array($source->id, $assetSources)) { + return false; + } } $sourcePath = $source->settings['path']; diff --git a/imageresizer/templates/settings/index.html b/imageresizer/templates/settings/index.html index 643510c..b890d82 100644 --- a/imageresizer/templates/settings/index.html +++ b/imageresizer/templates/settings/index.html @@ -27,4 +27,13 @@ size: 10, }) }} +{{ forms.checkboxSelectField({ + label: 'Asset Sources' | t, + instructions: 'Select which asset sources you want to allow Image Resizer work with.' | t, + id: 'assetSources', + name: 'assetSources', + values: settings.assetSources, + options: sourceOptions, +}) }} +