Skip to content

Commit b29117d

Browse files
committed
initial commit
Signed-off-by: Josh Crawford <[email protected]>
0 parents  commit b29117d

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Image Resizer
2+
3+
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.
4+
5+
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.
6+
7+
## Install
8+
9+
- Add the `imageresize` directory into your `craft/plugins` directory.
10+
- Navigate to Settings -> Plugins and click the "Install" button.
11+
12+
**Plugin options**
13+
14+
- Enable/Disable resizing images on upload.
15+
- Set the maximum width/height (in pixels) for uploaded images. Set to 2048px by default.
16+
17+
18+
## Roadmap
19+
20+
- Batch processing of existing assets.
21+
- Update assetRecord after image resize, to reflect new size.
22+
- Restrict to specific Assets sources.
23+
- Add image quality option.
24+
25+
26+
## Changelog
27+
28+
#### 0.0.1
29+
30+
- Initial release.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
namespace Craft;
3+
4+
class ImageResizerPlugin extends BasePlugin
5+
{
6+
/* --------------------------------------------------------------
7+
* PLUGIN INFO
8+
* ------------------------------------------------------------ */
9+
10+
public function getName()
11+
{
12+
return Craft::t('Image Resizer');
13+
}
14+
15+
public function getVersion()
16+
{
17+
return '0.0.1';
18+
}
19+
20+
public function getDeveloper()
21+
{
22+
return 'S. Group';
23+
}
24+
25+
public function getDeveloperUrl()
26+
{
27+
return 'http://sgroup.com.au';
28+
}
29+
30+
public function getSettingsHtml()
31+
{
32+
return craft()->templates->render('imageresizer/settings', array(
33+
'settings' => $this->getSettings(),
34+
));
35+
}
36+
37+
protected function defineSettings()
38+
{
39+
return array(
40+
'enabled' => array( AttributeType::Bool, 'default' => true ),
41+
'imageWidth' => array( AttributeType::Number, 'default' => '2048' ),
42+
'imageHeight' => array( AttributeType::Number, 'default' => '2048' ),
43+
);
44+
}
45+
46+
47+
/* --------------------------------------------------------------
48+
* HOOKS
49+
* ------------------------------------------------------------ */
50+
51+
public function init()
52+
{
53+
craft()->on('assets.onBeforeSaveAsset', function(Event $event) {
54+
if (craft()->imageResizer->getSettings()->enabled) {
55+
$asset = $event->params['asset'];
56+
57+
// Only process if it's a new asset being saved - and if its actually an image
58+
if ($event->params['isNewAsset'] && $asset->kind == 'image') {
59+
craft()->imageResizer->resize($asset);
60+
}
61+
}
62+
});
63+
}
64+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
namespace Craft;
3+
4+
class ImageResizerService extends BaseApplicationComponent
5+
{
6+
// Public Methods
7+
// =========================================================================
8+
9+
public function getPlugin()
10+
{
11+
return craft()->plugins->getPlugin('imageResizer');
12+
}
13+
14+
public function getSettings()
15+
{
16+
return $this->getPlugin()->getSettings();
17+
}
18+
19+
public function resize($asset)
20+
{
21+
// Get the full path of the asset we want to resize
22+
$path = $this->_getImagePath($asset);
23+
$image = craft()->images->loadImage($path);
24+
25+
// Our maximum width/height for assets from plugin settings
26+
$imageWidth = $this->getSettings()->imageWidth;
27+
$imageHeight = $this->getSettings()->imageHeight;
28+
29+
// Lets check to see if this image needs resizing. Split into two steps to ensure
30+
// proper aspect ratio is preserved and no upscaling occurs.
31+
32+
if ($image->getWidth() > $imageWidth) {
33+
$this->_resizeImage($image, $imageWidth, null);
34+
}
35+
36+
if ($image->getHeight() > $imageHeight) {
37+
$this->_resizeImage($image, null, $imageHeight);
38+
}
39+
40+
$image->saveAs($path);
41+
}
42+
43+
44+
// Private Methods
45+
// =========================================================================
46+
47+
private function _getImagePath($asset)
48+
{
49+
// Get the full path for the asset being uploaded
50+
$source = $asset->getSource();
51+
52+
// Can only deal with local assets for now
53+
if ($source->type != 'Local') {
54+
return true;
55+
}
56+
57+
$sourcePath = $source->settings['path'];
58+
$folderPath = $asset->getFolder()->path;
59+
60+
return $sourcePath . $folderPath . $asset->filename;
61+
}
62+
63+
private function _resizeImage(&$image, $width, $height)
64+
{
65+
// Calculate the missing width/height for the asset - ensure aspect ratio is maintained
66+
$dimensions = ImageHelper::calculateMissingDimension($width, $height, $image->getWidth(), $image->getHeight());
67+
68+
$image->resize($dimensions[0], $dimensions[1]);
69+
}
70+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{% import "_includes/forms" as forms %}
2+
3+
{{ forms.lightswitchField({
4+
label: 'Enabled' | t,
5+
instructions: 'Whether uploaded images should be resized or not.' | t,
6+
id: 'enabled',
7+
name: 'enabled',
8+
on: settings.enabled,
9+
first: true,
10+
}) }}
11+
12+
{{ forms.textField({
13+
label: 'Image Width' | t,
14+
instructions: 'The maximum width in pixels allowed for uploaded images.' | t,
15+
id: 'imageWidth',
16+
name: 'imageWidth',
17+
value: settings.imageWidth,
18+
size: 10,
19+
}) }}
20+
21+
{{ forms.textField({
22+
label: 'Image Height' | t,
23+
instructions: 'The maximum height in pixels allowed for uploaded images.' | t,
24+
id: 'imageHeight',
25+
name: 'imageHeight',
26+
value: settings.imageHeight,
27+
size: 10,
28+
}) }}
29+
30+
<hr>

0 commit comments

Comments
 (0)