Skip to content

Commit 26699fe

Browse files
committed
unify image size to dimension logic in dam/non dam context
1 parent bf8b18d commit 26699fe

File tree

4 files changed

+84
-96
lines changed

4 files changed

+84
-96
lines changed

inc/app_replacer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public static function add_size( $width = null, $height = null, $crop = null ) {
267267
* @return array
268268
* @global $wp_additional_image_sizes
269269
*/
270-
protected static function image_sizes() {
270+
public static function image_sizes() {
271271
if ( ! empty( self::$image_sizes ) ) {
272272
return self::$image_sizes;
273273
}

inc/dam.php

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
use Optimole\Sdk\Resource\ImageProperty\GravityProperty;
13+
use Optimole\Sdk\Resource\ImageProperty\ResizeTypeProperty;
1314
use Optimole\Sdk\ValueObject\Position;
1415

1516
/**
@@ -215,10 +216,9 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
215216
];
216217
}
217218

219+
$metadata = wp_get_attachment_metadata( $attachment_id );
218220
// Use the original size if the requested size is full.
219221
if ( $size === 'full' ) {
220-
$metadata = wp_get_attachment_metadata( $attachment_id );
221-
222222
$image_url = $this->replace_dam_url_args(
223223
[
224224
'width' => $metadata['width'],
@@ -235,45 +235,21 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
235235
false,
236236
];
237237
}
238-
239-
$crop = false;
240-
241-
// Size can be int [] containing width and height.
242-
if ( is_array( $size ) ) {
243-
$width = $size[0];
244-
$height = $size[1];
245-
$crop = true;
246-
} else {
247-
$sizes = $this->get_all_image_sizes();
248-
249-
if ( ! isset( $sizes[ $size ] ) ) {
250-
return [
251-
$image_url,
252-
$width,
253-
$height,
254-
false,
255-
];
256-
}
257-
258-
$width = $sizes[ $size ]['width'];
259-
$height = $sizes[ $size ]['height'];
260-
$crop = (bool) $sizes[ $size ]['crop'];
261-
}
238+
$sizes = $this->size_to_dimension( $size, $metadata );
262239

263240
$image_url = $this->replace_dam_url_args(
264241
[
265-
'width' => $width,
266-
'height' => $height,
267-
'crop' => $crop,
242+
'width' => $sizes['width'],
243+
'height' => $sizes['height'],
244+
'crop' => $sizes['resize'] ?? false,
268245
],
269246
$image_url
270247
);
271-
272248
return [
273249
$image_url,
274-
$width,
275-
$height,
276-
$crop,
250+
$sizes['width'],
251+
$sizes['height'],
252+
$size === 'full',
277253
];
278254
}
279255

@@ -626,7 +602,7 @@ public function alter_elementor_image_size( $html, $settings, $image_size_key, $
626602
return $this->replace_dam_url_args( $custom_dimensions, $html );
627603
}
628604

629-
$all_sizes = $this->get_all_image_sizes();
605+
$all_sizes = Optml_App_Replacer::image_sizes();
630606

631607
if ( ! isset( $all_sizes[ $image_size_key ] ) ) {
632608
return $html;
@@ -649,19 +625,19 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
649625
return $response;
650626
}
651627

652-
$sizes = $this->get_all_image_sizes();
653-
628+
$sizes = Optml_App_Replacer::image_sizes();
629+
$meta = [];
630+
if ( isset( $response['width'] ) ) {
631+
$meta['width'] = $response['width'];
632+
}
633+
if ( isset( $response['height'] ) ) {
634+
$meta['height'] = $response['height'];
635+
}
654636
foreach ( $sizes as $size => $args ) {
655637
if ( isset( $response['sizes'][ $size ] ) ) {
656638
continue;
657639
}
658-
659-
$args = [
660-
'height' => $args['height'],
661-
'width' => $args['width'],
662-
'crop' => true,
663-
];
664-
640+
$args = $this->size_to_dimension( $size, $meta );
665641
$response['sizes'][ $size ] = array_merge(
666642
$args,
667643
[
@@ -670,14 +646,7 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
670646
]
671647
);
672648
}
673-
674-
$url_args = [
675-
'height' => $response['height'],
676-
'width' => $response['width'],
677-
'crop' => false,
678-
];
679-
680-
$response['url'] = $this->replace_dam_url_args( $url_args, $response['url'] );
649+
$response['url'] = $this->replace_dam_url_args( $meta, $response['url'] );
681650

682651
return $response;
683652
}
@@ -745,7 +714,7 @@ public function replace_dam_url_args( $args, $subject ) {
745714

746715
$width = $args['width'];
747716
$height = $args['height'];
748-
$crop = (bool) $args['crop'];
717+
$crop = $args['crop'] ?? $args['resize'] ?? false;
749718

750719
$gravity = Position::CENTER;
751720

@@ -764,8 +733,12 @@ public function replace_dam_url_args( $args, $subject ) {
764733
// Use the proper replacement for the image size.
765734
$replacement = '/w:' . $width . '/h:' . $height;
766735

767-
if ( $crop ) {
736+
if ( $crop === true ) {
768737
$replacement .= '/g:' . $gravity . '/rt:fill';
738+
} elseif ( is_array( $crop ) && ! empty( $crop ) ) {
739+
$replacement .= '/' . ( new GravityProperty( $crop['gravity'] ) ) .
740+
'/' . new ResizeTypeProperty( $crop['type'] ) .
741+
( $crop['enlarge'] ? '/el:1' : '' );
769742
}
770743

771744
$replacement .= '/q:';

inc/tag_replacer.php

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -459,48 +459,7 @@ public function filter_image_downsize( $image, $attachment_id, $size ) {
459459
}
460460

461461
$image_meta = wp_get_attachment_metadata( $attachment_id );
462-
$image_args = self::image_sizes();
463-
// default size
464-
$sizes = [
465-
'width' => isset( $image_meta['width'] ) ? intval( $image_meta['width'] ) : false,
466-
'height' => isset( $image_meta['height'] ) ? intval( $image_meta['height'] ) : false,
467-
];
468-
469-
switch ( $size ) {
470-
case is_array( $size ):
471-
$width = isset( $size[0] ) ? (int) $size[0] : false;
472-
$height = isset( $size[1] ) ? (int) $size[1] : false;
473-
if ( ! $width || ! $height ) {
474-
break;
475-
}
476-
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $width, $height );
477-
if ( $image_resized ) {
478-
$width = $image_resized[6];
479-
$height = $image_resized[7];
480-
} else {
481-
$width = $image_meta['width'];
482-
$height = $image_meta['height'];
483-
}
484-
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $width, $height, $size );
485-
486-
break;
487-
case 'full' !== $size && isset( $image_args[ $size ] ):
488-
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $image_args[ $size ]['width'], $image_args[ $size ]['height'], $image_args[ $size ]['crop'] );
489-
490-
if ( $image_resized ) { // This could be false when the requested image size is larger than the full-size image.
491-
$sizes['width'] = $image_resized[6];
492-
$sizes['height'] = $image_resized[7];
493-
}
494-
// There are cases when the image meta is missing and image size is non existent, see SVG image handling.
495-
if ( ! $sizes['width'] || ! $sizes['height'] ) {
496-
break;
497-
}
498-
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $sizes['width'], $sizes['height'], $size, 'display' );
499-
500-
$sizes['resize'] = $this->to_optml_crop( $image_args[ $size ]['crop'] );
501-
502-
break;
503-
}
462+
$sizes = $this->size_to_dimension( $size, $image_meta );
504463
$image_url = $this->strip_image_size_from_url( $image_url );
505464

506465
$new_url = apply_filters( 'optml_content_url', $image_url, $sizes );

inc/traits/normalizer.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,62 @@ public function to_bound_integer( $value, $min, $max ) {
154154
return $integer;
155155
}
156156

157+
/**
158+
* Convert image size to dimensions.
159+
*
160+
* This function takes an image size and its metadata, and returns the dimensions
161+
* of the image based on the specified size. It handles different cases such as
162+
* custom sizes, predefined sizes, and full size.
163+
*
164+
* @param mixed $size The size of the image. Can be an array of width and height, a predefined size, or 'full'.
165+
* @param array $image_meta Metadata of the image, including width and height.
166+
*
167+
* @return array The dimensions of the image, including width, height, and optional resize parameters.
168+
*/
169+
public function size_to_dimension( $size, $image_meta ) {
170+
// default size
171+
$sizes = [
172+
'width' => isset( $image_meta['width'] ) ? intval( $image_meta['width'] ) : false,
173+
'height' => isset( $image_meta['height'] ) ? intval( $image_meta['height'] ) : false,
174+
];
175+
$image_args = self::image_sizes();
176+
switch ( $size ) {
177+
case is_array( $size ):
178+
$width = isset( $size[0] ) ? (int) $size[0] : false;
179+
$height = isset( $size[1] ) ? (int) $size[1] : false;
180+
if ( ! $width || ! $height ) {
181+
break;
182+
}
183+
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $width, $height );
184+
if ( $image_resized ) {
185+
$width = $image_resized[6];
186+
$height = $image_resized[7];
187+
} else {
188+
$width = $image_meta['width'];
189+
$height = $image_meta['height'];
190+
}
191+
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $width, $height, $size );
192+
193+
break;
194+
case 'full' !== $size && isset( $image_args[ $size ] ):
195+
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $image_args[ $size ]['width'], $image_args[ $size ]['height'], $image_args[ $size ]['crop'] );
196+
197+
if ( $image_resized ) { // This could be false when the requested image size is larger than the full-size image.
198+
$sizes['width'] = $image_resized[6];
199+
$sizes['height'] = $image_resized[7];
200+
}
201+
// There are cases when the image meta is missing and image size is non existent, see SVG image handling.
202+
if ( ! $sizes['width'] || ! $sizes['height'] ) {
203+
break;
204+
}
205+
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $sizes['width'], $sizes['height'], $size, 'display' );
206+
207+
$sizes['resize'] = $this->to_optml_crop( $image_args[ $size ]['crop'] );
208+
209+
break;
210+
}
211+
return $sizes;
212+
}
157213
/**
158214
* Normalize arguments for crop.
159215
*

0 commit comments

Comments
 (0)