|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helick\Imgix; |
|
4
|
|
|
|
|
5
|
|
|
use Imgix\UrlBuilder; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Get the imgix domain. |
|
9
|
|
|
* |
|
10
|
|
|
* @return string |
|
11
|
|
|
*/ |
|
12
|
|
|
function domain(): string |
|
13
|
|
|
{ |
|
14
|
|
|
return defined('IMGIX_DOMAIN') |
|
15
|
|
|
? IMGIX_DOMAIN |
|
|
|
|
|
|
16
|
|
|
: ''; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Get the imgix supported formats. |
|
21
|
|
|
* |
|
22
|
|
|
* @return array |
|
23
|
|
|
*/ |
|
24
|
|
|
function supported_formats(): array |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Control the imgix supported formats. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $supportedFormats |
|
30
|
|
|
*/ |
|
31
|
|
|
$supportedFormats = apply_filters('helick_imgix_supported_formats', ['gif', 'jpg', 'jpeg', 'png']); |
|
|
|
|
|
|
32
|
|
|
$supportedFormats = (array)$supportedFormats; |
|
33
|
|
|
|
|
34
|
|
|
return $supportedFormats; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Check whether the given format is supported. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $format |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
function is_supported_format(string $format): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return in_array($format, supported_formats(), true); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Check whether the given image url is processable. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $imageUrl |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
function is_processable_image_url(string $imageUrl): bool |
|
57
|
|
|
{ |
|
58
|
|
|
if (strpos($imageUrl, wp_upload_dir()['baseurl']) !== 0) { |
|
|
|
|
|
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$imageUrlPath = parse_url($imageUrl, PHP_URL_PATH); |
|
63
|
|
|
|
|
64
|
|
|
$imageFormat = pathinfo($imageUrlPath, PATHINFO_EXTENSION); |
|
65
|
|
|
$imageFormat = strtolower($imageFormat); |
|
66
|
|
|
|
|
67
|
|
|
if (!is_supported_format($imageFormat)) { |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Control whether the given image url is processable. |
|
73
|
|
|
* |
|
74
|
|
|
* @param bool $isProcessable |
|
75
|
|
|
* @param string $imageUrl |
|
76
|
|
|
* @param array $args |
|
77
|
|
|
*/ |
|
78
|
|
|
$isProcessable = apply_filters('helick_imgix_image_url_processable', true, $imageUrl, $args); |
|
|
|
|
|
|
79
|
|
|
$isProcessable = (bool)$isProcessable; |
|
80
|
|
|
|
|
81
|
|
|
return $isProcessable; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the imgix url. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $imageUrl |
|
88
|
|
|
* @param array $args |
|
89
|
|
|
* @param bool $useHttps |
|
90
|
|
|
* @param string $signKey |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
function url(string $imageUrl, array $args = [], bool $useHttps = true, string $signKey = ''): string |
|
95
|
|
|
{ |
|
96
|
|
|
if (!is_processable_image_url($imageUrl)) { |
|
97
|
|
|
return $imageUrl; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$imageUrlPath = parse_url($imageUrl, PHP_URL_PATH); |
|
101
|
|
|
|
|
102
|
|
|
$imageFile = basename($imageUrlPath); |
|
103
|
|
|
$imageFile = urlencode($imageFile); |
|
104
|
|
|
|
|
105
|
|
|
$imageUrlPath = str_replace($imageUrl, $imageFile, $imageUrlPath); |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Control the imgix image url path. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $imageUrlPath |
|
111
|
|
|
* @param array $args |
|
112
|
|
|
*/ |
|
113
|
|
|
$imageUrlPath = apply_filters('helick_imgix_image_url_path', $imageUrlPath, $args); |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Control the imgix arguments. |
|
117
|
|
|
* |
|
118
|
|
|
* @param array $args |
|
119
|
|
|
* @param string $imageUrlPath |
|
120
|
|
|
*/ |
|
121
|
|
|
$args = apply_filters('helick_imgix_args', $args, $imageUrlPath); |
|
122
|
|
|
|
|
123
|
|
|
$urlBuilder = new UrlBuilder(domain(), $useHttps, $signKey); |
|
124
|
|
|
$imgixUrl = $urlBuilder->createURL($imageUrlPath, $args); |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Control the imgix url. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $imgixUrl |
|
130
|
|
|
* @param string $imageUrl |
|
131
|
|
|
* @param array $args |
|
132
|
|
|
*/ |
|
133
|
|
|
$imgixUrl = apply_filters('helick_imgix_url', $imgixUrl, $imageUrl, $args); |
|
134
|
|
|
|
|
135
|
|
|
return $imgixUrl; |
|
136
|
|
|
} |
|
137
|
|
|
|