Skip to content

Commit

Permalink
### Changed
Browse files Browse the repository at this point in the history
* `hasSettings` -> `hasCpSettings` for Craft 3 beta 8 compatibility
* Added Craft 3 beta 8 compatible settings
* Modified config service calls for Craft 3 beta 8
  • Loading branch information
khalwat committed Mar 25, 2017
1 parent 7c51250 commit 14cd5bb
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ImageOptim Changelog

## 1.0.5 - 2017.03.24
### Changed
* `hasSettings` -> `hasCpSettings` for Craft 3 beta 8 compatibility
* Added Craft 3 beta 8 compatible settings
* Modified config service calls for Craft 3 beta 8

## 1.0.4 - 2017.03.12
### Added
- Added code inspection typehinting for the plugin & services
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nystudio107/craft3-imageoptim",
"description": "Automatically optimize images after they've been transformed",
"type": "craft-plugin",
"version": "1.0.4",
"version": "1.0.5",
"keywords": [
"craft",
"cms",
Expand Down Expand Up @@ -34,7 +34,7 @@
"name": "ImageOptim",
"handle": "imageOptim",
"schemaVersion": "1.0.0",
"hasSettings": false,
"hasCpSettings": false,
"hasCpSection": false,
"changelogUrl": "https://raw.githubusercontent.com/nystudio107/craft3-imageoptim/master/CHANGELOG.md",
"components": {
Expand Down
9 changes: 8 additions & 1 deletion src/ImageOptim.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ function (GenerateTransformEvent $event) {
}
);

Craft::info('ImageOptim ' . Craft::t('imageoptim', 'plugin loaded'), __METHOD__);
Craft::info(
Craft::t(
'imageoptim',
'{name} plugin loaded',
['name' => $this->name]
),
__METHOD__
);
}
}
7 changes: 3 additions & 4 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
/**
* 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.
* This file exists only as a template for the ImageOptim settings.
* It does nothing on its own.
*
* Don't edit this file, instead copy it to 'craft/config' as 'imageoptim.php'
* and make your changes there.
* and make your changes there to override default settings.
*
* 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
Expand Down
104 changes: 104 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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\models;

use craft\base\Model;

/**
* ImageOptim Settings model
*
* @author nystudio107
* @package ImageOptim
* @since 1.0.0
*/
class Settings extends Model
{
// Public Properties
// =========================================================================

/**
* Active image processors
*
* @var array
*/
public $activeImageProcessors = [
'jpg' => [
'jpegoptim',
],
'png' => [
'optipng',
],
'svg' => [
'svgo',
],
'gif' => [
'gifsicle',
],
];

/**
* Preset image processors
*
* @var array
*/
public $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' => '',
],
// gif optimizers
'gifsicle' => [
'commandPath' => '/usr/bin/gifsicle',
'commandOptions' => '-O3 -k 256',
],
];

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

/**
* @inheritdoc
*/
public function rules()
{
return [
['activeImageProcessors', 'required'],
['imageProcessors', 'required'],
];
}
}
5 changes: 3 additions & 2 deletions src/services/Optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ public function saveTransformToTempFile(AssetTransformIndex $index, Image $image
*/
public function optimizeImage(AssetTransformIndex $index, string $tempPath)
{
$settings = ImageOptim::$plugin->getSettings();
// Get the active processors for the transform format
$activeImageProcessors = Craft::$app->config->get('activeImageProcessors', 'imageoptim');
$activeImageProcessors = $settings['activeImageProcessors'];
$fileFormat = $index->detectedFormat;
if (!empty($activeImageProcessors[$fileFormat])) {
// Iterate through all of the processors for this format
$imageProcessors = Craft::$app->config->get('imageProcessors', 'imageoptim');
$imageProcessors = $settings['imageProcessors'];
foreach ($activeImageProcessors[$fileFormat] as $processor) {
if (!empty($imageProcessors[$processor])) {
// Make sure the command exists
Expand Down
2 changes: 1 addition & 1 deletion src/translations/en/imageoptim.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @since 1.0.0
*/
return [
'plugin loaded' => 'plugin loaded',
'ImageOptim plugin loaded' => 'ImageOptim plugin loaded',
'does not exist' => 'does not exist',
'Original' => 'Original',
'Optimized' => 'Optimized',
Expand Down

0 comments on commit 14cd5bb

Please sign in to comment.