Fix "Cannot use bool as array" error in ImageValidationTrait#18
Merged
dereuromark merged 3 commits intomasterfrom Mar 25, 2026
Merged
Fix "Cannot use bool as array" error in ImageValidationTrait#18dereuromark merged 3 commits intomasterfrom
dereuromark merged 3 commits intomasterfrom
Conversation
getimagesize() returns false for non-image files (PDFs, corrupted files, unsupported formats). The array destructuring then fails with: "Cannot use bool as array" This affects all four dimension validation methods when a file passes the upload check (UPLOAD_ERR_OK) but is not a valid image. The fix stores the getimagesize() result and checks for false before accessing the dimensions.
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #18 +/- ##
============================================
+ Coverage 71.82% 73.01% +1.19%
- Complexity 231 241 +10
============================================
Files 14 14
Lines 788 808 +20
============================================
+ Hits 566 590 +24
+ Misses 222 218 -4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Allows checking if an uploaded file is a valid image before running dimension checks. This enables clearer error messages like "File must be a valid image" instead of confusing dimension errors for non-image uploads. Supports optional array of allowed IMAGETYPE_* constants, defaulting to JPEG, PNG, GIF, and WebP.
- Add comprehensive tests for isValidImage() method - Add tests verifying dimension methods handle non-image files gracefully - Update Validation.md with isValidImage usage examples - Document all available image validation methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
getimagesize()returningfalsefor non-image filesisValidImage()method for clearer error messagesProblem
When a file passes the upload validation (
UPLOAD_ERR_OK) but is not a valid image (e.g., PDF, corrupted file, unsupported format),getimagesize()returnsfalse.The current code uses array destructuring directly on the result:
This causes a fatal error:
Additionally, if the error is handled, users get confusing messages like "This image should at least be 50px wide" for non-image uploads.
Solution
Fix the crash: Store the result and check for
falsebefore accessing dimensions in all four dimension methods.Add
isValidImage()method: Allows explicit validation before dimension checks, enabling clear messages like "File must be a valid image (JPEG, PNG, GIF, or WebP)".Usage