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): string |
54
|
|
|
{ |
55
|
|
|
// TODO: waiting for @khalwat :) |
56
|
|
|
return $url; |
57
|
|
|
|
58
|
|
|
$builder = self::getUrlBuilderForTransform($asset, $transform, $params) |
|
|
|
|
59
|
|
|
->addFilter('format', 'webp'); |
60
|
|
|
|
61
|
|
|
return (string) $builder; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
|
|
|
|
65
|
|
|
* @param string $url |
|
|
|
|
66
|
|
|
* @param array $params |
|
|
|
|
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public static function purgeUrl(string $url, array $params = []): bool |
71
|
|
|
{ |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
|
|
|
|
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public static function getTransformParams(): array |
79
|
|
|
{ |
80
|
|
|
$settings = ImageOptimize::$plugin->getSettings(); |
81
|
|
|
$params = [ |
82
|
|
|
'baseUrl' => $settings->thumborBaseUrl, |
83
|
|
|
'securityKey' => $settings->thumborSecurityKey, |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
return $params; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
|
|
|
|
90
|
|
|
* @param Asset $asset |
|
|
|
|
91
|
|
|
* @param AssetTransform|null $transform |
|
|
|
|
92
|
|
|
* @param array $params |
|
|
|
|
93
|
|
|
* |
94
|
|
|
* @return UrlBuilder |
95
|
|
|
* @throws \yii\base\Exception |
96
|
|
|
* @throws \yii\base\InvalidConfigException |
97
|
|
|
*/ |
98
|
|
|
private static function getUrlBuilderForTransform(Asset $asset, $transform, array $params = []): UrlBuilder |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$assetUri = self::getAssetUri($asset); |
101
|
|
|
$baseUrl = $params['baseUrl']; |
102
|
|
|
$securityKey = $params['securityKey'] ?: null; |
103
|
|
|
$builder = UrlBuilder::construct($baseUrl, $securityKey, $assetUri); |
104
|
|
|
$settings = ImageOptimize::$plugin->getSettings(); |
105
|
|
|
|
106
|
|
|
if ($transform->mode === 'fit') { |
107
|
|
|
|
108
|
|
|
// https://thumbor.readthedocs.io/en/latest/usage.html#fit-in |
109
|
|
|
$builder->fitIn($transform->width, $transform->height); |
110
|
|
|
} elseif ($transform->mode === 'stretch') { |
111
|
|
|
$builder |
|
|
|
|
112
|
|
|
->resize($transform->width, $transform->height) |
113
|
|
|
->addFilter('upscale'); |
114
|
|
|
|
115
|
|
|
// https://github.com/thumbor/thumbor/issues/1123 |
116
|
|
|
Craft::warning('Thumbor has no equivalent to the "stretch" transform mode. The resulting image will be resized and cropped, but not stretched.', __METHOD__); |
117
|
|
|
} else { |
118
|
|
|
|
119
|
|
|
// https://thumbor.readthedocs.io/en/latest/usage.html#image-size |
120
|
|
|
$builder->resize($transform->width, $transform->height); |
121
|
|
|
|
122
|
|
|
if ($focalPoint = self::getFocalPoint($asset)) { |
123
|
|
|
|
124
|
|
|
// https://thumbor.readthedocs.io/en/latest/focal.html |
125
|
|
|
$builder->addFilter('focal', $focalPoint); |
126
|
|
|
} elseif (preg_match('/(top|center|bottom)-(left|center|right)/', $transform->position, $matches)) { |
127
|
|
|
$v = str_replace('center', 'middle', $matches[1]); |
128
|
|
|
$h = $matches[2]; |
129
|
|
|
|
130
|
|
|
// https://thumbor.readthedocs.io/en/latest/usage.html#horizontal-align |
131
|
|
|
$builder->valign($v)->halign($h); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// https://thumbor.readthedocs.io/en/latest/format.html |
136
|
|
|
if ($format = self::getFormat($transform)) { |
137
|
|
|
$builder->addFilter('format', $format); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// https://thumbor.readthedocs.io/en/latest/quality.html |
141
|
|
|
if ($quality = self::getQuality($transform)) { |
142
|
|
|
$builder->addFilter('quality', $quality); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (property_exists($transform, 'interlace')) { |
146
|
|
|
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__); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ($settings->autoSharpenScaledImages) { |
150
|
|
|
|
151
|
|
|
// See if the image has been scaled >= 50% |
152
|
|
|
$widthScale = $asset->getWidth() / ($transform->width ?? $asset->getWidth()); |
153
|
|
|
$heightScale = $asset->getHeight() / ($transform->height ?? $asset->getHeight()); |
154
|
|
|
if (($widthScale >= 2.0) || ($heightScale >= 2.0)) { |
155
|
|
|
|
156
|
|
|
// https://thumbor.readthedocs.io/en/latest/sharpen.html |
157
|
|
|
$builder->addFilter('sharpen', .5, .5, 'true'); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $builder; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
|
|
|
|
165
|
|
|
* @return string|null |
166
|
|
|
*/ |
167
|
|
|
private static function getFocalPoint(Asset $asset) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$focalPoint = $asset->getFocalPoint(); |
170
|
|
|
|
171
|
|
|
if (!$focalPoint) { |
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$box = array_map('intval', [ |
|
|
|
|
176
|
|
|
'top' => $focalPoint['y'] * $asset->height - 1, |
177
|
|
|
'left' => $focalPoint['x'] * $asset->width - 1, |
178
|
|
|
'bottom' => $focalPoint['y'] * $asset->height + 1, |
179
|
|
|
'right' => $focalPoint['x'] * $asset->width + 1, |
180
|
|
|
]); |
|
|
|
|
181
|
|
|
|
182
|
|
|
return implode('', [ |
|
|
|
|
183
|
|
|
$box['top'], |
184
|
|
|
'x', |
185
|
|
|
$box['left'], |
186
|
|
|
':', |
187
|
|
|
$box['bottom'], |
188
|
|
|
'x', |
189
|
|
|
$box['right'], |
190
|
|
|
]); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
|
|
|
|
194
|
|
|
* @param AssetTransform|null $transform |
|
|
|
|
195
|
|
|
* |
196
|
|
|
* @return string|null |
197
|
|
|
*/ |
198
|
|
|
private static function getFormat($transform) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
$format = str_replace( |
201
|
|
|
['Auto', 'jpg'], |
202
|
|
|
['', 'jpeg'], |
203
|
|
|
$transform->format |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
return $format ?: null; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
|
|
|
|
210
|
|
|
* @param AssetTransform|null $transform |
|
|
|
|
211
|
|
|
* |
212
|
|
|
* @return int |
213
|
|
|
*/ |
214
|
|
|
private static function getQuality($transform) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
return $transform->quality ?? Craft::$app->getConfig()->getGeneral()->defaultImageQuality; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|