smart-media icon indicating copy to clipboard operation
smart-media copied to clipboard

Division by zero when an image size has a height of 0, affects WooCommerce

Open johnbillion opened this issue 2 years ago • 6 comments

The \HM\Media\Cropper\nearest_defined_crop_size() function triggers a division by zero warning if one of the registered image sizes has a height property of 0.

This affects WooCommerce which registers image sizes such as woocommerce_single with a height of 0, although I couldn't immediately trace the root cause or reason it does so.

Should the array map callback in nearest_defined_crop_size() return 1 when the height is 0?

johnbillion avatar May 12 '22 12:05 johnbillion

A zero value for width or height should probably mean we try and calculate the intrinsic height and use that value first. Any thoughts on that @goldenapples ?

roborourke avatar May 13 '22 11:05 roborourke

@goldenapples some input here will be helpful if you have any?

veselala avatar Jun 15 '22 08:06 veselala

I think we can move forward without bothering Than, it’s a clear bug that we could do with addressing in the function mentioned in the original comment here

roborourke avatar Jun 15 '22 09:06 roborourke

Yeah, I also can't think of why an image size would be registered with 0 as a dimension. But it seems safe to ignore that value if it exists in the nearest crop sizes.

goldenapples avatar Jun 16 '22 11:06 goldenapples

Zero is a shorthand for any size - only used sense for non cropped sizes.

roborourke avatar Jun 16 '22 12:06 roborourke

I'm unable to replicate this issue at the moment. Will loop back to it at a later stage. Unless anyone has clearer steps to replicate?

robindevitt avatar Aug 25 '22 07:08 robindevitt

We had this issue on a client project that is upgrading PHP from 7.4 to 8. In 7.4, this throws a warning, but in PHP 8 this throws a fatal error.

Screenshot 2022-11-21 at 14 30 52

In our instance, problem was being caused by requesting the image at an undefined size (post-thumbnail was being used but had not been set). Switching to a valid image size resolved the issue.

sambulance avatar Nov 21 '22 14:11 sambulance

@mikelittle might be worth fixing this soon with folks updating to PHP 8

roborourke avatar Nov 21 '22 15:11 roborourke