Problem/Motivation
An image style using the aspect switcher effect may cause a division by zero PHP error if the source image is corrupted. Marked as major because this bug took down an important production site without warning.
The website encountered an unexpected error. Please try again later.
DivisionByZeroError: Division by zero in Drupal\image_effects\Plugin\ImageEffect\AspectSwitcherImageEffect->getChildImageStyleToExecute() (line 200 of modules/contrib/image_effects/src/Plugin/ImageEffect/AspectSwitcherImageEffect.php).Steps to reproduce
- Create an image style with at least one aspect switcher effect
- Apply the image style to an image field display formatter (i.e. on a content type)
- Upload a corrupted image to the image field and save
- Attempt to view the image field via the display formatter
An example corrupted image is attached. It's a plain text file with a JPEG extension.
Proposed resolution
Verify the image toolkit was able to get the image dimensions before attempting to apply transformations.
| Comment | File | Size | Author |
|---|---|---|---|
| corrupted.jpg | 16 bytes | bobooon |
Issue fork image_effects-3357133
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
bobooon commentedPatch from MR. If either width or height is not a positive number do not return a style name.
https://git.drupalcode.org/project/image_effects/-/merge_requests/32/dif...
Comment #4
mondrakeThanks.
1) instead of returning in getChildImageStyleToExecute, I'd rather avoid to call it in applyEffect and transformDimensions when the image is invalid.
2) an automated test would help prevent regressions.
Comment #5
aporieThere is a duplicated issue #3136292: Division by zero in AspectSwitcherImageEffect which patch caused us
This patch is fine though (and seems an easier approach) than the attached duplicated issue.
Comment #6
aporieComment #7
mvonfrie commentedMR !32 works, but this is only half of the truth. In my case the image was not corrupted but the thumbnail of a document media entity embedded in a body field via CKeditor.
Steps to reproduce
Comment #8
bobooon commented