Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
F

favicon-fetcher

> 后端框架
Open source

A Laravel package for fetching favicons from websites.

191 stars0 likes0 views
WebsiteGitHub

About

A Laravel package for fetching favicons from websites.

Table of Contents

  • Overview
  • Installation
    • Requirements
    • Install the Package
    • Publish the Config
  • Usage
    • Fetching Favicons
      • Using the fetch Method
      • Using the fetchOr Method
      • Using the fetchAll Method
      • Using the fetchAllOr Method
    • Exceptions
    • Drivers
    • Available Drivers
      • How to Choose a Driver
    • Choosing a Driver
      • Fallback Drivers
      • Adding Your Own Driver
    • HTTP Timeouts
    • TLS Verification
    • HTTP User Agent
    • Storing Favicons
      • Using store
      • Using storeAs
    • Caching Favicons
    • Favicon Types
    • Favicon Sizes
  • Testing
  • Security
  • Contribution
  • Changelog
  • Upgrading
  • Credits
  • License

Overview

A Laravel package that can be used for fetching favicons from websites.

Installation

Requirements

The package has been developed and tested to work with the following minimum requirements:

  • PHP 8.0
  • Laravel 8.0

Install the Package

You can install the package via Composer:

bash
composer require ashallendesign/favicon-fetcher

Publish the Config

You can then publish the package's config file by using the following command:

bash
php artisan vendor:publish --provider="AshAllenDesign\FaviconFetcher\FaviconFetcherProvider"

Usage

Fetching Favicons

Now that you have the package installed, you can start fetching the favicons from different websites.

Using the fetch Method

To fetch a favicon from a website, you can use the fetch method which will return an instance of AshAllenDesign\FaviconFetcher\Favicon:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::fetch('https://ashallendesign.co.uk');

Using the fetchOr Method

If you'd like to provide a default value to be used if a favicon cannot be found, you can use the fetchOr method.

For example, if you wanted to use a default icon (https://example.com/favicon.ico) if a favicon could not be found, your code could look something like this:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::fetchOr('https://ashallendesign.co.uk', 'https://example.com/favicon.ico');

This method also accepts a Closure as the second argument if you'd prefer to run some custom logic. The url field passed as the first argument to the fetchOr method is available to use in the closure. For example, to use a closure, your code could look something like this:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::fetchOr('https://ashallendesign.co.uk', function ($url) {
    // Run extra logic here...

    return 'https://example.com/favicon.ico';
});

Using the fetchAll Method

There may be times when you want to retrieve the different sized favicons for a given website. To get the different sized favicons, you can use the fetchAll method which will return an instance of AshAllenDesign\FaviconFetcher\Collections\FaviconCollection. This collection contains instances of AshAllenDesign\FaviconFetcher\Favicon. For example, to get all the favicons for a site, you can use the fetchAll method like so:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicons = Favicon::fetchAll('https://ashallendesign.co.uk');

The FaviconCollection class extends the Illuminate\Support\Collection class, so you can use all the methods available on the Collection class.

It also includes a largest method that you can use to get the favicon with the largest dimensions. It's worth noting that if the size of the favicon is unknown, it will be treated as if it has a size of 0x0px when determining which is the largest. For example, you can use the largest method like this:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$largestFavicon = Favicon::fetchAll('https://ashallendesign.co.uk')->largest();

The FaviconCollection also provides a largestByFileSize method that you can use to get the favicon with the largest file size. You may want to do this if the package cannot detect the sizes of the icons for a given website, and so it can't detect the largest icon. This method works based on the assumption that the larger the file size, the larger the image dimensions. For example, you can use the largestByFileSize method like this:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$largestFavicon = Favicon::fetchAll('https://ashallendesign.co.uk')->largestByFileSize();

Note: Only the http driver supports retrieving all the favicons for a given website. For this reason, the fetchAll method does not support fallbacks. Support may be added for other drivers and fallbacks in the future.

Using the fetchAllOr Method

If you'd like to provide a default value to be used if all the favicons for a site cannot be found, you can use the fetchAllOr method.

For example, if you wanted to use a default icon (https://example.com/favicon.ico) if the favicons could not be found, your code could look something like this:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::fetchAllOr('https://ashallendesign.co.uk', 'https://example.com/favicon.ico');

This method also accepts a Closure as the second argument if you'd prefer to run some custom logic. The url field passed as the first argument to the fetchAllOr method is available to use in the closure. For example, to use a closure, your code could look something like this:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::fetchAllOr('https://ashallendesign.co.uk', function ($url) {
    // Run extra logic here...

    return 'https://example.com/favicon.ico';
});

