arthurhoaro/favicon

用于从给定URL发现favicon的PHP库

v2.0.0 2023-10-22 00:43 UTC

README

此库基于Chris Shiflett的工作

以下是您可以在本版本中看到的变化

  • 覆盖更多用例以查找favicon
  • Composer支持
  • 各种技术更改和改进
  • 单元测试

需求

Composer

通过在您的composer.json中添加以下行使用Composer

"require": {
    "arthurhoaro/favicon": "~2.0"
}

基本用法

require_once('vendor/autoload.php');

$favicon = new \Favicon\Favicon();

echo $favicon->get('http://hoa.ro');
// Displays: http://hoa.ro/themes/hoaro/img/favicon.png
var_dump($favicon->get('http://nofavicon.tld', FaviconDLType::HOTLINK_URL));
// Returns false

您可以通过下载favicon来避免热链接

$favicon = new \Favicon\Favicon();

// return the generated filename inside the cache folder
$favicon->get('http://hoa.ro', FaviconDLType::DL_FILE_PATH);
// return false
$favicon->get('http://nofavicon.tld');

或直接获取原始图像作为二进制字符串

$favicon = new \Favicon\Favicon();

// return the binary string of the downloaded favicon
$favicon->get('http://hoa.ro', FaviconDLType::RAW_IMAGE);
// return false
$favicon->get('http://nofavicon.tld');

注意:DL_FILE_PATHRAW_IMAGE需要启用缓存。

配置

您可以设置缓存设置

$favicon = new Favicon();
$settings = array(
    // Cache directory
    'dir' => '/tmp/',
    // Cache timeout in seconds
    'timeout' => 86400,
    // Default image when no favicon is found
    'defaultico' => 'img/fav.ico'
);
$favicon->cache($settings);