Search code examples
phpimage-processingpngfavicon

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


Solution

  • 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