PHPRO.ORG

Create Favicon With Imagick

Create Favicon With Imagick

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.


<?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();
    }
?>