I have WordPress set up to load, parse and cache the favicons for sites added as bookmarks. The PHP class I was using struggled with certain ICO files, such as alistapart.com and sitepoint.com.

In the below example, we load a favicon from the filesystem into memory. For my use-case, the favicon has been loaded directly from the external site. Wanting to avoid unnecessary temporary files, I found that you can pipe data to and from the convert program from ImageMagick using streams in PHP. Assuming that you have determined that the favicon is an ico file;

convert ico:- -thumbnail 16x16 -alpha on -background none -flatten png:-


The command reads left to right, ico:- instructs convert to receive an ico formatted string from STDIN. png:- then instructs convert to output a png string to STDOUT. Below is a working example;

$file = dirname(__FILE__);
$icon = file_get_contents("{$file}/favicon.ico");

$descriptorspec = array(
    0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
    1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
    2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

$process = proc_open('convert ico:- -thumbnail 16x16 -alpha on -background none -flatten png:-', $descriptorspec, $pipes);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/error-output.txt

    fwrite($pipes[0], $icon);
    fclose($pipes[0]);

    $conv = stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    $err  = $pipes[2];
    fclose($pipes[2]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    if ($return_value == 0) {
        header('Content-Type: image/png');
        echo $conv;
    } else {
        echo $err;
        echo "command returned $return_value\n";
    }

}

You can view a demo using convert here

I can’t take credit for the stream code; it is for the most part just the example code from the PHP manual.

Michael is a Software Engineer working in the North West of England. Michael spends his days building hand-crafted PHP applications. Rumours of his super-hero status are currently unconfirmed. He savours his victories when solving difficult programming challenges; occasionally writing about them here, on his personal blog.