Skip to content

Fix aspect ratio handling for gravity crops#45

Merged
TorstenDittmann merged 1 commit into
mainfrom
fix/gravity-crop
Jul 13, 2026
Merged

Fix aspect ratio handling for gravity crops#45
TorstenDittmann merged 1 commit into
mainfrom
fix/gravity-crop

Conversation

@TorstenDittmann

Copy link
Copy Markdown
Contributor

Summary

  • calculate non-center gravity resize dimensions from source and target aspect ratios
  • preserve image proportions while ensuring the resized image fully covers the crop area
  • add pixel-level coverage for every gravity across horizontal and vertical overflow
  • add a regression test for aspect-ratio preservation during upscaling

Testing

  • ./vendor/bin/phpunit --filter gravity (28 tests, 145 assertions)
  • composer lint
  • composer check
  • composer test reaches 51 tests; 3 existing AVIF/HEIC output tests fail locally because the installed ImageMagick delegates cannot encode HEIC and reject the AVIF lossless parameters

@greptile-apps

greptile-apps Bot commented Jul 13, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes a two-part bug in the non-center gravity crop path: it replaces an incorrect raw pixel-dimension comparison ($width > $height) with a correct aspect-ratio comparison ($targetAspect > $originalAspect), and fixes a formula error where resizeHeight was computed as $width * $originalAspect (multiplication) instead of $width / $originalAspect (division). The combined effect was that portrait source images could be scaled too small to cover the crop area, producing out-of-bounds crops.

  • src/Image/Image.php: The resize dimensions for non-center gravities are now derived from both the source and target aspect ratios using ceil to guarantee full coverage of the crop rectangle.
  • tests/Image/ImageTest.php: Adds a regression test for aspect-ratio preservation during upscaling and an 18-case data-driven test verifying pixel-level gravity positioning for every gravity constant in both horizontal and vertical overflow scenarios.

Confidence Score: 5/5

The change is a targeted, well-reasoned fix to the non-center gravity resize path with comprehensive test coverage; safe to merge.

Both changes in Image.php are mathematically correct: switching from raw pixel comparison to aspect-ratio comparison ensures the right dimension is pinned, and replacing $width * $originalAspect with ceil($width / $originalAspect) fixes the formula that previously under-calculated resizeHeight for portrait sources. The new tests exercise all nine gravity constants across both overflow directions with pixel-level color assertions.

No files require special attention.

Important Files Changed

Filename Overview
src/Image/Image.php Fixes non-center gravity crop: replaces raw pixel dimension comparison with aspect-ratio comparison and corrects resizeHeight formula (multiplication to division). Logic is mathematically sound.
tests/Image/ImageTest.php Adds a regression test for aspect-ratio preservation during upscaling and a data-driven test covering all 18 gravity+orientation combinations with pixel-level color assertions.

Reviews (1): Last reviewed commit: "fix: preserve aspect ratio for gravity c..." | Re-trigger Greptile

@TorstenDittmann
TorstenDittmann merged commit ed06c72 into main Jul 13, 2026
5 checks passed
@TorstenDittmann
TorstenDittmann deleted the fix/gravity-crop branch July 13, 2026 12:12
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