Found this PHP script on GitHub https://github.com/ao/favicons and seems work well. Easy to incorporate into other scripts that might need a favicon.
NOTE: Seems respected user here advises not to use this code... See comment below...
The issue I am having is cachine is not working correctly. If you look at top of index.php, you will see:
<?php
error_reporting(0);
// Change the location where images are stored/retrieved
//$_CACHE_PATH = "../favicon_cache"; // one directory up
$_CACHE_PATH = "cache"; // current directory
if (!isset($_GET['url'])) die();
if (substr( $_GET['url'], 0, 4 ) !== "http") {
$_GET['url'] = "http://".$_GET['url'];
}
$parse = parse_url($_GET['url']);
$domain = $parse['host'];
if (isset($_GET['refresh'])) {
@unlink('../'+$_CACHE_PATH+'/'.$domain);
}
if (isset($_GET['debug'])) {
require 'FaviconDownloader.php';
$_favicon = new FaviconDownloader($_GET['url']);
$_favicon->debug();
die();
}
if (file_exists($_CACHE_PATH+'/'.$domain)) {
//show cached copy first!
header('Content-Type: image/png');
echo file_get_contents($_CACHE_PATH+'/'.$domain);
die();
}
require 'FaviconDownloader.php';
$favicon = new FaviconDownloader($_GET['url']);
if($favicon->icoExists){
if (!file_exists($_CACHE_PATH+'/'.$domain)) {
file_put_contents($_CACHE_PATH+'/'.$domain, $favicon->icoData);
}
header('Content-Type: image/png');
echo file_get_contents($_CACHE_PATH+'/'.$domain);
} else {
header('Content-Type: image/png');
echo file_get_contents('default.png');
}
?>
Entire index.php: https://github.com/ao/favicons/blob/master/index.php
No matter what I change the cache folder to, all favicons are still written to the root folder. Have tried a few tweaks with the code, but nothing seems to work. Removing the error_reporting(0); at top of index.php doesn't show any extra errors (only shows: Resource interpreted as Document but transferred with MIME type image/png in dev console).
My cache folder is writable and properly assigned to owner,so pretty sure non-issue there.
Started to ask question / open issue at GitHub but see another person having same issue with no answer from author. In hopes someone here with better PHP skills than myself can point me in right direction.