PHP 获取网站图片缓存到本地服务器
访问地址类似于 http://*****/**本文件.php?http://需要获取图标的网址 (无需 url 编码)
PHP 代码:
/**
* 网站 favicon.ico 图标缓存
*
* 使用时 上传服务器访问 本文件.php?http://gouji.org
* 返回 http://gouji.org 的 favicon.ico 并缓存在本地
* 如果 favicon.ico 不存在 就显示当前文件夹下的 ico.png 文件
* 如果不存在本图片 将返回 透明的空白图片一张
*
* Author: 简爱
* Author Email: sc.419@qq.com
* Author URL: http://gouji.org
* Date: 20141103
**/
define('PATH', dirname(__FILE__) ."/");
define('CACHE', PATH ."/cache/"); // 缓存目录
define('ICO_TYPE', ""); // 缓存文件 后缀
define('TIME_OUT', 3600 * 24 * 30); // 缓存有效时间
if(!file_exists(CACHE)) mkdir(CACHE);
if(isset($_SERVER['QUERY_STRING']) &&
@ preg_match("/\/\/([^\/]*)/i", $_SERVER['QUERY_STRING'], $u))
$host = $u[1];
else
putIco();
define('ICO', CACHE . $host . ICO_TYPE);
if(file_exists(ICO) && time() - filectime(ICO) < TIME_OUT)
putIco(file_get_contents(ICO));
else
getIco($host);
function getIco($u){
$ico = file_get_contents("http://$u/favicon.ico");
file_put_contents(ICO, $ico);
putIco($ico);
}
function putIco($i=false){
$d = PATH . 'ico.png';
$i = !$i || empty($i) ? ( file_exists($d) ? file_get_contents($d) :
base64_decode('R0lGODlhAQABAJEAAAAAAP///wAAACH5BAEAAAIALAAAAAABAAEAAAICVAEAOw==')) : $i;
header("Content-Type: image/png");
die($i);
}