简单缓存网站小图标 favicon.ico

简爱代码>PHP2014-11-3 20:2597774
PHP 获取网站图片缓存到本地服务器
访问地址类似于  http://*****/**本文件.php?http://需要获取图标的网址    (无需 url 编码)

PHP 代码:
<?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);
}


本文出自简爱博客,转载时请注明出处及相应链接。

评论

  1. 小金2014-11-25 20:19回复

    学习下

  2. Vind2014-11-13 12:53回复

    来看一看

  3. wlp2014-11-11 01:47回复

    看看先

  4. mrxn2014-11-03 22:05回复

    没怎么看明白.....

发表评论

电子邮件地址不会被公开。必填项已用*标注