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.

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

5 Responses

1

gadgeturi

May 28th, 2011 at 6:15 am

Thank You! This is reaally useful!!

2

Mobitopi

November 30th, 2011 at 12:17 pm

Thanks for the code. But i didnt got solved as my code generates url s with http:// prefix so i cant use them with it. Any code to remove http:// ?

sameer

November 30th, 2011 at 8:26 pm

Try this simple code-

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

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

4

Juan

February 2nd, 2013 at 3:48 am

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

5

davey

April 25th, 2013 at 5:55 am

@ 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!

Your thoughts

Sign up for fresh content in your email