I would like to test if *.ico images are valid icons in PHP. I tried to use getimagesize function but it doesn't support ICO files.
Asked
Active
Viewed 2,820 times
4 Answers
5
http://en.wikipedia.org/wiki/ICO_(file_format)
icon must start with the '0x00 0x00 0x01 0x00' bytes, it shall be enough for simple test.

ProgramFOX
- 6,131
- 11
- 45
- 51

nothrow
- 15,882
- 9
- 57
- 104
-
if your filepath is $path, actually doing this check in PHP looks like this: `fread(fopen($path, 'r'), 4) === "\x00\x00\x01\x00"` – Augustine Calvino Oct 26 '21 at 17:26
-
please, never do `fread(fopen())`. use temporary variable, and `fclose()` the file! – nothrow Oct 27 '21 at 18:20
-
of course! I only did it that way bc it didn't seem like I could do a multiline comment. – Augustine Calvino Oct 27 '21 at 21:59
1
ico files are mainly image files, if you change a jpeg or gif/png into ico extension it works fine,so if you keep image type cheking it will do the job

dip1232001
- 240
- 3
- 10
-
-
this is not what I want, I need to check them because I downloaded some from net with CURL and they're sometimes 404 pages – chubbyk Aug 01 '10 at 14:18
-
i didn't got you ,i assume the you have downloaded icos from internet some of them are corrupted but still showing as ico files, in that case check the mime type for "image/x-icon" – dip1232001 Aug 01 '10 at 14:49
-
checking mimetype is absolutely unreliable i started with that but after a while i noticed hundreds of favicons served as text/html. useless for checking. – edelwater Mar 21 '11 at 02:03
0
https://github.com/lordelph/icofileloader is a composer-installable package for reading .ico files. To check and inspect an .ico file, you could adapt this sample:
$loader = new Elphin\IcoFileLoader\IcoFileService;
//parse ico file
try {
$icon = $loader->fromFile('/path/to/icon.ico');
//we can iterate over the images in the icon
foreach ($icon as $idx => $image) {
printf("image %d is %s\n", $idx, $image->getDescription());
}
}
catch (\Exception $e) {
echo "not a valid .ico file";
}

Paul Dixon
- 295,876
- 54
- 310
- 348
0
Check here: http://plugins.trac.wordpress.org/browser/wp-favicons/trunk/includes/class-favicon.php the getfiletype method

edelwater
- 2,650
- 8
- 39
- 67