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) 2018 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\imageoptimize\imagetransforms; |
12
|
|
|
|
13
|
|
|
use nystudio107\imageoptimize\helpers\UrlHelper; |
14
|
|
|
|
15
|
|
|
use craft\base\SavableComponent; |
16
|
|
|
use craft\elements\Asset; |
17
|
|
|
use craft\helpers\FileHelper; |
18
|
|
|
use craft\helpers\StringHelper; |
19
|
|
|
use craft\models\AssetTransform; |
20
|
|
|
|
21
|
|
|
/** |
|
|
|
|
22
|
|
|
* @author nystudio107 |
|
|
|
|
23
|
|
|
* @package ImageOptimize |
|
|
|
|
24
|
|
|
* @since 1.5.0 |
|
|
|
|
25
|
|
|
*/ |
|
|
|
|
26
|
|
|
abstract class ImageTransform extends SavableComponent implements ImageTransformInterface |
27
|
|
|
{ |
28
|
|
|
// Traits |
29
|
|
|
// ========================================================================= |
30
|
|
|
|
31
|
|
|
use ImageTransformTrait; |
32
|
|
|
|
33
|
|
|
// Static Methods |
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* @return array |
38
|
|
|
* @throws \ReflectionException |
39
|
|
|
*/ |
40
|
|
|
public static function getTemplatesRoot(): array |
41
|
|
|
{ |
42
|
|
|
$reflect = new \ReflectionClass(static::class); |
43
|
|
|
$classPath = FileHelper::normalizePath( |
44
|
|
|
dirname($reflect->getFileName()) |
45
|
|
|
. '/../templates' |
46
|
|
|
) |
47
|
|
|
. DIRECTORY_SEPARATOR; |
48
|
|
|
$id = StringHelper::toKebabCase($reflect->getShortName()); |
49
|
|
|
|
50
|
|
|
return [ |
51
|
|
|
$id, $classPath |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Public Methods |
56
|
|
|
// ========================================================================= |
57
|
|
|
|
58
|
|
|
/** |
|
|
|
|
59
|
|
|
* @param Asset $asset |
|
|
|
|
60
|
|
|
* @param AssetTransform|null $transform |
|
|
|
|
61
|
|
|
* @param array $params |
|
|
|
|
62
|
|
|
* |
63
|
|
|
* @return string|null |
64
|
|
|
*/ |
65
|
|
|
public function getTransformUrl(Asset $asset, $transform, array $params = []) |
66
|
|
|
{ |
67
|
|
|
$url = null; |
68
|
|
|
|
69
|
|
|
return $url; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
|
|
|
|
73
|
|
|
* @param string $url |
|
|
|
|
74
|
|
|
* @param Asset $asset |
|
|
|
|
75
|
|
|
* @param AssetTransform|null $transform |
|
|
|
|
76
|
|
|
* @param array $params |
|
|
|
|
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getWebPUrl(string $url, Asset $asset, $transform, array $params = []): string |
81
|
|
|
{ |
82
|
|
|
return $url; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
|
|
|
|
86
|
|
|
* @param Asset $asset |
|
|
|
|
87
|
|
|
* @param array $params |
|
|
|
|
88
|
|
|
* |
89
|
|
|
* @return null|string |
90
|
|
|
*/ |
91
|
|
|
public function getPurgeUrl(Asset $asset, array $params = []) |
92
|
|
|
{ |
93
|
|
|
$url = null; |
94
|
|
|
|
95
|
|
|
return $url; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
|
|
|
|
99
|
|
|
* @param string $url |
|
|
|
|
100
|
|
|
* @param array $params |
|
|
|
|
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function purgeUrl(string $url, array $params = []): bool |
105
|
|
|
{ |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
|
|
|
|
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function getTransformParams(): array |
113
|
|
|
{ |
114
|
|
|
$params = [ |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
return $params; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
|
|
|
|
121
|
|
|
* @param Asset $asset |
|
|
|
|
122
|
|
|
* |
123
|
|
|
* @return mixed |
124
|
|
|
* @throws \yii\base\InvalidConfigException |
125
|
|
|
*/ |
126
|
|
|
public function getAssetUri(Asset $asset) |
127
|
|
|
{ |
128
|
|
|
$volume = $asset->getVolume(); |
129
|
|
|
$assetPath = $asset->getPath(); |
130
|
|
|
|
131
|
|
|
// Account for volume types with a subfolder setting |
132
|
|
|
// e.g. craftcms/aws-s3, craftcms/google-cloud |
133
|
|
|
if ($volume->subfolder ?? null) { |
|
|
|
|
134
|
|
|
return rtrim($volume->subfolder, '/').'/'.$assetPath; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $assetPath; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
|
|
|
|
141
|
|
|
* @param string $url |
|
|
|
|
142
|
|
|
*/ |
|
|
|
|
143
|
|
|
public function prefetchRemoteFile($url) |
144
|
|
|
{ |
145
|
|
|
// Get an absolute URL with protocol that curl will be happy with |
146
|
|
|
$url = UrlHelper::absoluteUrlWithProtocol($url); |
147
|
|
|
$ch = curl_init($url); |
148
|
|
|
curl_setopt_array($ch, [ |
|
|
|
|
149
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
150
|
|
|
CURLOPT_FOLLOWLOCATION => 1, |
151
|
|
|
CURLOPT_SSL_VERIFYPEER => 0, |
152
|
|
|
CURLOPT_NOBODY => 1, |
153
|
|
|
]); |
|
|
|
|
154
|
|
|
curl_exec($ch); |
155
|
|
|
curl_close($ch); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
|
|
|
|
159
|
|
|
* Append an extension a passed url or path |
160
|
|
|
* |
161
|
|
|
* @param $pathOrUrl |
|
|
|
|
162
|
|
|
* @param $extension |
|
|
|
|
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public function appendExtension($pathOrUrl, $extension): string |
167
|
|
|
{ |
168
|
|
|
$path = $this->decomposeUrl($pathOrUrl); |
169
|
|
|
$path_parts = pathinfo($path['path']); |
170
|
|
|
$new_path = $path_parts['filename'] . '.' . $path_parts['extension'] . $extension; |
171
|
|
|
if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== '.') { |
172
|
|
|
$new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path; |
173
|
|
|
$new_path = preg_replace('/([^:])(\/{2,})/', '$1/', $new_path); |
174
|
|
|
} |
175
|
|
|
$output = $path['prefix'] . $new_path . $path['suffix']; |
176
|
|
|
|
177
|
|
|
return $output; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// Protected Methods |
181
|
|
|
// ========================================================================= |
182
|
|
|
|
183
|
|
|
/** |
|
|
|
|
184
|
|
|
* Decompose a url into a prefix, path, and suffix |
185
|
|
|
* |
186
|
|
|
* @param $pathOrUrl |
|
|
|
|
187
|
|
|
* |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
protected function decomposeUrl($pathOrUrl): array |
191
|
|
|
{ |
192
|
|
|
$result = array(); |
193
|
|
|
|
194
|
|
|
if (filter_var($pathOrUrl, FILTER_VALIDATE_URL)) { |
195
|
|
|
$url_parts = parse_url($pathOrUrl); |
196
|
|
|
$result['prefix'] = $url_parts['scheme'] . '://' . $url_parts['host']; |
197
|
|
|
$result['path'] = $url_parts['path']; |
198
|
|
|
$result['suffix'] = ''; |
199
|
|
|
$result['suffix'] .= empty($url_parts['query']) ? '' : '?' . $url_parts['query']; |
200
|
|
|
$result['suffix'] .= empty($url_parts['fragment']) ? '' : '#' . $url_parts['fragment']; |
201
|
|
|
} else { |
202
|
|
|
$result['prefix'] = ''; |
203
|
|
|
$result['path'] = $pathOrUrl; |
204
|
|
|
$result['suffix'] = ''; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $result; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|