Skip to content

Convert palette images to truecolor before encoding avif with gd - #333

Merged
freekmurze merged 2 commits into
spatie:mainfrom
AlexisSerneels:fix/gd-avif-palette-images
Sep 30, 2026
Merged

freekmurze merged 2 commits into
spatie:mainfrom
AlexisSerneels:fix/gd-avif-palette-images

Conversation

@AlexisSerneels

Copy link
Copy Markdown
Contributor

Problem

The GD driver fails to save palette (indexed color) images as AVIF:

ErrorException: imageavif(): avif doesn't support palette images
in src/Drivers/Gd/GdDriver.php:172

This shows up in practice with PNG-8 images (common output of PNG optimizers). With spatie/laravel-medialibrary, any AVIF conversion of such an upload fails, so the conversion is never generated.

The WebP branch already handles this by calling imagepalettetotruecolor() before imagewebp(), but the AVIF branch does not.

Fix

Call imagepalettetotruecolor() before imageavif(), in both save() and base64(), mirroring the WebP handling.

Tests

Added two tests using the existing palette-webp.png fixture: saving to AVIF and encoding to AVIF base64. Both are skipped when AVIF is not supported, like the existing AVIF tests.

Ran with PHP 8.4 and GD built with libavif: both GD tests fail without the fix and pass with it.

@freekmurze
freekmurze merged commit 5450a2f into spatie:main Sep 30, 2026
5 checks passed
@freekmurze

Copy link
Copy Markdown
Member

Thanks @AlexisSerneels! Great catch on the PNG-8 to AVIF failure with GD, and nice that the fix came with regression tests for both save() and base64(). I tightened the base64 test to also assert the output is AVIF. This is released in 3.9.7.

@AlexisSerneels
AlexisSerneels deleted the fix/gd-avif-palette-images branch September 30, 2026 07:18
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