Skip to content

Commit

Permalink
* Initial push
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Mar 11, 2017
0 parents commit 1dc9ccc
Show file tree
Hide file tree
Showing 11 changed files with 480 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# COMPOSER
/vendor

# BUILD FILES
/bower_components/*
/node_modules/*
/build/*

# MISC FILES
.cache
.DS_Store
.idea
.project
.settings
*.esproj
*.sublime-workspace
*.sublime-project
*.tmproj
*.tmproject
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ImageOptim Changelog

## 1.0.0 - 2017.03.11
### Added
- Initial release
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2017 nystudio107

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# ImageOptim plugin for Craft CMS 3.x

Automatically optimize images after they've been transformed

## Installation

To install ImageOptim, follow these steps:

1. Install with Composer via `composer require nystudio107/craft3-imageoptim`
2. Install plugin in the Craft Control Panel under Settings > Plugins

ImageOptim works on Craft 3.x.

ImageOptim won't do anything on its own; you'll need to also install the image optimization tools of your choice. Here's how to install a few on Ubuntu 16.04:

* **jpegoptim** - `sudo apt-get install jpegoptim`
* **mozjpeg** - [Installing mozjpeg on Ubuntu 16.04 (Forge)](https://nystudio107.com/blog/installing-mozjpeg-on-ubuntu-16-04-forge)
* **optipng** - `sudo apt-get install optipng`
* **svgo** - `sudo npm install -g svgo`

## ImageOptim Overview

ImageOptim allows you to optimize the images created by Craft 3's Image Transforms by automatically running a variety of image optimization tools on them. As configured by default, all of these are _lossless_ image optimizations that remove metadata and otherwise optimize the images without change their appearance in any way.

It's important to create optimized images for frontend delivery, especially for mobile devices. If you want to learn more about it, read the [Creating Optimized Images in Craft CMS](https://nystudio107.com/blog/creating-optimized-images-in-craft-cms) article.

Once ImageOptim is installed, optimized versions of image transforms are created with you having to do anything. This makes it great for client-proofing websites.

## Configuring ImageOptim

The only configuration for ImageOptim is in the `config.php` file, which is a multi-environment friendly way to store the default settings. Don't edit this file, instead copy it to `craft/config` as `imageoptim.php` and make your changes there.

The `activeProcessors` array lets you specify which of the image optimization tools to use for which file types.

The `imageProcessors` array specifies the path and options for each of the image optimization tools.

## Using ImageOptim

Once ImageOptim is set up and configured, there's nothing left to do. It just works.

## ImageOptim Roadmap

Some things to do, and ideas for potential features:

* Add support for addition image optimization tools

Brought to you by [nystudio107](https://nystudio107.com)
43 changes: 43 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "nystudio107/craft3-imageoptim",
"description": "Automatically optimize images after they've been transformed",
"type": "craft-plugin",
"version": "1.0.0",
"keywords": [
"craft",
"cms",
"craftcms",
"craft-plugin",
"imageoptim"
],
"support": {
"docs": "https://github.com/nystudio107/craft3-imageoptim/blob/master/README.md",
"issues": "https://github.com/nystudio107/craft3-imageoptim/issues"
},
"license": "MIT",
"authors": [
{
"name": "nystudio107",
"homepage": "https://nystudio107.com"
}
],
"require": {
},
"autoload": {
"psr-4": {
"nystudio107\\imageoptim\\": "src/"
}
},
"extra": {
"name": "ImageOptim",
"handle": "imageOptim",
"schemaVersion": "1.0.0",
"hasSettings": false,
"hasCpSection": false,
"changelogUrl": "https://raw.githubusercontent.com/nystudio107/craft3-imageoptim/master/CHANGELOG.md",
"components": {
"optimize": "nystudio107\\imageoptim\\services\\Optimize"
},
"class": "nystudio107\\imageoptim\\ImageOptim"
}
}
Binary file added resources/img/plugin-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions src/ImageOptim.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* ImageOptim plugin for Craft CMS 3.x
*
* Automatically optimize images after they've been transformed
*
* @link https://nystudio107.com
* @copyright Copyright (c) 2017 nystudio107
*/

namespace nystudio107\imageoptim;


use Craft;
use craft\base\Plugin;
use craft\services\AssetTransforms;
use craft\events\GenerateTransformEvent;

use yii\base\Event;

/**
* @author nystudio107
* @package ImageOptim
* @since 1.0.0
*/
class ImageOptim extends Plugin
{
// Static Properties
// =========================================================================

/**
* @var static
*/
public static $plugin;

// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function init()
{
parent::init();
self::$plugin = $this;

// Listen for image transform events
Event::on(
AssetTransforms::className(),
AssetTransforms::EVENT_GENERATE_TRANSFORM,
function (GenerateTransformEvent $event) {
// Save the transformed image to a temp file
$tempPath = ImageOptim::$plugin->optimize->saveTransformToTempFile(
$event->transformIndex,
$event->image
);
// Optimize the image
ImageOptim::$plugin->optimize->optimizeImage(
$event->transformIndex,
$tempPath
);
// Return the path to the optimized image to _createTransformForAsset()
$event->tempPath = $tempPath;
}
);

Craft::info('ImageOptim ' . Craft::t('imageoptim', 'plugin loaded'), __METHOD__);
}

// Protected Methods
// =========================================================================

}
76 changes: 76 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* ImageOptim plugin for Craft CMS 3.x
*
* Automatically optimize images after they've been transformed
*
* @link https://nystudio107.com
* @copyright Copyright (c) 2017 nystudio107
*/

/**
* ImageOptim config.php
*
* Completely optional configuration settings for ImageOptim if you want to
* customize some of its more esoteric behavior, or just want specific control
* over things.
*
* Don't edit this file, instead copy it to 'craft/config' as 'imageoptim.php'
* and make your changes there.
*
* Once copied to 'craft/config', this file will be multi-environment aware as
* well, so you can have different settings groups for each environment, just as
* you do for 'general.php'
*/

return [

// Active image processors
"activeProcessors" => [
"jpg" => [
"jpegoptim",
],
"png" => [
"optipng",
],
"svg" => [
"svgo",
],
],

// Preset image processors
"imageProcessors" => [
// jpeg optimizers
"jpegoptim" => [
"commandPath" => "/usr/bin/jpegoptim",
"commandOptions" => "-s",
],
"mozjpeg" => [
"commandPath" => "/usr/bin/mozjpeg",
"commandOptions" => "-optimize -copy none",
],
"jpegtran" => [
"commandPath" => "/usr/bin/jpegtran",
"commandOptions" => "-optimize -copy none",
],
// png optimizers
"optipng" => [
"commandPath" => "/usr/bin/optipng",
"commandOptions" => "-o7 -strip all",
],
"pngcrush" => [
"commandPath" => "/usr/bin/pngcrush",
"commandOptions" => "-brute -ow",
],
"pngquant" => [
"commandPath" => "/usr/bin/pngquant",
"commandOptions" => "--strip --skip-if-larger",
],
// svg optimizers
"svgo" => [
"commandPath" => "/usr/bin/svgo",
"commandOptions" => "",
],
],

];
67 changes: 67 additions & 0 deletions src/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1dc9ccc

Please sign in to comment.