Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Added template transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
sjelfull committed Nov 19, 2018
1 parent 470b198 commit 3d7bee1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,37 @@ return [
];
```

You can also use a Twig template to setup your transforms:

```php
<?php
return [
'transforms' => [
[
'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:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 40 additions & 1 deletion src/services/ImagerPretransformService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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');
Expand Down

0 comments on commit 3d7bee1

Please sign in to comment.