Skip to content

Fix "Cannot use bool as array" error in ImageValidationTrait#18

Merged
dereuromark merged 3 commits intomasterfrom
fix/handle-getimagesize-false
Mar 25, 2026
Merged

Fix "Cannot use bool as array" error in ImageValidationTrait#18
dereuromark merged 3 commits intomasterfrom
fix/handle-getimagesize-false

Conversation

@dereuromark
Copy link
Copy Markdown
Owner

@dereuromark dereuromark commented Mar 25, 2026

Summary

  • Handle getimagesize() returning false for non-image files
  • Add isValidImage() method for clearer error messages

Problem

When a file passes the upload validation (UPLOAD_ERR_OK) but is not a valid image (e.g., PDF, corrupted file, unsupported format), getimagesize() returns false.

The current code uses array destructuring directly on the result:

[$imgWidth] = getimagesize($file);

This causes a fatal error:

Cannot use bool as array in ImageValidationTrait.php on line 28

Additionally, if the error is handled, users get confusing messages like "This image should at least be 50px wide" for non-image uploads.

Solution

  1. Fix the crash: Store the result and check for false before accessing dimensions in all four dimension methods.

  2. 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

$validator->add('file', 'fileIsValidImage', [
    'rule' => 'isValidImage',
    'message' => 'File must be a valid image (JPEG, PNG, GIF, or WebP)',
    'provider' => 'upload',
]);

// Or with custom allowed types:
$validator->add('file', 'fileIsValidImage', [
    'rule' => ['isValidImage', [IMAGETYPE_JPEG, IMAGETYPE_PNG]],
    'message' => 'File must be a JPEG or PNG image',
    'provider' => 'upload',
]);

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-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 25, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 96.42857% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 73.01%. Comparing base (5d49fd4) to head (58594f3).

Files with missing lines Patch % Lines
src/Model/Validation/ImageValidationTrait.php 96.42% 1 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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
@dereuromark dereuromark merged commit 5f45286 into master Mar 25, 2026
16 checks passed
@dereuromark dereuromark deleted the fix/handle-getimagesize-false branch March 25, 2026 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants