diff --git a/CHANGELOG.md b/CHANGELOG.md index 78f4b3d..f81bc10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 2.0.1 - 2018-10-22 +### Added +- Added template transforms + ## 2.0.0 - 2018-10-22 ### Added - Initial release diff --git a/README.md b/README.md index c2d548f..28ba31b 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,37 @@ return [ ]; ``` +You can also use a Twig template to setup your transforms: + +```php + [ + [ + 'template' => '_imager-pretransform', + ], + ] +]; +``` + +The Twig template will be passed the variables `asset` and `pretransform`. You can check if `pretransform` is defined if you need to conditionally do transforms in a certain way. + +__imager-pretransform.twig:_ +```twig +{% set transforms = [ + { width: 800 }, + { width: 400, height: 400 }, +] %} + +{% if pretransform is defined %} + {% do craft.imager.transformImage(asset, transforms) %} +{% else %} + {# Your normal image partial #} +{% endif %} +``` + +This way you can keep your transforms in one place. + You should also set Imager's cache duration to a long time, say 1 year: In imager.php: diff --git a/composer.json b/composer.json index e81f9b7..c3424e4 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "superbig/craft-imagerpretransform", "description": "Pretransform any Assets on save, with Imager", "type": "craft-plugin", - "version": "2.0.0", + "version": "2.0.1", "keywords": [ "craft", "cms", diff --git a/src/services/ImagerPretransformService.php b/src/services/ImagerPretransformService.php index 3c8679a..281cf25 100644 --- a/src/services/ImagerPretransformService.php +++ b/src/services/ImagerPretransformService.php @@ -13,6 +13,7 @@ use aelvan\imager\Imager; use craft\base\ElementInterface; use craft\elements\Asset; +use craft\web\View; use superbig\imagerpretransform\ImagerPretransform; use Craft; @@ -49,7 +50,9 @@ public function onSaveAsset(Asset $asset) * @param Asset $asset * * @return array|bool|null + * @throws \Twig_Error_Loader * @throws \aelvan\imager\exceptions\ImagerException + * @throws \yii\base\Exception * @throws \yii\base\InvalidConfigException */ public function transformAsset(Asset $asset) @@ -78,7 +81,22 @@ public function transformAsset(Asset $asset) unset($transforms['configOverrides']); } - return Imager::$plugin->imager->transformImage($asset, $transforms, $transformDefaults, $configOverrides); + // Get template transforms + $templateTransforms = array_filter($transforms, function($transform) { + return isset($transform['template']); + }); + + $imagerTransforms = array_filter($transforms, function($transform) { + return !isset($transform['template']); + }); + + if (!empty($templateTransforms)) { + $this->renderTransformTemplates($asset, $templateTransforms); + } + + if (!empty($imagerTransforms)) { + Imager::$plugin->imager->transformImage($asset, $imagerTransforms, $transformDefaults, $configOverrides); + } } } @@ -100,6 +118,27 @@ public function getTransforms(Asset $asset, $volumeHandle = null) return $transforms; } + /** + * @param Asset $asset + * @param array $transforms + * + * @throws \Twig_Error_Loader + * @throws \yii\base\Exception + */ + public function renderTransformTemplates(Asset $asset, $transforms = []) + { + $view = Craft::$app->getView(); + $oldMode = $view->getTemplateMode(); + + $view->setTemplateMode(View::TEMPLATE_MODE_SITE); + + foreach ($transforms as $transform) { + $view->renderTemplate($transform['template'], ['asset' => $asset, 'pretransform' => true]); + } + + $view->setTemplateMode($oldMode); + } + public function shouldTransform(ElementInterface $element): bool { return $element instanceof Asset && $element->kind === 'image' && ($element->extension !== 'svg' && $element->mimeType !== 'image/svg+xml');