Skip to content

fix: use native Imagick for AVIF and HEIC output instead of magick convert#43

Merged
loks0n merged 3 commits intomainfrom
fix/avif-native-imagick
Apr 17, 2026
Merged

fix: use native Imagick for AVIF and HEIC output instead of magick convert#43
loks0n merged 3 commits intomainfrom
fix/avif-native-imagick

Conversation

@loks0n
Copy link
Copy Markdown
Contributor

@loks0n loks0n commented Apr 17, 2026

Summary

  • AVIF and HEIC output previously shelled out to magick convert, which is unreliable in containerised environments where the binary may not be on PATH
  • ImageMagick 7 with the heic/avif delegates exposes AVIF and HEIC encoding directly through the Imagick PHP extension — no shell exec needed
  • Switch the avif/heic case to the same native pattern used by jpg, png, and gif: setImageCompressionQuality + setImageFormat, then let the shared getImagesBlob/writeImages path handle output

Test plan

  • Convert a JPEG/PNG to AVIF via the preview endpoint and verify image/avif content-type and non-empty body
  • Convert a JPEG/PNG to HEIC via the preview endpoint and verify image/heic content-type and non-empty body
  • Verify quality parameter is respected (compare file sizes at different quality settings)
  • Confirm no temp files are left behind

🤖 Generated with Claude Code

loks0n and others added 2 commits April 17, 2026 15:24
…nvert

The shell-based `magick convert` path is unreliable in containerised
environments where the binary may not be on PATH, and is unnecessary
because ImageMagick 7 (with the heic/avif delegates built in) exposes
AVIF and HEIC encoding directly through the Imagick PHP extension.

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

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Apr 17, 2026

Greptile Summary

This PR replaces the shell-exec-based magick convert path for AVIF and HEIC output with native Imagick PHP extension calls (setImageFormat + setCompressionQuality), bringing these formats in line with the jpg/png/gif pattern. The implementation correctly uses the object-level setCompressionQuality() instead of setImageCompressionQuality() for AVIF/HEIC, with a well-documented comment explaining the libheif coder quirk (Imagick issue #711).

Confidence Score: 5/5

Safe to merge — clean refactor with no new bugs introduced and a real reliability problem fixed.

All findings are P2 or lower. The core logic correctly mirrors the existing jpg/png/gif pattern, the quality quirk is well-documented and technically justified, and the prior resource-cleanup concern (blob path) pre-exists across all formats and was already noted. No blocking issues remain.

No files require special attention.

Important Files Changed

Filename Overview
src/Image/Image.php Removes 55 lines of shell-exec logic for AVIF/HEIC; replaces with 4-line native Imagick path consistent with other formats. Quality handling correctly uses object-level setCompressionQuality() per documented Imagick/libheif behaviour.
tests/Image/ImageTest.php Updates expected HEIC file size from 8426 to 8490 bytes to match native-encoder output; all other test assertions are unchanged.

Reviews (3): Last reviewed commit: "fix: use setCompressionQuality for AVIF/..." | Re-trigger Greptile

Comment thread src/Image/Image.php
…essionQuality

setImageCompressionQuality() is silently ignored by the libheif coder in
the Imagick PHP extension — quality had no effect and all output was
encoded at the default level regardless of the requested value.

The fix is to call setCompressionQuality() (object-level) after
setImageFormat(), which correctly passes quality through to libheif.
See: Imagick/imagick#711

Also updates the HEIC q100 expected file size in tests (8426 → 8490)
to reflect the minor encoding difference between native Imagick and
the old magick convert path at the same quality level.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@loks0n loks0n merged commit 9af2fcf into main Apr 17, 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.

2 participants