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.6.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
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
|
|
|
|
39
|
|
|
public static function displayName(): string |
40
|
|
|
{ |
41
|
|
|
return Craft::t('image-optimize', 'Generic Transform'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
|
|
|
|
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
|
|
|
|
47
|
|
|
public static function getTemplatesRoot(): array |
48
|
|
|
{ |
49
|
|
|
$reflect = new \ReflectionClass(static::class); |
50
|
|
|
$classPath = FileHelper::normalizePath( |
51
|
|
|
dirname($reflect->getFileName()) |
52
|
|
|
. '/../templates' |
53
|
|
|
) |
54
|
|
|
. DIRECTORY_SEPARATOR; |
55
|
|
|
$id = StringHelper::toKebabCase($reflect->getShortName()); |
56
|
|
|
|
57
|
|
|
return [$id, $classPath]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Public Methods |
61
|
|
|
// ========================================================================= |
62
|
|
|
|
63
|
|
|
/** |
|
|
|
|
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
|
|
|
|
66
|
|
|
public function getTransformUrl(Asset $asset, $transform) |
67
|
|
|
{ |
68
|
|
|
$url = null; |
69
|
|
|
|
70
|
|
|
return $url; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
|
|
|
|
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
|
|
|
|
76
|
|
|
public function getWebPUrl(string $url, Asset $asset, $transform): string |
77
|
|
|
{ |
78
|
|
|
return $url; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
|
|
|
|
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
|
|
|
|
84
|
|
|
public function getPurgeUrl(Asset $asset) |
85
|
|
|
{ |
86
|
|
|
$url = null; |
87
|
|
|
|
88
|
|
|
return $url; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
|
|
|
|
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
|
|
|
|
94
|
|
|
public function purgeUrl(string $url): bool |
95
|
|
|
{ |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
|
|
|
|
100
|
|
|
* @inheritdoc |
101
|
|
|
*/ |
|
|
|
|
102
|
|
|
public function getAssetUri(Asset $asset) |
103
|
|
|
{ |
104
|
|
|
$volume = $asset->getVolume(); |
105
|
|
|
$assetPath = $asset->getPath(); |
106
|
|
|
|
107
|
|
|
// Account for volume types with a subfolder setting |
108
|
|
|
// e.g. craftcms/aws-s3, craftcms/google-cloud |
109
|
|
|
if ($volume->subfolder ?? null) { |
|
|
|
|
110
|
|
|
return rtrim($volume->subfolder, '/').'/'.$assetPath; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $assetPath; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
|
|
|
|
117
|
|
|
* @param string $url |
|
|
|
|
118
|
|
|
*/ |
|
|
|
|
119
|
|
|
public function prefetchRemoteFile($url) |
120
|
|
|
{ |
121
|
|
|
// Get an absolute URL with protocol that curl will be happy with |
122
|
|
|
$url = UrlHelper::absoluteUrlWithProtocol($url); |
123
|
|
|
$ch = curl_init($url); |
124
|
|
|
curl_setopt_array($ch, [ |
|
|
|
|
125
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
126
|
|
|
CURLOPT_FOLLOWLOCATION => 1, |
127
|
|
|
CURLOPT_SSL_VERIFYPEER => 0, |
128
|
|
|
CURLOPT_NOBODY => 1, |
129
|
|
|
]); |
|
|
|
|
130
|
|
|
curl_exec($ch); |
|
|
|
|
131
|
|
|
curl_close($ch); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
|
|
|
|
135
|
|
|
* Append an extension a passed url or path |
136
|
|
|
* |
137
|
|
|
* @param $pathOrUrl |
|
|
|
|
138
|
|
|
* @param $extension |
|
|
|
|
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function appendExtension($pathOrUrl, $extension): string |
143
|
|
|
{ |
144
|
|
|
$path = $this->decomposeUrl($pathOrUrl); |
145
|
|
|
$path_parts = pathinfo($path['path']); |
146
|
|
|
$new_path = $path_parts['filename'] . '.' . $path_parts['extension'] . $extension; |
147
|
|
|
if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== '.') { |
148
|
|
|
$new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path; |
149
|
|
|
$new_path = preg_replace('/([^:])(\/{2,})/', '$1/', $new_path); |
150
|
|
|
} |
151
|
|
|
$output = $path['prefix'] . $new_path . $path['suffix']; |
152
|
|
|
|
153
|
|
|
return $output; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// Protected Methods |
157
|
|
|
// ========================================================================= |
158
|
|
|
|
159
|
|
|
/** |
|
|
|
|
160
|
|
|
* Decompose a url into a prefix, path, and suffix |
161
|
|
|
* |
162
|
|
|
* @param $pathOrUrl |
|
|
|
|
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
protected function decomposeUrl($pathOrUrl): array |
167
|
|
|
{ |
168
|
|
|
$result = array(); |
169
|
|
|
|
170
|
|
|
if (filter_var($pathOrUrl, FILTER_VALIDATE_URL)) { |
171
|
|
|
$url_parts = parse_url($pathOrUrl); |
172
|
|
|
$result['prefix'] = $url_parts['scheme'] . '://' . $url_parts['host']; |
173
|
|
|
$result['path'] = $url_parts['path']; |
174
|
|
|
$result['suffix'] = ''; |
175
|
|
|
$result['suffix'] .= empty($url_parts['query']) ? '' : '?' . $url_parts['query']; |
176
|
|
|
$result['suffix'] .= empty($url_parts['fragment']) ? '' : '#' . $url_parts['fragment']; |
177
|
|
|
} else { |
178
|
|
|
$result['prefix'] = ''; |
179
|
|
|
$result['path'] = $pathOrUrl; |
180
|
|
|
$result['suffix'] = ''; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $result; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|