Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve checks for image dimensions extraction to avoid warnings #6968

Closed
milindmore22 opened this issue Mar 9, 2022 · 2 comments · Fixed by #6973
Closed

Improve checks for image dimensions extraction to avoid warnings #6968

milindmore22 opened this issue Mar 9, 2022 · 2 comments · Fixed by #6973
Labels
Bug Something isn't working Changelogged Whether the issue/PR has been added to release notes.
Milestone

Comments

@milindmore22
Copy link
Collaborator

milindmore22 commented Mar 9, 2022

Bug Description

As per this support topic User seeing a warning while extracting a dimension of an image.

PHP Warning: Division by zero in /wp-content/plugins/amp/includes/sanitizers/class-amp-img-sanitizer.php on line 299

I am not able to reproduce the issue at my end by I think it might be unsupported image type or corrupted image, which returned 0 or false

As suggested by Weston we should replace isset() checks with empty() to avoid warning and apply fallback dimensions instead.

Expected Behaviour

Should not show any warrning

Screenshots

No response

PHP Version

7.4

Plugin Version

2.2.1

AMP plugin template mode

Reader

WordPress Version

5.9.1

Site Health

Site health : https://insights.amp-wp.org/admin/report/uuid/ampwp-2c37937f-1474-5871-856e-92cd1bf35954

Gutenberg Version

No response

OS(s) Affected

No response

Browser(s) Affected

No response

Device(s) Affected

No response

Acceptance Criteria

No response

Implementation Brief

No response

QA Testing Instructions

No response

Demo

No response

Changelog Entry

No response

@milindmore22 milindmore22 added the Bug Something isn't working label Mar 9, 2022
@westonruter westonruter added this to the v2.2.2 milestone Mar 9, 2022
@westonruter
Copy link
Member

This is the line in question:

$height = ( (float) $node->getAttribute( Attribute::WIDTH ) * $dimensions['height'] ) / $dimensions['width'];

Somehow $dimensions['width'] is 0. Fixing the symptom would be to just make sure that $dimensions['width'] > 0. I wonder if it is actually false here and it is being cast to 0. I wonder if instead of checking isset() if we should rather be checking ! empty() in this method.

The value for $dimensions['height'] is most likely coming from here:

$dimensions[ $url ] = [
'width' => (int) $image_size[0],
'height' => (int) $image_size[1],
];

So perhaps wp_getimagesize() is returning a bad value?

@westonruter westonruter changed the title Improve checks for image dimensions extraction to avoid warrnings Improve checks for image dimensions extraction to avoid warnings Mar 10, 2022
@pooja-muchandikar
Copy link

pooja-muchandikar commented Mar 24, 2022

QA Passed ✅

Images are rendering proper dimensions and no warnings are displayed.

Image type Actual Dimension Displayed Dimensions Warning
JPEG 534x340 534x340 NO
PNG 1080x1920 1080x1920 NO
JPG 1280x796 1280x796 NO
PNG 920x946 920x946 NO

@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Changelogged Whether the issue/PR has been added to release notes.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants