|
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\helpers\UrlHelper; |
|
14
|
|
|
|
|
15
|
|
|
use craft\elements\Asset; |
|
16
|
|
|
use craft\helpers\Assets as AssetsHelper; |
|
17
|
|
|
use craft\models\AssetTransform; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author nystudio107 |
|
21
|
|
|
* @package ImageOptimize |
|
22
|
|
|
* @since 1.0.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class ImageTransform implements ImageTransformInterface |
|
25
|
|
|
{ |
|
26
|
|
|
// Public Static Methods |
|
27
|
|
|
// ========================================================================= |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param Asset $asset |
|
31
|
|
|
* @param AssetTransform|null $transform |
|
32
|
|
|
* @param array $params |
|
33
|
|
|
* |
|
34
|
|
|
* @return string|null |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function getTransformUrl(Asset $asset, $transform, array $params = []) |
|
37
|
|
|
{ |
|
38
|
|
|
$url = null; |
|
39
|
|
|
|
|
40
|
|
|
return $url; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $url |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function getWebPUrl(string $url): string |
|
49
|
|
|
{ |
|
50
|
|
|
return $url; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param Asset $asset |
|
55
|
|
|
* @param array $params |
|
56
|
|
|
* |
|
57
|
|
|
* @return null|string |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function getPurgeUrl(Asset $asset, array $params = []) |
|
60
|
|
|
{ |
|
61
|
|
|
$url = null; |
|
62
|
|
|
|
|
63
|
|
|
return $url; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $url |
|
68
|
|
|
* @param array $params |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function purgeUrl(string $url, array $params = []): bool |
|
73
|
|
|
{ |
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function getTransformParams(): array |
|
81
|
|
|
{ |
|
82
|
|
|
$params = [ |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
return $params; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param Asset $asset |
|
90
|
|
|
* |
|
91
|
|
|
* @return mixed |
|
92
|
|
|
* @throws \yii\base\InvalidConfigException |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function getAssetUri(Asset $asset) |
|
95
|
|
|
{ |
|
96
|
|
|
$volume = $asset->getVolume(); |
|
97
|
|
|
$assetUrl = AssetsHelper::generateUrl($volume, $asset); |
|
98
|
|
|
$assetUri = parse_url($assetUrl, PHP_URL_PATH); |
|
99
|
|
|
|
|
100
|
|
|
return $assetUri; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $url |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function prefetchRemoteFile($url) |
|
107
|
|
|
{ |
|
108
|
|
|
// Get an absolute URL with protocol that curl will be happy with |
|
109
|
|
|
$url = UrlHelper::absoluteUrlWithProtocol($url); |
|
110
|
|
|
$ch = curl_init($url); |
|
111
|
|
|
curl_setopt_array($ch, [ |
|
112
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
|
113
|
|
|
CURLOPT_FOLLOWLOCATION => 1, |
|
114
|
|
|
CURLOPT_SSL_VERIFYPEER => 0, |
|
115
|
|
|
CURLOPT_NOBODY => 1, |
|
116
|
|
|
]); |
|
117
|
|
|
curl_exec($ch); |
|
118
|
|
|
curl_close($ch); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Append an extension a passed url or path |
|
123
|
|
|
* |
|
124
|
|
|
* @param $pathOrUrl |
|
125
|
|
|
* @param $extension |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public static function appendExtension($pathOrUrl, $extension): string |
|
130
|
|
|
{ |
|
131
|
|
|
$path = self::decomposeUrl($pathOrUrl); |
|
132
|
|
|
$path_parts = pathinfo($path['path']); |
|
133
|
|
|
$new_path = $path_parts['filename'] . '.' . $path_parts['extension'] . $extension; |
|
134
|
|
|
if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== '.') { |
|
135
|
|
|
$new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path; |
|
136
|
|
|
$new_path = preg_replace('#/+#', '/', $new_path); |
|
137
|
|
|
} |
|
138
|
|
|
$output = $path['prefix'] . $new_path . $path['suffix']; |
|
139
|
|
|
|
|
140
|
|
|
return $output; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// Protected Methods |
|
144
|
|
|
// ========================================================================= |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Decompose a url into a prefix, path, and suffix |
|
148
|
|
|
* |
|
149
|
|
|
* @param $pathOrUrl |
|
150
|
|
|
* |
|
151
|
|
|
* @return array |
|
152
|
|
|
*/ |
|
153
|
|
|
protected static function decomposeUrl($pathOrUrl): array |
|
154
|
|
|
{ |
|
155
|
|
|
$result = array(); |
|
156
|
|
|
|
|
157
|
|
|
if (filter_var($pathOrUrl, FILTER_VALIDATE_URL)) { |
|
158
|
|
|
$url_parts = parse_url($pathOrUrl); |
|
159
|
|
|
$result['prefix'] = $url_parts['scheme'] . '://' . $url_parts['host']; |
|
160
|
|
|
$result['path'] = $url_parts['path']; |
|
161
|
|
|
$result['suffix'] = ''; |
|
162
|
|
|
$result['suffix'] .= empty($url_parts['query']) ? '' : '?' . $url_parts['query']; |
|
163
|
|
|
$result['suffix'] .= empty($url_parts['fragment']) ? '' : '#' . $url_parts['fragment']; |
|
164
|
|
|
} else { |
|
165
|
|
|
$result['prefix'] = ''; |
|
166
|
|
|
$result['path'] = $pathOrUrl; |
|
167
|
|
|
$result['suffix'] = ''; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $result; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|