Exceptions

By default, if a favicon can't be found for a URL, the fetch method will return null. However, if you'd prefer an exception to be thrown, you can use the throw method available on the Favicon facade. This means that if a favicon can't be found, an AshAllenDesign\FaviconFetcher\Exceptions\FaviconNotFoundException will be thrown.

To enable exceptions to be thrown, your code could look something like this:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::throw()->fetch('https://ashallendesign.co.uk');

If you attempt to fetch a favicon and the request times out or no website is found at the URL, an AshAllenDesign\FaviconFetcher\Exceptions\ConnectionException will be thrown. This will be thrown even if the throw method has not been used.

Drivers

Favicon Fetcher provides the functionality to use different drivers for retrieving favicons from websites.

Available Drivers

By default, Favicon Fetcher ships with 5 drivers out-the-box: http, google-shared-stuff, favicon-kit, unavatar, favicon-grabber, duck-duck-go.

The http driver fetches favicons by attempting to parse "icon" and "shortcut icon" link elements from the returned HTML of a webpage. If it can't find one, it will attempt to guess the URL of the favicon based on common defaults.

The google-shared-stuff driver fetches favicons using the Google Shared Stuff API.

The favicon-kit driver fetches favicons using the Favicon Kit API.

The unavatar driver fetches favicons using the Unavatar API.

The favicon-grabber driver fetches favicons using the Favicon Grabber API.

The duck-duck-go driver fetches favicons using the DuckDuckGo Icons API.

How to Choose a Driver

It's important to remember that the google-shared-stuff, favicon-kit, unavatar, favicon-grabber, and duck-duck-go drivers interact with third-party APIs to retrieve the favicons. So, this means that some data will be shared to external services.

However, the http driver does not use any external services and directly queries the website that you are trying to fetch the favicon for. Due to the fact that this package is new, it is likely that the http driver may not be 100% accurate when trying to fetch favicons from websites. So, theoretically, the http driver should provide you with better privacy, but may not be as accurate as the other drivers.

Choosing a Driver

You can select which driver to use by default by changing the default field in the favicon-fetcher config file after you've published it. The package originally ships with the http driver enabled as the default driver.

For example, if you wanted to change your default driver to favicon-kit, you could update your favicon-fetcher config like so:

php
return [

    // ...
        
    'default' => 'favicon-kit',
            
    // ...

]

If you'd like to set the driver on-the-fly, you can do so by using the driver method on the Favicon facade. For example, if you wanted to use the google-shared-stuff driver, you could do so like this:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::driver('google-shared-stuff')->fetch('https://ashallendesign.co.uk');

Fallback Drivers

There may be times when a particular driver cannot find a favicon for a website. If this happens, you can fall back and attempt to find it again using a different driver.

For example, if we wanted to try and fetch the favicon using the http driver and then fall back to the google-shared-stuff driver if we can't find it, your code could look something like this:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::withFallback('google-shared-stuff')->fetch('https://ashallendesign.co.uk');

Adding Your Own Driver

There might be times when you want to provide your own custom logic for fetching favicons. To do this, you can build your driver and register it with the package for using.

First, you'll need to create your own class and make sure that it implements the AshAllenDesign\FaviconFetcher\Contracts\Fetcher interface. For example, your class could like this:

php
use AshAllenDesign\FaviconFetcher\Contracts\Fetcher;
use AshAllenDesign\FaviconFetcher\Favicon;

class MyCustomDriver implements Fetcher
{
    public function fetch(string $url): ?Favicon
    {
        // Add logic here that attempts to fetch a favicon...
    }

    public function fetchOr(string $url, mixed $default): mixed
    {
        // Add logic here that attempts to fetch a favicon or return a default...
    }
}

After you've created your new driver, you'll be able to register it with the package using the extend method available through the Favicon facade. You may want to do this in a service provider so that it is set up and available in the rest of your application.

You can register your custom driver like so:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

Favicon::extend('my-custom-driver', new MyCustomDriver());

Now that you've registered your custom driver, you'll be able to use it for fetching favicons like so:

php
use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::driver('my-custom-driver')->fetch('https://ashallendesign.co.uk');

HTTP Timeouts

Favicon Fetcher provides the ability for you to set the connection timeout and request timeout for

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPhacktoberfestlaravelphp

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架