Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Crawford <[email protected]>
  • Loading branch information
engram-design committed Sep 19, 2015
0 parents commit b29117d
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
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.
64 changes: 64 additions & 0 deletions imageresizer/ImageResizerPlugin.php
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);
}
}
});
}
}
70 changes: 70 additions & 0 deletions imageresizer/services/ImageResizerService.php
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]);
}
}
30 changes: 30 additions & 0 deletions imageresizer/templates/settings/index.html
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>

0 comments on commit b29117d

Please sign in to comment.