PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 3.11.0
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v3.11.0
4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 3.12.1 All 134 releases
optimole-wp / inc / image.php

image.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 3.11.0, at inc/image.php

273 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The class maps settings and options from imageproxy.
5 *
6 * @package \Optml\Inc
7 * @author Optimole <friends@optimole.com>
8 */
9 class Optml_Image extends Optml_Resource {
10 use Optml_Dam_Offload_Utils;
11
12 /**
13 * Watermark for the image.
14 *
15 * @var Optml_Watermark Watermark.
16 */
17 public static $watermark = null;
18 /**
19 * Quality of the resulting image.
20 *
21 * @var Optml_Quality Quality;
22 */
23 public $quality = null;
24 /**
25 * Width of the resulting image.
26 *
27 * @var Optml_Width Width.
28 */
29 private $width = null;
30 /**
31 * Height of the resulting image.
32 *
33 * @var Optml_Height Height.
34 */
35 private $height = null;
36 /**
37 * Resize type for the image.
38 *
39 * @var Optml_Resize Resize details.
40 */
41 private $resize = null;
42
43 /**
44 * The attachment id.
45 *
46 * @var null | int Attachment id.
47 */
48 private $attachment_id = null;
49
50 /**
51 * Optml_Image constructor.
52 *
53 * @param string $url Source image url.
54 * @param array $args Transformation arguments.
55 *
56 * @throws \InvalidArgumentException In case that the url is not provided.
57 */
58 public function __construct( $url = '', $args = [], $cache_buster = '' ) {
59 parent::__construct( $url, $cache_buster );
60
61 $this->width->set( $args['width'] );
62 $this->height->set( $args['height'] );
63
64 if ( isset( $args['quality'] ) ) {
65 $this->quality->set( $args['quality'] );
66 }
67
68 if ( isset( $args['resize'] ) ) {
69 $this->resize->set( $args['resize'] );
70 }
71
72 if ( isset( $args['attachment_id'] ) ) {
73 $this->attachment_id = $args['attachment_id'];
74 }
75 }
76
77 /**
78 * Set defaults for image transformations.
79 */
80 protected function set_defaults() {
81 $this->width = new Optml_Width( 'auto' );
82 $this->height = new Optml_Height( 'auto' );
83 $this->resize = new Optml_Resize();
84 $this->quality = new Optml_Quality();
85 }
86
87 /**
88 * Return transformed url.
89 *
90 * @param array $params Image transforms parameters.
91 *
92 * @return string Transformed image url.
93 */
94 public function get_url( $params = [] ) {
95 $path_params = $this->get_path_params( $params );
96
97 if ( $this->is_dam_url() ) {
98 return $this->get_dam_url( $path_params );
99 }
100
101 if ( $this->is_offloaded_url() ) {
102 return $this->get_offloaded_url( $path_params );
103 }
104
105 $path = $path_params . '/' . $this->source_url;
106
107 return sprintf( '%s%s', Optml_Config::$service_url, $path );
108
109 }
110
111 /**
112 * Check if this contains the DAM flag.
113 *
114 * @return bool
115 */
116 public function is_dam_url() {
117 return strpos( $this->source_url, Optml_Dam::URL_DAM_FLAG ) !== false;
118 }
119
120 /**
121 * Get the DAM image URL.
122 *
123 * @param string $path_params Path parameters.
124 *
125 * @return string
126 */
127 private function get_dam_url( $path_params ) {
128 $url = $this->source_url;
129
130 // Remove DAM flag.
131 $url = str_replace( Optml_Dam::URL_DAM_FLAG, '', $url );
132
133 // Split the path params.
134 $path_params_parts = explode( '/', $path_params );
135
136 foreach ( $path_params_parts as $param ) {
137 if ( empty( $param ) ) {
138 continue;
139 }
140
141 // Split into parts.
142 $param_parts = explode( ':', $param );
143
144 // Check if the param is width or height and if it's auto.
145 // Rely on the initial URL as it generates the correct image size.
146 if ( in_array( $param_parts[0], ['w', 'h'], true ) ) {
147 if ( $param_parts[1] === 'auto' ) {
148 continue;
149 }
150 }
151
152 // if the param exists we do a non-greedy replace for /$param:$value/ with the updated value.
153 if ( strpos( $url, '/' . $param_parts[0] . ':' ) !== false ) {
154 $url = preg_replace( '/\/' . $param_parts[0] . ':.*?\//', '/' . $param . '/', $url );
155
156 continue;
157 }
158
159 // Otherwise, we add it before the /q: param.
160 $url = str_replace( '/q:', '/' . $param . '/q:', $url );
161 }
162
163 return $url;
164 }
165
166 /**
167 * Get transform params as string.
168 *
169 * @param array $params Image transforms parameters.
170 *
171 * @return string Transform parameters string to use in the URL.
172 */
173 private function get_path_params( $params = [] ) {
174 $path_parts = [];
175
176 $path_parts[] = $this->width->toString();
177 $path_parts[] = $this->height->toString();
178 $path_parts[] = $this->quality->toString();
179
180 if ( isset( $this->resize->get()['type'] ) ) {
181 $path_parts[] = $this->resize->toString();
182 }
183 if ( isset( $params['apply_watermark'] ) && $params['apply_watermark'] && is_array( self::$watermark->get() ) && isset( self::$watermark->get()['id'] ) && self::$watermark->get()['id'] > 0 ) {
184 $path_parts[] = self::$watermark->toString();
185 }
186
187 $path = '';
188
189 if ( isset( $params['format'] ) && ! empty( $params['format'] ) ) {
190 $path = '/f:' . $params['format'] . $path;
191 }
192
193 if ( isset( $params['strip_metadata'] ) && '0' === $params['strip_metadata'] ) {
194 $path = '/sm:' . $params['strip_metadata'] . $path;
195 }
196
197 if ( isset( $params['ignore_avif'] ) && $params['ignore_avif'] === true ) {
198 $path = '/ig:avif' . $path;
199 }
200
201 if ( $this->has_attachment_id() ) {
202 $path = $path . sprintf( '/%s%s', Optml_Media_Offload::KEYS['not_processed_flag'], $this->attachment_id );
203 }
204
205 if ( apply_filters( 'optml_keep_copyright', false ) === true ) {
206 $path = '/keep_copyright:true' . $path;
207 }
208
209 $path = sprintf( '/%s%s', implode( '/', $path_parts ), $path );
210
211 $cache_buster = $this->get_cache_buster();
212 if ( $cache_buster !== false ) {
213 $path = sprintf( '/%s%s', 'cb:' . $cache_buster, $path );
214 }
215
216 return $path;
217 }
218
219 /**
220 * Check if this URL was offloaded.
221 *
222 * @return bool
223 */
224 private function is_offloaded_url() {
225 // Catch this from URLs that explicitly have the flag when constructing the image.
226 if ( $this->attachment_id !== null && $this->attachment_id > 0 ) {
227 return ! empty( get_post_meta( $this->attachment_id, Optml_Media_Offload::OM_OFFLOADED_FLAG, true ) );
228 }
229
230 $attachment_id = 0;
231
232 if ( strpos( $this->source_url, Optml_Media_Offload::KEYS['not_processed_flag'] ) !== false ) {
233 $attachment_id = (int) Optml_Media_Offload::get_attachment_id_from_url( $this->source_url );
234 } else {
235 $attachment_id = $this->attachment_url_to_post_id( $this->source_url );
236 }
237
238 if ( $attachment_id === 0 ) {
239 return false;
240 }
241
242 if ( empty( get_post_meta( $attachment_id, Optml_Media_Offload::OM_OFFLOADED_FLAG, true ) ) ) {
243 return false;
244 }
245
246 $this->attachment_id = (int) $attachment_id;
247
248 return true;
249 }
250
251 /**
252 * Get offloaded URL.
253 *
254 * @param string $path_params Path parameters.
255 *
256 * @return string
257 */
258 private function get_offloaded_url( $path_params ) {
259 $metadata = wp_get_attachment_metadata( $this->attachment_id );
260
261 return sprintf( '%s%s%s', Optml_Config::$service_url, $path_params, $metadata['file'] );
262 }
263
264 /**
265 * Check if the URL has an attachment ID.
266 *
267 * @return bool
268 */
269 private function has_attachment_id() {
270 return $this->attachment_id !== null && $this->attachment_id !== 0;
271 }
272 }
273