Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - getting a site's favicon and converting it to png if necessary

For any given site "example.domain.tld" or merely "domain.tld" I need to do the following in PHP:

  • If the site has a favicon, get it wherever it is
    • If it is not already a PNG, convert it to PNG
    • Save it to /favicons/example.domain.tld.png
  • If the site has no favicon, do nothing.

Any ideas? I'm being stumped by the unreliable fileformat and location of the favicons, but if at all possible I want to avoid downloading the entire source of the page with file_get_contents in order to find it in the headers. Also converting to png seems nontrivial.

Thanks,

Mala

like image 535
Mala Avatar asked Aug 14 '09 08:08

Mala


People also ask

Can I use PNG instead of ICO for favicon?

A favicon can actually be either a PNG, GIF, or ICO file. However, ICO files are typically used more than others as the file size is smaller and it is supported in all major browsers. PNGs are used more commonly for IOS, Android, and Windows 10 devices.

Should favicons be PNG or JPG?

ico format anymore (even though browsers do support . ico still). Favicons' format is always . png .

Should favicon be SVG or PNG?

SVG would be nice, but is not supported by all browsers. So the easiest solution is still to just use PNG images. Google explicitly says they don't support 16x16 or 32x32 icons. For Google results, you need to include at least one icon that is a multiple of 48x48.


2 Answers

As is typical, I found a passable solution shortly after asking the question - let google do the work for you:

http://www.google.com/s2/favicons?domain=URL

returns a 16x16 png

like image 76
Mala Avatar answered Sep 29 '22 15:09

Mala


Found this: http://www.controlstyle.com/articles/programming/text/php-favicon/

I'm about to try it for my project and I'll report back and tell you if it works!

Cheers

Iain

like image 26
Iain Fraser Avatar answered Sep 29 '22 16:09

Iain Fraser