Aspect ratio value in wp_calculate_image_srcset() breaking the function
-
I’ve done a lot of debugging and figured I’ve discovered the problem.
It started when I was getting “false” returned from the function wp_get_attachment_image_srcset. wp_get_attachment_image_srcset calls the function wp_calculate_image_srcset, and that was where the problem was.
The image I was using was 1080*1920 – and was just a placeholder image for while I got the site working. (if it matters the exact image, I got it from here, it was the one with the marinated steak being prodded with a fork on a barbeque, you’ll have to refresh until it comes up.)
I tracked the problem down to this part of the source code:
// If the new ratio differs by less than 0.002, use it. if ( abs( $image_ratio - $image_ratio_compare ) < 0.002 ) { // Add the URL, descriptor, and value to the sources array to be returned. $sources[ $image['width'] ] = array( 'url' => $image_baseurl . $image['file'], 'descriptor' => 'w', 'value' => $image['width'], ); }
$image_ratio is calculated from the src image and if $image_ratio_compare isn’t within 0.002 of the src’s ratio that particular image won’t get added to the srcset. All of my images were filters out, except the original src. I don’t know why this is. It might be because the image was portrait in such a high aspect ratio? Either way, this seems like a bug to me – the value is so low that it’s filtering out legitimate srcset images.
I’ve got around it by adding a filter that basically reuses all the code except changes the 0.002 value to 0.005, but it really was a bother tracking this down and I can’t imagine I’m the first to encounter this issue (though I think I am the first to post about it)
So this is less of a request for help and more of a “if this helps anyone” and a potential bug report.
- The topic ‘Aspect ratio value in wp_calculate_image_srcset() breaking the function’ is closed to new replies.