سؤال

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.

هل كانت مفيدة؟

المحلول

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

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";
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top