Skip to content

Commit 82d7364

Browse files
Use float value for more accuracy
1 parent 90dc20f commit 82d7364

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $
201201
case 'wide':
202202
$layout_width = auto_sizes_get_layout_width( 'wide' );
203203
if ( str_ends_with( $layout_width, 'px' ) ) {
204-
$layout_width = (int) $layout_width * $container_relative_width;
204+
// First remove 'px' from width.
205+
$layout_width = str_replace( 'px', '', $layout_width );
206+
// Convert to float for better precision.
207+
$layout_width = (float) $layout_width * $container_relative_width;
205208
$layout_width = sprintf( '%dpx', (int) $layout_width );
206209
}
207210
break;
@@ -218,7 +221,10 @@ function auto_sizes_calculate_better_sizes( int $id, $size, string $align, int $
218221
* on the server. Otherwise, we need to rely on CSS functions.
219222
*/
220223
if ( str_ends_with( $layout_width, 'px' ) ) {
221-
$layout_width = (int) $layout_width * $container_relative_width;
224+
// First remove 'px' from width.
225+
$layout_width = str_replace( 'px', '', $layout_width );
226+
// Convert to float for better precision.
227+
$layout_width = (float) $layout_width * $container_relative_width;
222228
$layout_width = sprintf( '%dpx', min( (int) $layout_width, $image_width ) );
223229
} else {
224230
$layout_width = sprintf( 'min(%1$s, %2$spx)', $layout_width, $image_width );

0 commit comments

Comments
 (0)