Intervention/image · error · EncoderException
Failed to encode ico format
Error message
Failed to encode ico format
What it means
The Imagick driver's IcoEncoder clones the native Imagick object, sets format 'ICO' with COMPRESSION_NO (src/Drivers/Imagick/Encoders/IcoEncoder.php:33-37) and serializes with getImagesBlob(). Any ImagickException is rethrown as EncoderException ('Failed to encode ico format') with the original as previous. Besides a missing ICON coder, ICO is spec-limited to 256x256 entries, so encoding oversized images is a classic trigger. Inspect $e->getPrevious()->getMessage() to distinguish the two.
Source
Thrown at src/Drivers/Imagick/Encoders/IcoEncoder.php:44
*/
public function encode(ImageInterface $image): EncodedImageInterface
{
$format = 'ICO';
$compression = Imagick::COMPRESSION_NO;
try {
$imagick = clone $image->core()->native();
$imagick->setFormat($format);
$imagick->setImageFormat($format);
$imagick->setCompression($compression);
$imagick->setImageCompression($compression);
$result = new EncodedImage($imagick->getImagesBlob(), 'image/x-icon');
$imagick->clear();
return $result;
} catch (ImagickException $e) {
throw new EncoderException('Failed to encode ico format', previous: $e);
}
}
}
View on GitHub (pinned to 5598b9e397)
Solutions
- Read $e->getPrevious()->getMessage() for the exact ImageMagick error.
- Ensure the source fits the ICO spec: resize to 256x256 or smaller (favicon sizes are typically 16/32/48/64) before calling toIco().
- Confirm the coder exists via Imagick::queryFormats('ICO'); if empty, install a full ImageMagick build and rebuild the imagick extension.
- Prefer dedicated favicon tooling for multi-size .ico generation and use toPng() for single-size icons.
Example fix
// before
$ico = $manager->read('logo-1024.png')->toIco(); // fails: ICO entries max out at 256x256
// after
$ico = $manager->read('logo-1024.png')
->resize(256, 256)
->toIco(); Defensive patterns
Strategy: validation
Validate before calling
$icoSupported = in_array('ICO', Imagick::queryFormats('ICO'), true);
$icoSized = $image->width() <= 256 && $image->height() <= 256;
if (!$icoSupported || !$icoSized) {
throw new RuntimeException('ICO output requires the ICON coder and dimensions <= 256x256');
}
$encoded = $image->toIco(); Try / catch
try {
$encoded = $image->toIco();
} catch (EncoderException $e) {
$reason = $e->getPrevious()?->getMessage() ?? $e->getMessage();
$encoded = $image->toPng(); // browsers accept PNG favicons, then log $reason
} Prevention
- Resize to 256x256 or smaller before toIco() - the ICO spec caps entries at 256.
- Validate upload dimensions at the API boundary for favicon endpoints.
- For multi-size favicons, generate 16/32/48 PNGs with dedicated tooling instead of relying on ICO writing.
When it happens
Trigger: Calling toIco() on the Imagick driver when the ICON coder is absent (custom/minimal ImageMagick builds), or when serializing images whose width or height exceeds the 256-pixel ICO limit, causing getImagesBlob() to fail. Verify with php -r "var_dump((new Imagick())->queryFormats('ICO'));".
Common situations: Generating favicons from full-size source images without downscaling; automated asset pipelines that feed arbitrary uploads to toIco(); stripped Alpine/static ImageMagick builds without the ICON coder.
Related errors
- Failed to encode avif format
- Failed to encode bmp format
- Failed to encode gif format
- Failed to encode heic format
- Failed to encode jp2 format
AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23).
Data as JSON: /api/errors/d2096ddf07eff096.
Report an issue: GitHub.