Skip to content

Commit 8be45b6

Browse files
committed
Merge branch 'release/1.4.22' into v1
2 parents 3528652 + ca1fcd9 commit 8be45b6

File tree

8 files changed

+60
-29
lines changed

8 files changed

+60
-29
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# ImageOptimize Changelog
22

3+
## 1.4.22 - 2018.03.02
4+
### Changed
5+
* Using Image Optimize via Twig should have less of a performance hit now, since all placeholder image/color palette generation is disabled
6+
* Fixed deprecation errors from Craft CMS 3 RC13
7+
38
## 1.4.21 - 2018.02.27
49
### Changed
510
* Show a warning if people try to add an OptimizedImages field in a Matrix block

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ If you plan to do this manually via the above console commands, you can disable
174174

175175
If you wish to dynamically create Optimized Image Variants in your templates without having to use the Field.
176176

177-
**N.B.:** If you create the Optimized Image Variants in your templates, the image transforms, placeholder images, and color palette extraction will all be done at pageload time. This means you'll miss out on the advantages of using the OptimizedImages field, where all of that computation is done when an Asset is saved.
177+
**N.B.:** We recommend _against_ using Image Optimize via Twig. If you create the Optimized Image Variants in your templates, the image transforms, placeholder images, and color palette extraction will all be done at pageload time. This means you'll miss out on the advantages of using the OptimizedImages field, where all of that computation is done when an Asset is saved.
178178

179179
To create Optimized Image Variants dynamically in your templates, you can do:
180180

@@ -247,7 +247,7 @@ You can create as many Optimized Image Variants as you like, by just including a
247247

248248
The `optimizedImages` object that is returned to you can be used in your templates as described in the *Displaying images on the frontend* section.
249249

250-
**N.B.:** Because they are lengthy operations, by default the generation of the dominant color palette and the generation of the placeholder silhouette are off by default. You can enable them via additional parameters passed down to `craft.imageOptimize.createOptimizedImages`:
250+
**N.B.:** Because they are lengthy operations, by default the generation of the dominant color palette and the generation of the placeholder silhouette are off. You can enable them via an additional parameter passed down to `craft.imageOptimize.createOptimizedImages`:
251251

252252
```
253253
{% set optimzedImages = craft.imageOptimize.createOptimizedImages(
@@ -264,12 +264,11 @@ The `optimizedImages` object that is returned to you can be used in your templat
264264
},
265265
],
266266
true,
267-
true,
268267
) %}
269268
270269
```
271270

272-
The third parameter is the `createColorPalette` setting, the fourth parameter is the `createPlaceholderSilhouettes` setting.
271+
The third parameter is the `generatePlacholders` setting, which disables generating all placeholders and dominant color palette extraction.
273272

274273
### Displaying images on the frontend
275274

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nystudio107/craft-imageoptimize",
33
"description": "Automatically create & optimize responsive image transforms, using either native Craft transforms or a service like Imgix, with zero template changes.",
44
"type": "craft-plugin",
5-
"version": "1.4.21",
5+
"version": "1.4.22",
66
"keywords": [
77
"craft",
88
"cms",

src/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
// Should image variant be created on Asset save (aka BeforePageLoad)
4747
'generateTransformsBeforePageLoad' => true,
4848

49+
// Set to false to disable all placeholder generation
50+
'generatePlacholders' => true,
51+
4952
// Controls whether a dominant color palette should be created for image variants
5053
// It takes a bit of time, so if you never plan to use it, you can turn it off
5154
'createColorPalette' => true,

src/models/OptimizedImage.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ public function placeholderImage()
285285
$header = 'data:image/jpeg;base64,';
286286
if (!empty($this->placeholder)) {
287287
$content = $this->placeholder;
288+
} else {
289+
// At least return something
290+
return $this->defaultPlaceholderImage();
288291
}
289292

290293
return Template::raw($header . rawurlencode($content));
@@ -337,6 +340,9 @@ public function placeholderSilhouette()
337340
$header = 'data:image/svg+xml,';
338341
if (!empty($this->placeholderSvg)) {
339342
$content = $this->placeholderSvg;
343+
} else {
344+
// At least return something
345+
return $this->defaultPlaceholderImage();
340346
}
341347

342348
return Template::raw($header . $content);
@@ -484,4 +490,18 @@ protected function getSrcsetFromArray(array $array, bool $dpr = false): string
484490

485491
return $srcset;
486492
}
493+
494+
/**
495+
* Return a default placeholder image
496+
*
497+
* @return \Twig_Markup
498+
*/
499+
protected function defaultPlaceholderImage(): \Twig_Markup
500+
{
501+
$width = 1;
502+
$height = 1;
503+
$color = '#CCC';
504+
505+
return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color));
506+
}
487507
}

