Intervention/image · error · EncoderException

Failed to encode gif format

Error message

Failed to encode gif format

What it means

The Imagick driver's GifEncoder clones the native Imagick object, sets format 'GIF' with COMPRESSION_LZW, optionally sets INTERLACE_LINE when interlaced (src/Drivers/Imagick/Encoders/GifEncoder.php:34-42), then serializes all frames with getImagesBlob(). Any ImagickException or ImageException is rethrown as EncoderException ('Failed to encode gif format') with the original as previous. GIF support is bundled in essentially every ImageMagick build, so failures usually come from resource exhaustion on animated/multi-frame images or a coder list stripped in a custom build.

Source

Thrown at src/Drivers/Imagick/Encoders/GifEncoder.php:49

        $compression = Imagick::COMPRESSION_LZW;

        try {
            $imagick = clone $image->core()->native();
            $imagick->setFormat($format);
            $imagick->setImageFormat($format);
            $imagick->setCompression($compression);
            $imagick->setImageCompression($compression);

            if ($this->interlaced) {
                $imagick->setInterlaceScheme(Imagick::INTERLACE_LINE);
            }

            $result = new EncodedImage($imagick->getImagesBlob(), 'image/gif');
            $imagick->clear();

            return $result;
        } catch (ImagickException | ImageException $e) {
            throw new EncoderException('Failed to encode gif format', previous: $e);
        }
    }
}

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Read $e->getPrevious()->getMessage() for the underlying ImageMagick error.
  2. Verify coder availability with php -r "var_dump((new Imagick())->queryFormats('GIF'));".
  3. For animated images, raise PHP memory_limit and the ImageMagick policy.xml memory/area limits, or reduce frame count/dimensions before encoding.
  4. If the coder is genuinely missing, install the full distro imagemagick package and rebuild the imagick extension.

Example fix

// before
$encoded = $image->toGif(interlaced: true);

// after - catch and degrade instead of shipping a 500
try {
    $encoded = $image->toGif(interlaced: true);
} catch (EncoderException $e) {
    report($e->getPrevious()?->getMessage() ?? $e->getMessage());
    $encoded = $image->toGif(); // retry without interlace
Defensive patterns

Strategy: try-catch

Validate before calling

if (!in_array('GIF', Imagick::queryFormats('GIF'), true)) {
    throw new RuntimeException('GIF encoding unavailable on this ImageMagick build');
}

Try / catch

try {
    $encoded = $image->toGif(interlaced: $interlace);
} catch (EncoderException $e) {
    $reason = $e->getPrevious()?->getMessage() ?? $e->getMessage();
    $encoded = $image->toGif(); // retry without interlace, then log $reason
}

Prevention

When it happens

Trigger: Calling toGif() (or with interlaced: true, which triggers setInterlaceScheme(INTERLACE_LINE) at GifEncoder.php:41) on the Imagick driver when the LZW/GIF coder is missing in a minimal ImageMagick build, or when serializing a large animated multi-frame image exhausts memory or exceeds ImageMagick policy limits during getImagesBlob().

Common situations: Encoding long GIF animations frame-by-frame in a queue worker under a low memory_limit; Alpine/static ImageMagick builds with a reduced coder list; container environments where policy.xml caps resource usage; rarely, builds compiled without LZW.

Related errors


AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23). Data as JSON: /api/errors/597253ebf8cb3543. Report an issue: GitHub.