Skip to content

Commit 83be032

Browse files
Return accurate sizes for center alignment
1 parent 8ca8b27 commit 83be032

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

plugins/auto-sizes/includes/improve-calculate-sizes.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
8282
*/
8383
$filter = static function ( $sizes, $size ) use ( $block ) {
8484

85+
$block_name = $block->name;
8586
$id = $block->attributes['id'] ?? 0;
8687
$alignment = $block->attributes['align'] ?? '';
8788
$width = $block->attributes['width'] ?? '';
8889
$max_alignment = $block->context['max_alignment'];
8990

90-
$better_sizes = auto_sizes_calculate_better_sizes( (int) $id, (string) $size, (string) $alignment, (string) $width, (string) $max_alignment );
91+
$better_sizes = auto_sizes_calculate_better_sizes( (string) $block_name, (int) $id, (string) $size, (string) $alignment, (string) $width, (string) $max_alignment );
9192

9293
// If better sizes can't be calculated, use the default sizes.
9394
return false !== $better_sizes ? $better_sizes : $sizes;
@@ -124,14 +125,15 @@ function auto_sizes_filter_image_tag( $content, array $parsed_block, WP_Block $b
124125
*
125126
* @since n.e.x.t
126127
*
128+
* @param string $block_name The block name.
127129
* @param int $id The image id.
128130
* @param string $size The image size data.
129131
* @param string $align The image alignment.
130132
* @param string $resize_width Resize image width.
131133
* @param string $max_alignment The maximum usable layout alignment.
132134
* @return string|false An improved sizes attribute or false if a better size cannot be calculated.
133135
*/
134-
function auto_sizes_calculate_better_sizes( int $id, string $size, string $align, string $resize_width, string $max_alignment ) {
136+
function auto_sizes_calculate_better_sizes( string $block_name, int $id, string $size, string $align, string $resize_width, string $max_alignment ) {
135137
// Without an image ID or a resize width, we cannot calculate a better size.
136138
if ( ! (bool) $id && ! (bool) $resize_width ) {
137139
return false;
@@ -166,9 +168,9 @@ function auto_sizes_calculate_better_sizes( int $id, string $size, string $align
166168
*/
167169
$constraints = array(
168170
'full' => 0,
169-
'left' => 1,
170-
'right' => 1,
171-
'wide' => 2,
171+
'wide' => 1,
172+
'left' => 2,
173+
'right' => 2,
172174
'default' => 3,
173175
'center' => 3,
174176
);
@@ -187,9 +189,14 @@ function auto_sizes_calculate_better_sizes( int $id, string $size, string $align
187189

188190
case 'left':
189191
case 'right':
190-
// These alignment get constrained by the wide layout size but do not get stretched.
191-
$alignment = auto_sizes_get_layout_width( 'wide' );
192-
$layout_width = sprintf( '%1$spx', min( (int) $alignment, $image_width ) );
192+
/*
193+
* Update width for cover block.
194+
* See https://github.com/WordPress/gutenberg/blob/938720602082dc50a1746bd2e33faa3d3a6096d4/packages/block-library/src/cover/style.scss#L82-L87.
195+
*/
196+
if ( 'core/cover' === $block_name ) {
197+
$image_width = $image_width * 0.5;
198+
}
199+
$layout_width = sprintf( '%1$spx', $image_width );
193200
break;
194201

195202
case 'center':

plugins/auto-sizes/tests/test-improve-calculate-sizes.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public function data_image_sizes_for_left_right_center_alignment(): array {
341341
}
342342

343343
/**
344-
* Test the cover block with left and right alignment.
344+
* Test the cover block with left, right and center alignment.
345345
*
346346
* @dataProvider data_image_left_right_center_alignment
347347
*
@@ -368,8 +368,8 @@ public function test_cover_block_with_left_right_center_alignment( string $align
368368
*/
369369
public function data_image_left_right_center_alignment(): array {
370370
return array(
371-
array( 'left', 'sizes="(max-width: 420px) 100vw, 420px' ),
372-
array( 'right', 'sizes="(max-width: 420px) 100vw, 420px' ),
371+
array( 'left', 'sizes="(max-width: 540px) 100vw, 540px' ),
372+
array( 'right', 'sizes="(max-width: 540px) 100vw, 540px' ),
373373
array( 'center', 'sizes="(max-width: 620px) 100vw, 620px' ),
374374
);
375375
}
@@ -469,10 +469,10 @@ public function data_ancestor_and_image_block_alignment(): array {
469469
'left',
470470
'sizes="(max-width: 1024px) 100vw, 1024px" ',
471471
),
472-
'Return image size 1024px, parent block wide alignment, image block center alignment' => array(
472+
'Return image size 620px, parent block wide alignment, image block center alignment' => array(
473473
'wide',
474474
'center',
475-
'sizes="(max-width: 1024px) 100vw, 1024px" ',
475+
'sizes="(max-width: 620px) 100vw, 620px" ',
476476
),
477477
'Return image size 1024px, parent block wide alignment, image block right alignment' => array(
478478
'wide',
@@ -501,10 +501,10 @@ public function data_ancestor_and_image_block_alignment(): array {
501501
'left',
502502
'sizes="(max-width: 1024px) 100vw, 1024px" ',
503503
),
504-
'Return image size 1024px, parent block full alignment, image block center alignment' => array(
504+
'Return image size 620px, parent block full alignment, image block center alignment' => array(
505505
'full',
506506
'center',
507-
'sizes="(max-width: 1024px) 100vw, 1024px" ',
507+
'sizes="(max-width: 620px) 100vw, 620px" ',
508508
),
509509
'Return image size 1024px, parent block full alignment, image block right alignment' => array(
510510
'full',

0 commit comments

Comments
 (0)