{"record":{"id":"d2096ddf07eff096","repo":"Intervention/image","slug":"failed-to-encode-ico-format","errorCode":null,"errorMessage":"Failed to encode ico format","messagePattern":"Failed to encode ico format","errorType":"exception","errorClass":"EncoderException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Encoders/IcoEncoder.php","lineNumber":44,"sourceCode":"     */\n    public function encode(ImageInterface $image): EncodedImageInterface\n    {\n        $format = 'ICO';\n        $compression = Imagick::COMPRESSION_NO;\n\n        try {\n            $imagick = clone $image->core()->native();\n            $imagick->setFormat($format);\n            $imagick->setImageFormat($format);\n            $imagick->setCompression($compression);\n            $imagick->setImageCompression($compression);\n\n            $result = new EncodedImage($imagick->getImagesBlob(), 'image/x-icon');\n            $imagick->clear();\n\n            return $result;\n        } catch (ImagickException $e) {\n            throw new EncoderException('Failed to encode ico format', previous: $e);\n        }\n    }\n}\n","sourceCodeStart":26,"sourceCodeEnd":48,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Encoders/IcoEncoder.php#L26-L48","documentation":"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.","triggerScenarios":"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'));\".","commonSituations":"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.","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."],"exampleFix":"// before\n$ico = $manager->read('logo-1024.png')->toIco(); // fails: ICO entries max out at 256x256\n\n// after\n$ico = $manager->read('logo-1024.png')\n    ->resize(256, 256)\n    ->toIco();","handlingStrategy":"validation","validationCode":"$icoSupported = in_array('ICO', Imagick::queryFormats('ICO'), true);\n$icoSized = $image->width() <= 256 && $image->height() <= 256;\nif (!$icoSupported || !$icoSized) {\n    throw new RuntimeException('ICO output requires the ICON coder and dimensions <= 256x256');\n}\n$encoded = $image->toIco();","typeGuard":null,"tryCatchPattern":"try {\n    $encoded = $image->toIco();\n} catch (EncoderException $e) {\n    $reason = $e->getPrevious()?->getMessage() ?? $e->getMessage();\n    $encoded = $image->toPng(); // browsers accept PNG favicons, then log $reason\n}","preventionTips":["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."],"tags":["ico","favicon","imagick","encode","image-size"],"backgroundTag":"imagick-no-encode-delegate","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}