src/models/Settings.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class Settings extends Model
6363
*/
6464
public $generateTransformsBeforePageLoad = true;
6565

66+
/**
67+
* @var bool Set to false to disable all placeholder generation
68+
*/
69+
public $generatePlacholders = true;
70+
6671
/**
6772
* @var bool Controls whether a dominant color palette should be created
6873
* for image variants It takes a bit of time, so if you never plan to

src/services/OptimizedImages.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,28 @@ public function populateOptimizedImageModel(Asset $asset, $variants, OptimizedIm
152152
protected function generatePlaceholders(Asset $element, OptimizedImage $model, $aspectRatio)
153153
{
154154
$settings = ImageOptimize::$plugin->getSettings();
155-
$placeholder = ImageOptimize::$plugin->placeholder;
156-
if ($element->focalPoint) {
157-
$position = $element->getFocalPoint();
158-
} else {
159-
$position = 'center-center';
160-
}
161-
$tempPath = $placeholder->createTempPlaceholderImage($element, $aspectRatio, $position);
162-
if (!empty($tempPath)) {
163-
// Generate our placeholder image
164-
$model->placeholder = $placeholder->generatePlaceholderImage($tempPath, $aspectRatio, $position);
165-
// Generate the color palette for the image
166-
if ($settings->createColorPalette) {
167-
$model->colorPalette = $placeholder->generateColorPalette($tempPath);
155+
if ($settings->generatePlacholders) {
156+
$placeholder = ImageOptimize::$plugin->placeholder;
157+
if ($element->focalPoint) {
158+
$position = $element->getFocalPoint();
159+
} else {
160+
$position = 'center-center';
168161
}
169-
// Generate the Potrace SVG
170-
if ($settings->createPlaceholderSilhouettes) {
171-
$model->placeholderSvg = $placeholder->generatePlaceholderSvg($tempPath);
162+
$tempPath = $placeholder->createTempPlaceholderImage($element, $aspectRatio, $position);
163+
if (!empty($tempPath)) {
164+
// Generate our placeholder image
165+
$model->placeholder = $placeholder->generatePlaceholderImage($tempPath, $aspectRatio, $position);
166+
// Generate the color palette for the image
167+
if ($settings->createColorPalette) {
168+
$model->colorPalette = $placeholder->generateColorPalette($tempPath);
169+
}
170+
// Generate the Potrace SVG
171+
if ($settings->createPlaceholderSilhouettes) {
172+
$model->placeholderSvg = $placeholder->generatePlaceholderSvg($tempPath);
173+
}
174+
// Get rid of our placeholder image
175+
@unlink($tempPath);
172176
}
173-
// Get rid of our placeholder image
174-
@unlink($tempPath);
175177
}
176178
}
177179

src/variables/ImageOptimizeVariable.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,18 @@ public function placeholderBox($width, $height, $color = null)
4343
/**
4444
* @param Asset $asset
4545
* @param array $variants
46-
* @param bool $createColorPalette
47-
* @param bool $createPlaceholderSilhouettes
46+
* @param bool $generatePlacholders
4847
*
4948
* @return OptimizedImage|null
5049
*/
5150
public function createOptimizedImages(
5251
Asset $asset,
5352
$variants = null,
54-
$createColorPalette = false,
55-
$createPlaceholderSilhouettes = false
53+
$generatePlacholders = false
5654
) {
5755
// Override our settings for lengthy operations, since we're doing this via Twig
5856
$settings = ImageOptimize::$plugin->getSettings();
59-
$settings->createColorPalette = $createColorPalette;
60-
$settings->createPlaceholderSilhouettes = $createPlaceholderSilhouettes;
57+
$settings->generatePlacholders = $generatePlacholders;
6158

6259
return ImageOptimize::$plugin->optimizedImages->createOptimizedImages($asset, $variants);
6360
}

0 commit comments

Comments
 (0)