I'm working on trying to download and save a favicon(.ICO) from a website using php. I want to convert this ico file to a png.
If I understand this correctly, a single .ICO file can store different image resolutions, like 16x16 and 32x32 (google's favicon is an example of this). My question is, is there a way to choose which ico file to convert?
Currently I can download the file
$image = file_get_contents('http://google.com/favicon.ico');
$saved_file = fopen("favicon.ico", "w");
fwrite($saved_file, $image);
fclose($saved_file);
This code downloads and saves a file called favicon.ico
.
Then when I run this line of code
var_dump(getimagesize('favicon.ico'));
I get this output
array(6) {
[0]=>
int(16)
[1]=>
int(16)
[2]=>
int(17)
[3]=>
string(22) "width="16" height="16""
["bits"]=>
int(32)
["mime"]=>
string(24) "image/vnd.microsoft.icon"
}
So my question is, how do I programmatically access the other icon sizes? I can clearly see them using Preview on my mac, but not within php.
Any ideas?
After many times I found your answer ;)
You can use this class: http://www.phpclasses.org/package/2369-PHP-Extract-graphics-from-ico-files-into-PNG-images.html
The sample code:
<?php
include 'class.ico.php';
$ico = new Ico('http://google.com/favicon.ico');
$im = $ico->GetIcon(0); //For get first image
//$im = $ico->GetIcon(1); //For get second image
//$TotalIcons = $ico->TotalIcons(); //for get total numbers of images in icon
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
Output:
For first image 16x16 separate will be in output:
For second image 32x32 separate will be in output:
And also, this class have a great method for get all icon in single image. see:
<?php
include 'class.icothumb.php';
$ico = new IcoThumb('http://google.com/favicon.ico');
$ico->max_size = 128;
$ico->use_diferent_depths = false;
$ico->GetThumb(4);
?>
The output is perfect as single image: