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 we can use the following in a browser.

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

  function getSiteFavicon($url)
    $ch = curl_init(''.$url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
    $data = curl_exec($ch);
    header("Content-type: image/png; charset=utf-8");
    echo $data;

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.

  function saveSiteFavicon($url)
    /* Drop the TLD from the url */
    $saveFileName = substr($url,0,-4);
    $fp = fopen ($saveFileName.'.png', 'w+');
    $ch = curl_init(''.$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);

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

function getSiteFavicon($url)
    $ch = curl_init(''.$url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    return $data;
/* Urls to grab the Favicon of */
$urls = array("", "", "",
              "", "", "");
/* 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');

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] for any new project requirements and price quotes.

6 Responses



May 28th, 2011 at 6:15 am

Thank You! This is reaally useful!!



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:// ?


November 30th, 2011 at 8:26 pm

Try this simple code-

$url = “”;

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



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



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!



January 25th, 2015 at 7:31 pm

I’d recommend using 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.


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

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

Your thoughts

Sign up for fresh content in your email