Skip to content

fix: use native Imagick for WebP output#44

Merged
loks0n merged 1 commit intomainfrom
fix/webp-native
Apr 19, 2026
Merged

fix: use native Imagick for WebP output#44
loks0n merged 1 commit intomainfrom
fix/webp-native

Conversation

@loks0n
Copy link
Copy Markdown
Contributor

@loks0n loks0n commented Apr 19, 2026

Summary

  • Remove the cwebp shell fallback in the webp save path — originally added in the 2021 initial commit to guard against Imagick builds lacking libwebp, but the utopia-base image now ships Imagick with webp support
  • Switch to the same native pattern used by jpg, png, gif, avif, and heic: setImageCompressionQuality + setImageFormat('webp'), then fall through to the shared getImagesBlob/writeImages output path
  • Mirrors the AVIF/HEIC cleanup in fix: use native Imagick for AVIF and HEIC output instead of magick convert #43

Test plan

  • Existing webp tests pass (test_crop100x100_webp, test_crop100x100_webp_quality30)
  • New test_webp_blob_output — verifies output('webp') returns bytes with RIFF/WEBP magic and correct dimensions (covers the blob-return branch)
  • New test_webp_from_webp_input — verifies webp → webp roundtrip (covers readImageBlob on webp source)
  • Full suite: 32/32 phpunit, phpstan max, pint

🤖 Generated with Claude Code

The `cwebp` shell fallback dates back to the initial 2021 commit, when
it guarded against Imagick builds without libwebp delegate support. The
`utopia-base` image now ships Imagick with webp support, so the Imagick
path succeeds and the catch block has effectively stopped firing.

Switch to the same native pattern used for jpg, png, gif, avif, and
heic: setImageCompressionQuality + setImageFormat('webp'), then let the
shared getImagesBlob/writeImages path handle output.

Add two tests: blob-return via output() (covers the empty-path branch)
and webp-input roundtrip (covers readImageBlob on webp source).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Apr 19, 2026

Greptile Summary

Removes the legacy cwebp shell-command fallback for WebP output and replaces it with the same native Imagick pattern (setImageCompressionQuality + setImageFormat + shared getImagesBlob/writeImages path) already used by JPG, PNG, GIF, AVIF, and HEIC. The cleanup is straightforward and consistent with the rest of the output() method; two new tests cover the blob-return and webp→webp roundtrip paths.

Confidence Score: 5/5

Safe to merge — change is minimal, well-scoped, and aligns with existing patterns.

Only finding is a P2 style concern about an overly permissive format assertion in the new tests. No logic or correctness issues were found in the production code path.

No files require special attention.

Important Files Changed

Filename Overview
src/Image/Image.php Removes the cwebp shell fallback for WebP output, replacing it with the same native Imagick pattern (setImageCompressionQuality + setImageFormat + shared getImagesBlob/writeImages path) used by all other formats. Change is minimal and consistent.
tests/Image/ImageTest.php Adds two new WebP tests covering blob output and webp→webp roundtrip; both include a slightly permissive format assertion that accepts 'PAM' alongside 'WEBP', which could mask an incorrect output format in the roundtrip test.

Reviews (1): Last reviewed commit: "fix: use native Imagick for WebP output ..." | Re-trigger Greptile

Comment thread tests/Image/ImageTest.php
$probe->readImageBlob($blob);
$this->assertEquals(100, $probe->getImageWidth());
$this->assertEquals(100, $probe->getImageHeight());
$this->assertTrue(in_array($probe->getImageFormat(), ['PAM', 'WEBP']));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Overly permissive format assertion

'PAM' (Netpbm Portable Arbitrary Map) is accepted alongside 'WEBP' as a valid output format. In test_webp_blob_output this is harmless since the RIFF/WEBP magic-byte assertions on lines 425–426 already confirm the bytes are real WebP. But the same loose assertion is repeated in test_webp_from_webp_input (line 450) where there is no magic-byte guard, so a file saved in PAM format would silently pass the whole test. If PAM is a known quirk of a specific Imagick build, a brief comment would clarify the intent; otherwise tightening the assertion to 'WEBP' alone would give stronger coverage.

@loks0n loks0n merged commit 85ab702 into main Apr 19, 2026
5 checks passed
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.

1 participant