-
-
Notifications
You must be signed in to change notification settings - Fork 34
Added php8.0 features to Resizer #77
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
base: wip/1.3
Are you sure you want to change the base?
Conversation
|
There maybe a bug when trying to resize in crop mode where the calculated values ( Need to add more tests for better coverage of the |
|
@jaxwilko can you remerge develop into this branch and then let me know what's left on it? |
WalkthroughA single file, Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (5)
src/Database/Attach/Resizer.php (5)
359-364: Useround()for crop offsets to prevent pixel shift.
The calculation produces floats ($optimalWidth / 2and$newWidth / 2), butcrop()expectsintparameters. PHP implicitly truncates floats to integers, which can misalign the crop by up to 1 pixel. Rounding provides more accurate centering for this calculation.Suggested fix
- $cropStartX = ($optimalWidth / 2) - ($newWidth / 2) - $offset[0]; - $cropStartY = ($optimalHeight / 2) - ($newHeight / 2) - $offset[1]; + $cropStartX = (int) round(($optimalWidth / 2) - ($newWidth / 2) - $offset[0]); + $cropStartY = (int) round(($optimalHeight / 2) - ($newHeight / 2) - $offset[1]);
523-555: Add validation for failed image creation before returning.
imagecreatefrom*()functions returnfalseon failure (e.g., corrupted or invalid image files), but the function signature declaresGdImageas the return type without a union. Returningfalseviolates the type contract and will cause type-checking failures. Additionally,retainImageTransparency()is called with potentiallyfalsevalues, causing secondary type violations.Suggested fix
- return $img; + if ($img === false) { + throw new Exception('Failed to open image from file.'); + } + return $img;
320-347: Add error checks after GD function calls before assigning to typedGdImageproperties.
getRotatedOriginal(),imagescale(), andimagecreatetruecolor()can returnfalseon failure (memory exhaustion, invalid dimensions, etc.). With typedGdImageproperties, assigningfalsewill throw aTypeError. Add explicit checks:Suggested fix
// Get the original image rotated according to exif orientation $rotatedOriginal = $this->getRotatedOriginal(); + if ($rotatedOriginal === false) { + throw new Exception('Failed to rotate source image.'); + } if ($this->mime === 'image/gif') { // Use imagescale() for GIFs, as it produces better results $imageResized = imagescale($rotatedOriginal, $optimalWidth, $optimalHeight, IMG_NEAREST_NEIGHBOUR); + if ($imageResized === false) { + throw new Exception('Failed to scale image.'); + } $this->retainImageTransparency($imageResized); } else { // Resample - create image canvas of x, y size $imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight); + if ($imageResized === false) { + throw new Exception('Failed to create resize canvas.'); + } $this->retainImageTransparency($imageResized);
81-107: Guard against null MIME/extension and initialize$this->file.
Symfony\Component\HttpFoundation\File\File::guessExtension()andgetMimeType()both return?string(nullable), which will throw aTypeErrorwhen assigned to the non-nullable$extensionand$mimeproperties. Additionally, the$fileproperty declared on line 34 is never initialized in the constructor.Suggested fix
+ $this->file = $file; // Get the file extension - $this->extension = $file->guessExtension(); - $this->mime = $file->getMimeType(); + $extension = $file->guessExtension() ?: pathinfo($file->getPathname(), PATHINFO_EXTENSION); + $mime = $file->getMimeType(); + if (!$extension || !$mime) { + throw new Exception('Unable to determine image extension or MIME type.'); + } + $this->extension = $extension; + $this->mime = $mime;
410-433: Use nullable ints for optional params and guard crop canvas creation.The parameters
int $srcWidth = nullandint $srcHeight = nulluse implicit nullable syntax, which is invalid in PHP 8.2+. They must be explicitly declared as?int $srcWidth = nulland?int $srcHeight = null.Additionally,
imagecreatetruecolor()returnsGdImage|false, but the result is passed directly toretainImageTransparency()which expectsGdImagewithout checking for failure. Add a guard to handle the false case:✅ Suggested fix
- int $srcWidth = null, - int $srcHeight = null + ?int $srcWidth = null, + ?int $srcHeight = null ): static { ... - $imageResized = imagecreatetruecolor($newWidth, $newHeight); + $imageResized = imagecreatetruecolor($newWidth, $newHeight); + if ($imageResized === false) { + throw new Exception('Failed to create crop canvas.'); + }
|
@jaxwilko the robot has some feedback |
|
@LukeTowers robot knows not of the fact that variables type hinted as int and docblocked as int are actually floats that get chopped when passing because php is weird |
A simple PR that adds some php8.0 features and enforces return types.
Something of note, all the widths & heights should probably be
ints but in reality they work offfloats and changing them tointbreaks the tests.This PR lays groundwork for future improvements.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.