7

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

Mala
  • 14,178
  • 25
  • 88
  • 119

6 Answers6

28

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

Mala
  • 14,178
  • 25
  • 88
  • 119
  • that's just like writing yourself: www.domain.com/favicon.ico. I've checked and it's the same.. – vsync May 25 '10 at 09:10
  • 5
    Not really - first of all, it returns a png, not an ico. Secondly, favicons can be located in a myriad of paths - this standardizes it, and leaves the work to google. Finally, if a favicon is not found, it returns a placeholder image, instead of landing you with a broken image. – Mala May 27 '10 at 23:19
  • 3
    But as with all central solutions: Be aware that Google can connect you and that bunch of URLs now. If that does not bother you, this looks good. – Martin Ueding Oct 11 '11 at 17:41
  • Is there a way to ask for a higher resolution favicon? – tmyie Jun 23 '16 at 21:53
4

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

Iain Fraser
  • 6,578
  • 8
  • 43
  • 68
  • This actually works! But so far it doesn't seem that smart about parsing favicons in non-standard locations. I'll dig around and will post the source if I come up with a solution. -Iain – Iain Fraser Aug 16 '10 at 06:48
  • This class does not seem to work well in new versions of php. – Maelish Aug 04 '15 at 13:55
2

As Iain Fraser said, the Favicon class from controlstyle.com doesn't works with all test case.

Basically, if provided, the <link> shortcut icon tag can contain different URL types :

  • full absolute URL : http://www.domain.com/images/fav.ico
  • absolute URL with relative scheme : //www.domain.com/images/fav.ico
  • absolute path : /images/fav.ico
  • relative URL : ../images/fav.ico

Furthermore, the web page can contain a <base href="..." /> attribute that changes how to deal with relative URL and absoute path...

So I've written a PHP class that works with all these cases. First, it tries to get the favicon URL from the <link> attribute, and fallback to the default favicon URI (//www.domain.com/favicon.ico) in case of failure.

You can grab it on my website here : http://www.finalclap.com/faq/477-php-favicon-find-download or install it using composer : composer require vincepare/favicon-downloader.

How to use :

<?php
require 'FaviconDownloader.class.php';
$favicon = new FaviconDownloader('http://stackoverflow.com/questions/19503326/bug-with-chrome-tabs-create-in-a-loop');

if($favicon->icoExists){
    echo "Favicon found : ".$favicon->icoUrl."\n";

    // Saving favicon to file
    $filename = 'favicon-'.time().'.'.$favicon->icoType;
    file_put_contents($filename, $favicon->icoData);
    echo "Saved to ".$filename."\n\n";
} else {
    echo "No favicon for ".$favicon->url."\n\n";
}
?>
Vince
  • 3,274
  • 2
  • 26
  • 28
  • Nice class. Just a note: your finalclap.com script has a lot of spacing errors, which are tricky to find, and break the code. Once fixed though, it works great. Future plans to convert .ico to .png? – Sean O Jul 10 '14 at 15:40
  • I know, these spacing issue is caused by http://alexgorbatchev.com/SyntaxHighlighter/, used to display code on my website. You should rather download the class from here : http://www.finalclap.com/download/item/76-favicondownloader-php-class instead of copy & paste the source code from the tutorial page. I don't plan to add image convert features because I think there is better tools to achieve this, like imagemagick. – Vince Jul 11 '14 at 07:51
0

If the favicon isn't located at /favicon.ico I guess you have to parse the HTML.

For the filetype detection, you can use this extension, which detects the filetype by using magic bytes.

You can convert to PNG by using the GD library, an example can be found here.

Johannes Bittner
  • 3,503
  • 2
  • 23
  • 21
0

If your PHP install includes the GD library, you can convert an image to a PNG using the imagepng function.

Josh
  • 10,961
  • 11
  • 65
  • 108
Andy Balaam
  • 6,423
  • 6
  • 34
  • 37
-1

Coverting to PNG is not that hard.

I don't get the question entirely, is this fav icon on your site or on other sites? If on other sites, you will have to parse fetched HTML and then somehow load favicon.

usoban
  • 5,428
  • 28
  • 42
  • My site needs to accept any URL (say, http://www.amazon.com) and then get the site's favicon - at the moment i'm using file_get_contents($site/favicon.ico) but the issue is that some sites like to have other format icons, or store them in other places. I was wondering if there's a generic way of getting to the favicon – Mala Aug 14 '09 at 09:14