Skip to content

Commit b20eb1a

Browse files
authored
Merge pull request #850 from Codeinwp/fix/resize
Fix/resize
2 parents bf8b18d + ed150d2 commit b20eb1a

File tree

8 files changed

+128
-191
lines changed

8 files changed

+128
-191
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: 29 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,18 +625,20 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
649625
return $response;
650626
}
651627

652-
$sizes = $this->get_all_image_sizes();
628+
$sizes = Optml_App_Replacer::image_sizes();
653629

630+
$meta = [];
631+
if ( isset( $response['width'] ) ) {
632+
$meta['width'] = $response['width'];
633+
}
634+
if ( isset( $response['height'] ) ) {
635+
$meta['height'] = $response['height'];
636+
}
654637
foreach ( $sizes as $size => $args ) {
655638
if ( isset( $response['sizes'][ $size ] ) ) {
656639
continue;
657640
}
658-
659-
$args = [
660-
'height' => $args['height'],
661-
'width' => $args['width'],
662-
'crop' => true,
663-
];
641+
$args = $this->size_to_dimension( $size, $meta );
664642

665643
$response['sizes'][ $size ] = array_merge(
666644
$args,
@@ -670,14 +648,7 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
670648
]
671649
);
672650
}
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'] );
651+
$response['url'] = $this->replace_dam_url_args( $meta, $response['url'] );
681652

