All formats

Imagick

LanguagePHP
LicensePHP License 3.01 (ImageMagick license for the native library)
Repositorygithub.com/Imagick/imagick
DocumentationImagick in the PHP manual
Depends onImageMagick (MagickWand)
Read / writeyes / yes, every entry, BMP and PNG entries; CUR read

Imagick is the PHP extension for ImageMagick. An Imagick object holds a list of images, and for an ICO file each entry is one image in the list. Writing the list with writeImages produces one icon entry per image. Use it to generate favicon.ico from an uploaded logo or to extract icon sizes on a server.

GD, PHP’s bundled image extension, doesn’t support ICO, so Imagick is the usual choice. Entries are stored as BMP unless they are larger than 256x256 or you set icon:png-compression-size.

Installation#

Shell
# Debian / Ubuntu
sudo apt install php-imagick
# or from PECL
pecl install imagick

Basic usage: list the sizes in an ICO file#

PHP
<?php
$icon = new Imagick('app.ico');
echo $icon->getNumberImages(), " entries\n";
foreach ($icon as $i => $entry) {
    printf("%d: %d x %d\n", $i, $entry->getImageWidth(), $entry->getImageHeight());
}

How to convert ICO to PNG#

Export the largest entry:

PHP
<?php
$icon = new Imagick('app.ico');
$best = 0;
$bestArea = 0;
foreach ($icon as $i => $entry) {
    $area = $entry->getImageWidth() * $entry->getImageHeight();
    if ($area > $bestArea) {
        $best = $i;
        $bestArea = $area;
    }
}
$icon->setIteratorIndex($best);
$png = $icon->getImage();
$png->setImageFormat('png');
$png->writeImage('app.png');

How to create a multi-size favicon.ico#

icon:auto-resize resizes one square image to every listed size when it’s written as ICO:

PHP
<?php
$logo = new Imagick('logo.png');
$logo->resizeImage(256, 256, Imagick::FILTER_LANCZOS, 1);
$logo->setOption('icon:auto-resize', '256,64,48,32,16');
$logo->setOption('icon:png-compression-size', '256');  // 256x256 entry as PNG
$logo->setImageFormat('ico');
$logo->writeImage('favicon.ico');

How to build an ICO from hand-drawn sizes#

PHP
<?php
$icon = new Imagick();
foreach (['icon16.png', 'icon32.png', 'icon48.png', 'icon256.png'] as $file) {
    $icon->readImage($file);
}
$icon->setFormat('ico');
$icon->writeImages('app.ico', true);  // true: all images into one file

Tips#