Monday, February 27, 2012

Create favicon (*.ico) with imagick in php

Here is an example that will take any image format supported by Image Magick (is there one that is not?) and create a 16 pixel favicon image from it. The cropThumbnailImage() method does the work of resizing and cropping in a single swoop while the setFormat() method converts the file to .ico format.

Code is here:
[php]
<?php
try
{
/*** set the image name ***/
$img = 'test.jpg';

/*** read the image into imagick ***/
$Imagick = new Imagick($img);

/*** crop and thumbnail the image ***/
$Imagick->cropThumbnailImage(16,16);

/*** set the format to ico ***/
$Imagick->setFormat('ico');

/*** write the favicon.ico file ***/
$Imagick->writeImage("favicon.ico");

echo 'done';
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>

[/php]

1 comment:

  1. I just want to mention I am just very new to weblog and truly savored you're website. Almost certainly I’m planning to bookmark your website . You absolutely come with fabulous articles and reviews. Many thanks for revealing your web-site.

    ReplyDelete

Note: Only a member of this blog may post a comment.