PHP ICO - The PHP ICO Generator
PHP ICO provides an easy-to-use PHP class that can be used to generate valid
ICO files. Note that
these are not simply BMP, GIF, or PNG formatted images with an extension of
ico, the images generated by this class are fully valid ICO-formatted image
files., (*1)
The class uses the GD library for reading an image from the source file and
uses pure PHP for generating the ICO file format. In theory, any image format
that GD can read can be used as a source image to generate the ICO file. I
have tested this library with JPEG, GIF, and PNG images with great results. If
an animated GIF is supplied, the first frame will be used to generate the ICO
image., (*2)
The primary goal of creating this class was to have a simple-to-use and
reliable method of generating ICO files for use as
favicons in websites. This goal drove
much of the development and is the reason for some of the limitations in the
resulting functionality., (*3)
ICO files support two different ways of encoding the actual image data: the
Windows BMP format and the
PNG format. Since
support for the PNG format was introduced in Windows Vista and both older and
newer versions of Windows support the BMP enocoding, the BMP encoding method
was used., (*4)
Images are encoded using 32 bits per pixel. This allows for alpha channel
information in the resulting images. Support for 32 bit images was added in
Windows XP. This means that older versions of Windows are not likely to display
the ICO images properly, if at all. The generated images have not been tested
on versions of Windows predating XP., (*5)
Usage
The following is a very basic example of using the PHP ICO library:, (*6)
require __DIR__ . '/vendor/autoload.php';
$source = __DIR__ . '/example.gif';
$destination = __DIR__ . '/example.ico';
$converter = new Ossobuffo\PhpIco\IcoConverter($source);
$converter->saveIco($destination);
It takes a source file named example.gif
and produce an output ICO file named
example.ico
. example.ico
will contain a single image that is the same size
as the source image., (*7)
The ICO file format is capable of holding multiple images, each of a different
size. The PHP ICO library opens up this feature of the ICO format as shown in
the following example:, (*8)
require __DIR__ . '/vendor/autoload.php';
$source = __DIR__ . '/example.gif';
$destination = __DIR__ . '/example.ico';
$dimensions = [[16, 16], [24, 24], [32, 32], [64, 64]];
$converter = new Ossobuffo\PhpIco\IcoConverter($source, $dimensions);
$converter->saveIco($destination);
As with the previous example, this example produces example.ico
from the
example.gif
source file. In this example, sizes were passed to the
constructor that result in the example.ico
file containing four images:
one that is 16x16, another that is 24x24, yet another that is 32x32, and
finally one that is 64x64. Since the same source image is used for each of
the contained images, each contained image is simply the source image scaled
to the new dimensions., (*9)
Using different source images for the different sizes contained inside an ICO
file can create much better results. For instance, you can use a high-quality
image for higher-resolution images and use smaller images that are tailored
for their specific dimensions for the smaller sizes. The following example
shows how the PHP ICO library can be used to create such ICO files:, (*10)
require __DIR__ . '/vendor/autoload.php';
$destination = __DIR__ . '/example.ico';
$converter = new Ossobuffo\PhpIco\IcoConverter();
$converter->addImage(__DIR__ . '/example-small.gif', [[16, 16], [24, 24], [32, 32]]);
$converter->addImage(__DIR__ . '/example-medium.gif', [[48, 48], [64, 64]]);
$converter->addImage(__DIR__ . '/example-large.gif', [[128, 128]]);
$converter->saveIco($destination);
This example creates a single ICO file named example.ico
just as the previous
examples. The difference is that this example used multiple source images to
generate the final ICO images. The final ICO image contains a total of six
images: 16x16, 24x24, and 32x32 images from example-small.gif
; 48x48 and
96x96 images from example-medium.gif
; and a 128x128 image from
example-large.gif
., (*11)
By using this feature of supplying multiple source images with specific sizes
for each, you can generate ICO files that have very high-quality images at each
supplied resolution., (*12)
I've found that the 16x16, 24x24, 32x32, and 48x48 image sizes cover all the
sizes that browsers and Windows will try to use a favicon image for. The
different sizes come from using the favicon for various browser icons,
bookmarks, and various other places., (*13)
Thanks
I'd like to thank iThemes for making this project possible.
This code was originally developed to add easy-to-use favicon support to the
Builder theme. I asked
Cory, owner of iThemes and my boss, if I could share
the final code. He gave me the green light. Win for everyone. :), (*14)
Thanks iThemes. Thanks Cory., (*15)