Skip to content

Commit 9b4f95b

Browse files
committed
simplify logic
1 parent 8657a3d commit 9b4f95b

File tree

4 files changed

+19
-37
lines changed

4 files changed

+19
-37
lines changed

inc/dam.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ public function alter_img_tag_w_h( $dimensions, $image_src, $image_meta, $attach
678678
$width = $incoming_size[0];
679679
$height = $incoming_size[1];
680680

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

683683
// If this is an image size. Return its dimensions.
684684
foreach ( $sizes as $size => $args ) {

inc/media_offload.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,7 +2253,7 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
22532253
[
22542254
'width' => $sizes['width'],
22552255
'height' => $sizes['height'],
2256-
'resize' => $sizes['resize'],
2256+
'resize' => $sizes['resize'] ?? [],
22572257
'attachment_id' => $attachment_id,
22582258
]
22592259
);
@@ -2280,19 +2280,20 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
22802280
return $response;
22812281
}
22822282

2283-
$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();
22842291

22852292
foreach ( $sizes as $size => $args ) {
22862293
if ( isset( $response['sizes'][ $size ] ) ) {
22872294
continue;
22882295
}
2289-
2290-
$args = [
2291-
'height' => $args['height'],
2292-
'width' => $args['width'],
2293-
'crop' => true,
2294-
];
2295-
2296+
$args = $this->size_to_dimension( $size, $meta );
22962297
$response['sizes'][ $size ] = array_merge(
22972298
$args,
22982299
[
@@ -2301,14 +2302,7 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
23012302
]
23022303
);
23032304
}
2304-
2305-
$url_args = [
2306-
'height' => $response['height'],
2307-
'width' => $response['width'],
2308-
'crop' => false,
2309-
];
2310-
2311-
$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 );
23122306

23132307
return $response;
23142308
}

inc/traits/dam_offload_utils.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private function is_attachment_edit_page( $attachment_id ) {
157157
* @return mixed
158158
*/
159159
private function get_altered_metadata_for_remote_images( $metadata, $id ) {
160-
$sizes = $this->get_all_image_sizes();
160+
161161

162162
$post = get_post( $id );
163163

@@ -174,24 +174,14 @@ private function get_altered_metadata_for_remote_images( $metadata, $id ) {
174174
if ( ! isset( $metadata['height'] ) || ! isset( $metadata['width'] ) ) {
175175
return $metadata;
176176
}
177-
177+
$sizes = Optml_App_Replacer::image_sizes();
178178
foreach ( $sizes as $size => $args ) {
179179
// 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-
180+
$dimensions = $this->size_to_dimension( $size, $metadata );
191181
$sizes_meta[ $size ] = [
192182
'file' => $metadata['file'],
193-
'width' => $args['width'],
194-
'height' => $args['height'],
183+
'width' => $dimensions['width'],
184+
'height' => $dimensions['height'],
195185
'mime-type' => $post->post_mime_type,
196186
];
197187
}

tests/test-media.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,7 @@ public function test_alter_attachment_image_src() {
543543
'data' => Optml_Media_Offload::instance()->alter_attachment_image_src( [], self::$sample_attachement, [10, 20], false ),
544544
'contains' => [
545545
'process:' . self::$sample_attachement . '/id:',
546-
'w:10/h:20',
547-
'rt:fill'
546+
'w:10/h:7'
548547
]
549548
],
550549
'scaled-full' => [
@@ -572,8 +571,7 @@ public function test_alter_attachment_image_src() {
572571
'data' => Optml_Media_Offload::instance()->alter_attachment_image_src( [], self::$sample_attachment_scaled, [10, 20], false ),
573572
'contains' => [
574573
'process:' . self::$sample_attachment_scaled . '/id:',
575-
'w:10/h:20',
576-
'rt:fill'
574+
'w:10/h:10'
577575
],
578576
'not_contain' => [
579577
'-scaled.jpg'

0 commit comments

Comments
 (0)