Make WordPress Core

Opened 6 weeks ago

Last modified 3 days ago

#64480 new defect (bug)

WordPress PHP Deprecated: Implicit conversion from float 44.168

Reported by: sotnas's profile sotnas Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.9
Component: Media Keywords: reporter-feedback
Focuses: Cc:

Description

Hi,

WP V6.9
PHP 8.1
And give me

Wordpress PHP Deprecated: Implicit conversion from float 44.168 to int loses precision in /home/cpanel/public_html/wp-includes/media.php on line 1500

In wp-content/debug.log

Why?

Change History (4)

#1 @mindctrl
6 weeks ago

  • Keywords reporter-feedback added

@sotnas Welcome to Trac. Can you provide more information to help troubleshoot the issue? What steps are you taking to trigger this notice? Would you mind installing the Test Reports plugin and sharing the Bug Report for Trac? https://wordpress.org/plugins/test-reports/

#2 follow-up: @Presskopp
6 weeks ago

I assume the error comes from this block of code:

// The 'src' image has to be the first in the 'srcset', because of a bug in iOS8. See #35030.
	if ( $is_src ) {
		$sources = array( $image['width'] => $source ) + $sources;
	} else {
		$sources[ $image['width'] ] = $source;
	}

Since $image['width'] is used as an array key, an implicit float-to-int conversion can occur under PHP 8.1.
Casting the value explicitly beforehand should resolve the warning:

$width = (int) $image['width'];

and then using $width as the array key.

That said, the open question is why $image['width'] can be a float in the first place, given that image dimensions are generally expected to be integers.

#3 @sabernhardt
6 weeks ago

Could the site have an SVG attachment with 44.168 as the width or a value in the viewBox attribute?

Safe SVG uses floatval() to set dimensions for SVG, but it gave me integers when I tried uploading the following graphics:

1. viewBox-20.2x20-safe.svg
   dimensions: 20 × 20, from viewBox
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20.2 20"><circle cx="7" cy="7" r="5"/><circle cx="9" cy="9" r="5"/></svg>

2. 20.2w20h-safe.svg
   dimensions: 202 × 200, from viewBox
<svg xmlns="http://www.w3.org/2000/svg" width="20.2" height="20" viewBox="0 0 202 200"><circle cx="100" cy="100" r="100"/></svg>

3. 20.2w20h-safe2.svg
   dimensions: 20 × 20, from width and height attributes because I used
   add_filter( 'safe_svg_use_width_height_attributes', '__return_true' );
<svg xmlns="http://www.w3.org/2000/svg" width="20.2" height="20" viewBox="0 0 202 200"><circle cx="100" cy="100" r="100"/></svg>

#4 in reply to: ↑ 2 @sabernhardt
6 weeks ago

Casting the value explicitly beforehand should resolve the warning

The wp_image_matches_ratio() function in line 1488 expects all four parameters to be integers.

If $image['width'] is not an integer, I would suggest using either

  1. continue to try another image size in the loop or
  2. return false (wp_calculate_image_srcset() returns "False on error or when only one source exists").

In this function, I think I prefer returning false when encountering unexpected input instead of adding continue, casting, or _doing_it_wrong(). Then the image simply would not get a srcset attribute.

Float values might create problems elsewhere too, but then the other place(s) could be better to give an error message or warning.

Last edited 3 days ago by sabernhardt (previous) (diff)
Note: See TracTickets for help on using tickets.