1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ImageOptimize plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Automatically optimize images after they've been transformed |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\imageoptimize\imagetransforms; |
12
|
|
|
|
13
|
|
|
use nystudio107\imageoptimize\ImageOptimize; |
14
|
|
|
|
15
|
|
|
use craft\elements\Asset; |
16
|
|
|
use craft\helpers\ArrayHelper; |
17
|
|
|
use craft\helpers\UrlHelper; |
18
|
|
|
use craft\models\AssetTransform; |
19
|
|
|
use Thumbor\Url\Builder as UrlBuilder; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
|
22
|
|
|
use Craft; |
23
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @author nystudio107 |
|
|
|
|
26
|
|
|
* @package ImageOptimize |
|
|
|
|
27
|
|
|
* @since 1.0.0 |
|
|
|
|
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
class ThumborImageTransform extends ImageTransform implements ImageTransformInterface |
30
|
|
|
{ |
31
|
|
|
// Static Methods |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
/** |
|
|
|
|
35
|
|
|
* @param Asset $asset |
|
|
|
|
36
|
|
|
* @param AssetTransform|null $transform |
|
|
|
|
37
|
|
|
* @param array $params |
|
|
|
|
38
|
|
|
* |
39
|
|
|
* @return string|null |
40
|
|
|
* @throws \yii\base\Exception |
41
|
|
|
* @throws \yii\base\InvalidConfigException |
42
|
|
|
*/ |
43
|
|
|
public static function getTransformUrl(Asset $asset, $transform, array $params = []) |
44
|
|
|
{ |
45
|
|
|
return (string) self::getUrlBuilderForTransform($asset, $transform, $params); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
|
|
|
|
49
|
|
|
* @param string $url |
|
|
|
|
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public static function getWebPUrl(string $url, Asset $asset, $transform, array $params = []): string |
|
|
|
|
54
|
|
|
{ |
|
|
|
|
55
|
|
|
$builder = self::getUrlBuilderForTransform($asset, $transform, $params) |
56
|
|
|
->addFilter('format', 'webp'); |
57
|
|
|
|
58
|
|
|
return (string) $builder; |
59
|
|
|
} |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
|
|
|
|
62
|
|
|
* @param string $url |
|
|
|
|
63
|
|
|
* @param array $params |
|
|
|
|
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public static function purgeUrl(string $url, array $params = []): bool |
68
|
|
|
{ |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
|
|
|
|
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public static function getTransformParams(): array |
76
|
|
|
{ |
77
|
|
|
$settings = ImageOptimize::$plugin->getSettings(); |
78
|
|
|
$params = [ |
79
|
|
|
'baseUrl' => $settings->thumborBaseUrl, |
80
|
|
|
'securityKey' => $settings->thumborSecurityKey, |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
return $params; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
|
|
|
|
87
|
|
|
* @param Asset $asset |
|
|
|
|
88
|
|
|
* @param AssetTransform|null $transform |
|
|
|
|
89
|
|
|
* @param array $params |
|
|
|
|
90
|
|
|
* |
91
|
|
|
* @return UrlBuilder |
92
|
|
|
* @throws \yii\base\Exception |
93
|
|
|
* @throws \yii\base\InvalidConfigException |
94
|
|
|
*/ |
95
|
|
|
private static function getUrlBuilderForTransform(Asset $asset, $transform, array $params = []): UrlBuilder |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$assetUri = self::getAssetUri($asset); |
98
|
|
|
$baseUrl = $params['baseUrl']; |
99
|
|
|
$securityKey = $params['securityKey'] ?: null; |
100
|
|
|
$builder = UrlBuilder::construct($baseUrl, $securityKey, $assetUri); |
101
|
|
|
$settings = ImageOptimize::$plugin->getSettings(); |
102
|
|
|
|
103
|
|
|
if ($transform->mode === 'fit') { |
104
|
|
|
|
105
|
|
|
// https://thumbor.readthedocs.io/en/latest/usage.html#fit-in |
106
|
|
|
$builder->fitIn($transform->width, $transform->height); |
107
|
|
|
} elseif ($transform->mode === 'stretch') { |
108
|
|
|
$builder |
|
|
|
|
109
|
|
|
->resize($transform->width, $transform->height) |
110
|
|
|
->addFilter('upscale'); |
111
|
|
|
|
112
|
|
|
// https://github.com/thumbor/thumbor/issues/1123 |
113
|
|
|
Craft::warning('Thumbor has no equivalent to the "stretch" transform mode. The resulting image will be resized and cropped, but not stretched.', __METHOD__); |
114
|
|
|
} else { |
115
|
|
|
|
116
|
|
|
// https://thumbor.readthedocs.io/en/latest/usage.html#image-size |
117
|
|
|
$builder->resize($transform->width, $transform->height); |
118
|
|
|
|
119
|
|
|
if ($focalPoint = self::getFocalPoint($asset)) { |
120
|
|
|
|
121
|
|
|
// https://thumbor.readthedocs.io/en/latest/focal.html |
122
|
|
|
$builder->addFilter('focal', $focalPoint); |
123
|
|
|
} elseif (preg_match('/(top|center|bottom)-(left|center|right)/', $transform->position, $matches)) { |
124
|
|
|
$v = str_replace('center', 'middle', $matches[1]); |
125
|
|
|
$h = $matches[2]; |
126
|
|
|
|
127
|
|
|
// https://thumbor.readthedocs.io/en/latest/usage.html#horizontal-align |
128
|
|
|
$builder->valign($v)->halign($h); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// https://thumbor.readthedocs.io/en/latest/format.html |
133
|
|
|
if ($format = self::getFormat($transform)) { |
134
|
|
|
$builder->addFilter('format', $format); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// https://thumbor.readthedocs.io/en/latest/quality.html |
138
|
|
|
if ($quality = self::getQuality($transform)) { |
139
|
|
|
$builder->addFilter('quality', $quality); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (property_exists($transform, 'interlace')) { |
143
|
|
|
Craft::warning('Thumbor enables progressive JPEGs on the server-level, not as a request option. See https://thumbor.readthedocs.io/en/latest/jpegtran.html', __METHOD__); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($settings->autoSharpenScaledImages) { |
147
|
|
|
|
148
|
|
|
// See if the image has been scaled >= 50% |
149
|
|
|
$widthScale = $asset->getWidth() / ($transform->width ?? $asset->getWidth()); |
150
|
|
|
$heightScale = $asset->getHeight() / ($transform->height ?? $asset->getHeight()); |
151
|
|
|
if (($widthScale >= 2.0) || ($heightScale >= 2.0)) { |
152
|
|
|
|
153
|
|
|
// https://thumbor.readthedocs.io/en/latest/sharpen.html |
154
|
|
|
$builder->addFilter('sharpen', .5, .5, 'true'); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $builder; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
|
|
|
|
162
|
|
|
* @return string|null |
163
|
|
|
*/ |
164
|
|
|
private static function getFocalPoint(Asset $asset) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
$focalPoint = $asset->getFocalPoint(); |
167
|
|
|
|
168
|
|
|
if (!$focalPoint) { |
169
|
|
|
return null; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$box = array_map('intval', [ |
|
|
|
|
173
|
|
|
'top' => $focalPoint['y'] * $asset->height - 1, |
174
|
|
|
'left' => $focalPoint['x'] * $asset->width - 1, |
175
|
|
|
'bottom' => $focalPoint['y'] * $asset->height + 1, |
176
|
|
|
'right' => $focalPoint['x'] * $asset->width + 1, |
177
|
|
|
]); |
|
|
|
|
178
|
|
|
|
179
|
|
|
return implode('', [ |
|
|
|
|
180
|
|
|
$box['top'], |
181
|
|
|
'x', |
182
|
|
|
$box['left'], |
183
|
|
|
':', |
184
|
|
|
$box['bottom'], |
185
|
|
|
'x', |
186
|
|
|
$box['right'], |
187
|
|
|
]); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
|
|
|
|
191
|
|
|
* @param AssetTransform|null $transform |
|
|
|
|
192
|
|
|
* |
193
|
|
|
* @return string|null |
194
|
|
|
*/ |
195
|
|
|
private static function getFormat($transform) |
|
|
|
|
196
|
|
|
{ |
197
|
|
|
$format = str_replace( |
198
|
|
|
['Auto', 'jpg'], |
199
|
|
|
['', 'jpeg'], |
200
|
|
|
$transform->format |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
return $format ?: null; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
|
|
|
|
207
|
|
|
* @param AssetTransform|null $transform |
|
|
|
|
208
|
|
|
* |
209
|
|
|
* @return int |
210
|
|
|
*/ |
211
|
|
|
private static function getQuality($transform) |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
return $transform->quality ?? Craft::$app->getConfig()->getGeneral()->defaultImageQuality; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|