Fix infinite recursion and add configurable minScale in image-crop#286
Conversation
Fixed critical bug in getCroppedPngImage that caused infinite recursion when images exceeded maxImageSize. The scaleFactor parameter was never applied to canvas dimensions, resulting in identical output size on each recursive call. Changes: - Apply scaleFactor to canvas width/height to actually downscale images - Add configurable minScale parameter (default: 0.1) to prevent infinite loops - Enable imageSmoothingEnabled with 'high' quality for better downscaling - Add minScale prop to ImageCrop component for user configuration Users can now set minScale lower than 0.1 (e.g., 0.01 for 1%) if needed for stricter size requirements.
|
@NicholasJAlexander is attempting to deploy a commit to the Haste Ventures Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThe PR introduces a Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🔇 Additional comments (4)
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 |
Summary
Fixed infinite recursion bug in
getCroppedPngImagefunction and added configurable minimum scale threshold.Problem
The
getCroppedPngImagefunction had a bug causing infinite recursion when images exceededmaxImageSize:scaleFactorparameter was accepted but never applied to canvas dimensionsblob.size > maxImageSize, the function recursed infinitely with no way to terminateSolution
canvas.widthandcanvas.heightbyscaleFactorimageSmoothingEnabledfromfalsetotruewith'high'quality for better downscalingminScaleinImageCropPropsfor user configurationChanges
packages/image-crop/index.tsx(lines 52-110, 141-149, 151-160, 199-213)minScale: number = 0.1parameter togetCroppedPngImagefunctioncanvas.width = Math.max(1, Math.floor(pixelCrop.width * scaleFactor))canvas.height = Math.max(1, Math.floor(pixelCrop.height * scaleFactor))ctx.imageSmoothingEnabled = truewithctx.imageSmoothingQuality = "high"minScale?: numbertoImageCropPropstypeminScaleparameter through component to function callUsage Example
Testing
Summary by CodeRabbit
New Features
Improvements