0

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.

Charles
  • 50,943
  • 13
  • 104
  • 142
chubbyk
  • 6,212
  • 13
  • 53
  • 67

4 Answers4

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
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
  • or you can check the mime type which is "image/x-icon" – dip1232001 Aug 01 '10 at 14:18
  • 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