Grabbing website Favicons using curl and php

Google Shared Stuff allows one to easily get the Favicon of any website with a single line. For instance to get the Favicon of yahoo.com we can use the following in a browser.

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

We can do do the same using PHP and curl using the following code.

<?php
 
  function getSiteFavicon($url)
  {
    $ch = curl_init('http://www.google.com/s2/favicons?domain='.$url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
    $data = curl_exec($ch);
    curl_close($ch);
 
    header("Content-type: image/png; charset=utf-8");
    echo $data;
  }
 
  getSiteFavicon("yahoo.com");
 
?>

This will display the Favicon in the browser. An even more interesting thing we can do is to save the same to a PNG file. A little modification to the above code will yield the following.

<?php
 
  function saveSiteFavicon($url)
  {
    /* Drop the TLD from the url */
    $saveFileName = substr($url,0,-4);
    $fp = fopen ($saveFileName.'.png', 'w+');
    $ch = curl_init('http://www.google.com/s2/favicons?domain='.$url);
 
    curl_setopt($ch, CURLOPT_TIMEOUT, 6);
 
    /* Save the returned data to a file */
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
  }
 
 
  saveSiteFavicon("yahoo.com");
 
?>

Below is a more elaborate code to grab Favicons from a list of urls and combine them into a single image.

<?php
 
function getSiteFavicon($url)
{
    $ch = curl_init('http://www.google.com/s2/favicons?domain='.$url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    curl_close($ch);
 
    return $data;
}
 
 
/* Urls to grab the Favicon of */
$urls = array("yahoo.com", "microsoft.com", "wired.com",
              "cakephp.org", "wordpress.com", "msn.com");
 
/* Create a 180px by 40px blank image */
$dest = imagecreate(180, 40);
 
$x = 0;
$y = 0;
 
foreach($urls as $url) {
    /* Get the Favicon data for a url */
    $imageData = getSiteFavicon($url);
 
    /* Create a GD image resource ffrom the data */
    $src = imagecreatefromstring($imageData);
 
    /* Copy each Favicon image to the blank canvas */
    imagecopy($dest, $src, $x, $y, 0, 0, 16, 16);
 
    /* change the x,y offset for the next favicon */
    $x += 20;
 
    if($x >= 60) {
        $x = 0;
        $y += 20;
    }
}
 
/* Output the newly created image */
header('Content-Type: image/gif');
imagegif($dest);
 
imagedestroy($dest);
imagedestroy($src);

The above code will create the following image in your browser.

A little more code change and you can save the image to a file.



7 thoughts on “Grabbing website Favicons using curl and php

  1. Try this simple code-

    $url = “http://www.example.com”;

    if(substr($url,0,5) == “http:”)
    {
    $url = substr($url,7,strlen($url)-5);
    }
    echo $url;

  2. First of all, thanks for sharing this. This is exactly what I am looking for.

    I am integrating this on my website but it seems that images saved on local folder is not displaying on IE. Do you have any idea what causing this issue? I don’t have any problem with other browser and all images shows up perfectly it just with IE. I don’t know if the problem is with IE or how the images are saved on local folder because some are actually showing up like facebook.

    Any idea?

    Thanks in advance

  3. @ Juan. I’ve been playing around with the same problem. I had this code working and I think I had the same problem.

    I was under the impression though that the favicon the google service returns has been changed to png. However I do remember having the problem of favicons not showing in IE.

    The reason is that although Microsoft came up with the .ico filetype their browsers don’t support it as an image type, hence it won’t work in IE. I hate Microsoft. (I’ll throw in here that windows 8 is absolute rubbish AVOID!)

    Anyway, I tried to change the format of the .ico file in another project I was working on and gave up because I couldn’t find any useful info and php doesn’t have a function to handle .ico files.

    If you figure something out, post it for others plz!

  4. I’d recommend using grabicon.com instead of google’s S2 favicon service. Google’s service was never meant to be public, and could go away at any time. Grabicon was built to be a drop-in replacement – it’s also free, and it works the same way.

    It also lets you specify what size you want the icon to be, and generates unique default icons for sites that don’t have one. This ensures a uniform user experience for web/mobile apps, because icons are always the requested size, and never missing.

    Example:

    http://grabicon.com/icon?domain=wikipedia.org

    Icons are 32px by default, but you can specify a custom size:

    http://grabicon.com/icon?domain=wikipedia.org&size=16

    Unlike google, grabicon is fully supported and being actively improved. Icon retrieval is very fast.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>