-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathnormalizer.php
More file actions
379 lines (346 loc) · 10.3 KB
/
Copy pathnormalizer.php
File metadata and controls
379 lines (346 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
use Optimole\Sdk\Resource\ImageProperty\ResizeTypeProperty;
use Optimole\Sdk\ValueObject\Position;
/**
* Normalization traits.
*
* @package \Optml\Inc\Traits
* @author Optimole <friends@optimole.com>
*/
trait Optml_Normalizer {
/**
* Static cache for dimension calculations
*
* @var array
*/
private static $dimension_cache = [];
/**
* Normalize value to boolean.
*
* @param mixed $value Value to process.
*
* @return bool
*/
public function to_boolean( $value ) {
if ( in_array( $value, [ 'yes', 'enabled', 'true', '1' ], true ) ) {
return true;
}
if ( in_array( $value, [ 'no', 'disabled', 'false', '0' ], true ) ) {
return false;
}
return boolval( $value );
}
/**
* Get the unoptimized url from an Optimole url.
* Works only on non-offloaded images.
*
* @param string $url The url to get the unoptimized url for.
*
* @return string The unoptimized url.
*/
public function get_unoptimized_url( $url ) {
// If the url is not an optimole url, return the url
if ( strpos( $url, Optml_Config::$service_url ) === false ) {
return $url;
}
// If the url is an uploaded image, return the url
if ( Optml_Media_Offload::is_uploaded_image( $url ) ) {
$pattern = '#/id:([^/]+)/((?:https?://|http://|directUpload/)\S+)#';
if ( preg_match( $pattern, $url, $matches ) ) {
$url = $matches[0];
}
return $url;
}
$url_parts = explode( 'http', $url );
if ( ! isset( $url_parts[2] ) ) {
return $url;
}
return 'http' . $url_parts[2];
}
/**
* Return domain hash.
*
* @param string $domain Full url.
*
* @return string Domain hash.
*/
public function to_domain_hash( $domain ) {
$domain_parts = parse_url( $domain );
$domain = isset( $domain_parts['host'] ) ? $domain_parts['host'] : '';
$prefix = substr( $domain, 0, 4 );
if ( $prefix === 'www.' ) {
$domain = substr( $domain, 4 );
}
return base64_encode( $domain );
}
/**
* Strip slashes on unicode encoded strings.
*
* @param string $content Input string.
*
* @return string Decoded string.
*/
public function strip_slashes( $content ) {
return html_entity_decode( stripslashes( preg_replace( '/\\\u([\da-fA-F]{4})/', '&#x\1;', $content ) ) );
}
/**
* Normalize value to positive integer.
*
* @param mixed $value Value to process.
*
* @return integer
*/
public function to_positive_integer( $value ) {
$integer = (int) $value;
return ( $integer > 0 ) ? $integer : 0;
}
/**
* Normalize value to map.
*
* @param mixed $value Value to process.
* @param array $map Associative list from witch to return.
* @param mixed $initial Default.
*
* @return mixed
*/
public function to_map_values( $value, $map, $initial ) {
if ( in_array( $value, $map, true ) ) {
return $value;
}
return $initial;
}
/**
* Normalize value to an accepted quality.
*
* @param mixed $value Value to process.
*
* @return mixed
*/
public function to_accepted_quality( $value ) {
if ( is_numeric( $value ) ) {
return intval( $value );
}
$value = trim( $value );
$accepted_qualities = [
'eco' => 'eco',
'auto' => 'auto',
'mauto' => 'mauto',
'high_c' => 55,
'medium_c' => 75,
'low_c' => 90,
];
if ( array_key_exists( $value, $accepted_qualities ) ) {
return $accepted_qualities[ $value ];
}
// Legacy values.
return 60;
}
/**
* Normalize value to an accepted minify.
*
* @param mixed $value Value to process.
*
* @return mixed
*/
public function to_accepted_minify( $value ) {
if ( is_numeric( $value ) ) {
return $this->to_bound_integer( $value, 0, 1 );
}
return 'auto';
}
/**
* Normalize value to an integer within bounds.
*
* @param mixed $value Value to process.
* @param integer $min Lower bound.
* @param integer $max Upper bound.
*
* @return integer
*/
public function to_bound_integer( $value, $min, $max ) {
$integer = absint( $value );
if ( $integer < $min ) {
$integer = $min;
}
if ( $integer > $max ) {
$integer = $max;
}
return $integer;
}
/**
* Convert image size to dimensions.
*
* This function takes an image size and its metadata, and returns the dimensions
* of the image based on the specified size. It handles different cases such as
* custom sizes, predefined sizes, and full size.
*
* @param mixed $size The size of the image. Can be an array of width and height, a predefined size, or 'full'.
* @param array $image_meta Metadata of the image, including width and height.
* @param int $attachment_id The ID of the attachment.
*
* @return array The dimensions of the image, including width, height, and optional resize parameters.
*/
public function size_to_dimension( $size, $image_meta, $attachment_id = null ) {
// default size
$sizes = [
'width' => isset( $image_meta['width'] ) ? intval( $image_meta['width'] ) : false,
'height' => isset( $image_meta['height'] ) ? intval( $image_meta['height'] ) : false,
];
$image_args = Optml_App_Replacer::image_sizes();
$sizes2crop = Optml_App_Replacer::size_to_crop();
switch ( $size ) {
case is_array( $size ):
$width = isset( $size[0] ) ? (int) $size[0] : false;
$height = isset( $size[1] ) ? (int) $size[1] : false;
if ( ! $width || ! $height ) {
break;
}
$cache_key = 'a' . $width . '_' . $height . '_' . $sizes['width'] . '_' . $sizes['height'];
if ( isset( self::$dimension_cache[ $cache_key ] ) ) {
return self::$dimension_cache[ $cache_key ];
}
if ( $attachment_id ) {
$intermediate = image_get_intermediate_size( $attachment_id, $size );
if ( $intermediate ) {
$sizes['width'] = $intermediate['width'];
$sizes['height'] = $intermediate['height'];
}
}
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $sizes['width'], $sizes['height'], $size );
$resize = apply_filters( 'optml_default_crop', [] );
if ( isset( $sizes2crop[ $sizes['width'] . $sizes['height'] ] ) ) {
$resize = $this->to_optml_crop( $sizes2crop[ $sizes['width'] . $sizes['height'] ] );
}
$sizes['resize'] = $resize;
self::$dimension_cache[ $cache_key ] = $sizes;
break;
case 'full' !== $size && isset( $image_args[ $size ] ):
$cache_key = 'b' . $size . '_' . $sizes['width'] . '_' . $sizes['height'];
if ( isset( self::$dimension_cache[ $cache_key ] ) ) {
return self::$dimension_cache[ $cache_key ];
}
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $image_args[ $size ]['width'], $image_args[ $size ]['height'], $image_args[ $size ]['crop'] );
if ( $image_resized ) { // This could be false when the requested image size is larger than the full-size image.
$sizes['width'] = $image_resized[6];
$sizes['height'] = $image_resized[7];
}
// There are cases when the image meta is missing and image size is non existent, see SVG image handling.
if ( ! $sizes['width'] || ! $sizes['height'] ) {
break;
}
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $sizes['width'], $sizes['height'], $size, 'display' );
$sizes['resize'] = $this->to_optml_crop( $image_args[ $size ]['crop'] );
self::$dimension_cache[ $cache_key ] = $sizes;
break;
}
return $sizes;
}
/**
* Normalize arguments for crop.
*
* @param array|bool $crop_args Crop arguments.
*
* @return array
*/
public function to_optml_crop( $crop_args = [] ) {
$enlarge = false;
if ( isset( $crop_args['enlarge'] ) ) {
$enlarge = $crop_args['enlarge'];
$crop_args = $crop_args['crop'];
}
if ( $crop_args === true ) {
return [
'type' => ResizeTypeProperty::FILL,
'enlarge' => $enlarge,
'gravity' => Position::CENTER,
];
}
if ( $crop_args === false || ! is_array( $crop_args ) || count( $crop_args ) !== 2 ) {
return [];
}
$allowed_x = [
'left' => true,
'center' => true,
'right' => true,
];
$allowed_y = [
'top' => true,
'center' => true,
'bottom' => true,
];
$allowed_gravities = [
'left' => Position::WEST,
'right' => Position::EAST,
'top' => Position::NORTH,
'bottom' => Position::SOUTH,
'lefttop' => Position::NORTH_WEST,
'leftbottom' => Position::SOUTH_WEST,
'righttop' => Position::NORTH_EAST,
'rightbottom' => Position::SOUTH_EAST,
'centertop' => [ 0.5, 0 ],
'centerbottom' => [ 0.5, 1 ],
'leftcenter' => [ 0, 0.5 ],
'rightcenter' => [ 1, 0.5 ],
];
$gravity = Position::CENTER;
$key_search = ( $crop_args[0] === true ? '' :
( isset( $allowed_x[ $crop_args[0] ] ) ? $crop_args[0] : '' ) ) .
( $crop_args[1] === true ? '' :
( isset( $allowed_y[ $crop_args[1] ] ) ? $crop_args[1] : '' ) );
if ( array_key_exists( $key_search, $allowed_gravities ) ) {
$gravity = $allowed_gravities[ $key_search ];
}
return [
'type' => ResizeTypeProperty::FILL,
'enlarge' => $enlarge,
'gravity' => $gravity,
];
}
/**
* Normalize arguments for watermark.
*
* @param array $watermark_args Watermark arguments.
*
* @return array
*/
public function to_optml_watermark( $watermark_args = [] ) {
$allowed_gravities = [
'left' => Position::WEST,
'right' => Position::EAST,
'top' => Position::NORTH,
'bottom' => Position::SOUTH,
'left_top' => Position::NORTH_WEST,
'left_bottom' => Position::SOUTH_WEST,
'right_top' => Position::NORTH_EAST,
'right_bottom' => Position::SOUTH_EAST,
];
$gravity = Position::CENTER;
if ( isset( $watermark_args['position'] ) && array_key_exists( $watermark_args['position'], $allowed_gravities ) ) {
$gravity = $allowed_gravities[ $watermark_args['position'] ];
}
return [
'opacity' => 1,
'position' => $gravity,
];
}
/**
* If missing, add schema to urls.
*
* @param string $url Url to check.
*
* @return string
*/
public function add_schema( $url ) {
$schema_url = $url;
$should_add_schema = false;
if ( function_exists( 'str_starts_with' ) ) {
$should_add_schema = str_starts_with( $schema_url, '//' );
} else {
$should_add_schema = substr( $schema_url, 0, strlen( '//' ) ) === '//';
}
if ( $should_add_schema ) {
$schema_url = ( is_ssl() ? 'https:' : 'http:' ) . $schema_url;
}
return $schema_url;
}
}