开发者

How to detect if *.ico file is a valid icon in PHP

I wou开发者_开发问答ld like to test if *.ico images are valid icons in PHP. I tried to use getimagesize function but it doesn't support ICO files.


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.


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


Check here: http://plugins.trac.wordpress.org/browser/wp-favicons/trunk/includes/class-favicon.php the getfiletype method


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";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