682653
return $response;
683654
}
@@ -707,7 +678,7 @@ public function alter_img_tag_w_h( $dimensions, $image_src, $image_meta, $attach
707678
$width = $incoming_size[0];
708679
$height = $incoming_size[1];
709680

710-
$sizes = $this->get_all_image_sizes();
681+
$sizes = Optml_App_Replacer::image_sizes();
711682

712683
// If this is an image size. Return its dimensions.
713684
foreach ( $sizes as $size => $args ) {
@@ -741,11 +712,11 @@ public function alter_img_tag_w_h( $dimensions, $image_src, $image_meta, $attach
741712
* @return string
742713
*/
743714
public function replace_dam_url_args( $args, $subject ) {
744-
$args = wp_parse_args( $args, [ 'width' => 'auto', 'height' => 'auto', 'crop' => false, 'dam' => true ] );
715+
$args = wp_parse_args( $args, [ 'width' => 'auto', 'height' => 'auto', 'dam' => true ] );
745716

746717
$width = $args['width'];
747718
$height = $args['height'];
748-
$crop = (bool) $args['crop'];
719+
$crop = $args['crop'] ?? $args['resize'] ?? false;
749720

750721
$gravity = Position::CENTER;
751722

@@ -764,8 +735,13 @@ public function replace_dam_url_args( $args, $subject ) {
764735
// Use the proper replacement for the image size.
765736
$replacement = '/w:' . $width . '/h:' . $height;
766737

767-
if ( $crop ) {
738+
if ( $crop === true ) {
768739
$replacement .= '/g:' . $gravity . '/rt:fill';
740+
} elseif ( is_array( $crop ) && ! empty( $crop ) ) {
741+
742+
$replacement .= '/' . ( new GravityProperty( $crop['gravity'] ) ) .
743+
'/' . new ResizeTypeProperty( $crop['type'] ) .
744+
( $crop['enlarge'] ? '/el:1' : '' );
769745
}
770746

771747
$replacement .= '/q:';

inc/media_offload.php

Lines changed: 17 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,6 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
22212221

22222222
$url = get_post( $attachment_id );
22232223
$url = $url->guid;
2224-
$image_url = $this->get_new_offloaded_attachment_url( $url, $attachment_id );
22252224
$metadata = wp_get_attachment_metadata( $attachment_id );
22262225

22272226
// Use the original size if the requested size is full.
@@ -2244,65 +2243,26 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
22442243
];
22452244
}
22462245

2247-
$crop = false;
2248-
2249-
// Size can be int [] containing width and height.
2250-
if ( is_array( $size ) ) {
2251-
$width = $size[0];
2252-
$height = $size[1];
2253-
$crop = true;
2254-
} else {
2255-
$sizes = $this->get_all_image_sizes();
2256-
2257-
if ( ! isset( $sizes[ $size ] ) ) {
2258-
return [
2259-
$image_url,
2260-
$metadata['width'],
2261-
$metadata['height'],
2262-
false,
2263-
];
2264-
}
2265-
2266-
$width = $sizes[ $size ]['width'];
2267-
$height = $sizes[ $size ]['height'];
2268-
$crop = is_array( $sizes[ $size ]['crop'] ) ? $sizes[ $size ]['crop'] : (bool) $sizes[ $size ]['crop'];
2269-
}
2270-
2271-
$sizes2crop = self::size_to_crop();
2272-
22732246
if ( wp_attachment_is( 'video', $attachment_id ) && doing_action( 'wp_insert_post_data' ) ) {
22742247
return $image;
22752248
}
2276-
2277-
$resize = apply_filters( 'optml_default_crop', [] );
2278-
$data = image_get_intermediate_size( $attachment_id, $size );
2279-
2280-
if ( is_array( $data ) && isset( $data['width'] ) && isset( $data['height'] ) ) { // @phpstan-ignore-line - these both exist.
2281-
if ( isset( $sizes2crop[ $data['width'] . $data['height'] ] ) ) {
2282-
$resize = $this->to_optml_crop( $sizes2crop[ $data['width'] . $data['height'] ] );
2283-
}
2284-
}
2285-
2286-
if ( $crop !== false ) {
2287-
$resize = $this->to_optml_crop( $crop );
2288-
}
2289-
2249+
$sizes = $this->size_to_dimension( $size, $metadata );
22902250
$image_url = $this->get_new_offloaded_attachment_url(
22912251
$url,
22922252
$attachment_id,
22932253
[
2294-
'width' => $width,
2295-
'height' => $height,
2296-
'resize' => $resize,
2254+
'width' => $sizes['width'],
2255+
'height' => $sizes['height'],
2256+
'resize' => $sizes['resize'] ?? [],
22972257
'attachment_id' => $attachment_id,
22982258
]
22992259
);
23002260

23012261
return [
23022262
$image_url,
2303-
$width,
2304-
$height,
2305-
$crop,
2263+
$sizes['width'],
2264+
$sizes['height'],
2265+
$size === 'full',
23062266
];
23072267
}
23082268

@@ -2320,19 +2280,20 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
23202280
return $response;
23212281
}
23222282

2323-
$sizes = $this->get_all_image_sizes();
2283+
$meta = [];
2284+
if ( isset( $response['width'] ) ) {
2285+
$meta['width'] = $response['width'];
2286+
}
2287+
if ( isset( $response['height'] ) ) {
2288+
$meta['height'] = $response['height'];
2289+
}
2290+
$sizes = Optml_App_Replacer::image_sizes();
23242291

23252292
foreach ( $sizes as $size => $args ) {
23262293
if ( isset( $response['sizes'][ $size ] ) ) {
23272294
continue;
23282295
}
2329-
2330-
$args = [
2331-
'height' => $args['height'],
2332-
'width' => $args['width'],
2333-
'crop' => true,
2334-
];
2335-
2296+
$args = $this->size_to_dimension( $size, $meta );
23362297
$response['sizes'][ $size ] = array_merge(
23372298
$args,
23382299
[
@@ -2341,14 +2302,7 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
23412302
]
23422303
);
23432304
}
2344-
2345-
$url_args = [
2346-
'height' => $response['height'],
2347-
'width' => $response['width'],
2348-
'crop' => false,
2349-
];
2350-
2351-
$response['url'] = $this->get_new_offloaded_attachment_url( $response['url'], $attachment->ID, $url_args );
2305+
$response['url'] = $this->get_new_offloaded_attachment_url( $response['url'], $attachment->ID, $meta );
23522306

23532307
return $response;
23542308
}

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/dam_offload_utils.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
trait Optml_Dam_Offload_Utils {
4+
use Optml_Normalizer;
5+
46
/**
57
* Checks that the attachment is a DAM image.
68
*
@@ -157,7 +159,6 @@ private function is_attachment_edit_page( $attachment_id ) {
157159
* @return mixed
158160
*/
159161
private function get_altered_metadata_for_remote_images( $metadata, $id ) {
160-
$sizes = $this->get_all_image_sizes();
161162

162163
$post = get_post( $id );
163164

@@ -174,24 +175,14 @@ private function get_altered_metadata_for_remote_images( $metadata, $id ) {
174175
if ( ! isset( $metadata['height'] ) || ! isset( $metadata['width'] ) ) {
175176
return $metadata;
176177
}
177-
178+
$sizes = Optml_App_Replacer::image_sizes();
178179
foreach ( $sizes as $size => $args ) {
179180
// check if the image is portrait or landscape using attachment metadata.
180-
$is_portrait = $metadata['height'] > $metadata['width'];
181-
182-
// proportionally set the width/height based on this if image is uncropped.
183-
if ( ! (bool) $args['crop'] ) {
184-
if ( $is_portrait ) {
185-
$args['width'] = (int) ( $args['height'] * round( $metadata['width'] / $metadata['height'] ) );
186-
} else {
187-
$args['height'] = (int) ( $args['width'] * round( $metadata['height'] / $metadata['width'] ) );
188-
}
189-
}
190-
181+
$dimensions = $this->size_to_dimension( $size, $metadata );
191182
$sizes_meta[ $size ] = [
192183
'file' => $metadata['file'],
193-
'width' => $args['width'],
194-
'height' => $args['height'],
184+
'width' => $dimensions['width'],
185+
'height' => $dimensions['height'],
195186
'mime-type' => $post->post_mime_type,
196187
];
197188
}

0 commit comments

Comments
 (0)