一个 Laravel 包,用于从网站中获取 favicon。
A Laravel package that can be used for fetching favicons from websites.
The package has been developed and tested to work with the following minimum requirements:
You can install the package via Composer:
composer require ashallendesign/favicon-fetcherYou can then publish the package's config file by using the following command:
php artisan vendor:publish --provider="AshAllenDesign\FaviconFetcher\FaviconFetcherProvider"Now that you have the package installed, you can start fetching the favicons from different websites.
fetch MethodTo fetch a favicon from a website, you can use the fetch method which will return an instance of AshAllenDesign\FaviconFetcher\Favicon:
use AshAllenDesign\FaviconFetcher\Facades\Favicon;
$favicon = Favicon::fetch('https://ashallendesign.co.uk');fetchOr MethodIf 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:
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:
use AshAllenDesign\FaviconFetcher\Facades\Favicon;
$favicon = Favicon::fetchOr('https://ashallendesign.co.uk', function ($url) {
// Run extra logic here...
return 'https://example.com/favicon.ico';
});fetchAll MethodThere 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:
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:
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:
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.
fetchAllOr MethodIf 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:
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:
use AshAllenDesign\FaviconFetcher\Facades\Favicon;
$favicon = Favicon::fetchAllOr('https://ashallendesign.co.uk', function ($url) {
// Run extra logic here...
return 'https://example.com/favicon.ico';
});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:
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.
Favicon Fetcher provides the functionality to use different drivers for retrieving favicons from websites.
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.
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.
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:
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:
use AshAllenDesign\FaviconFetcher\Facades\Favicon;
$favicon = Favicon::driver('google-shared-stuff')->fetch('https://ashallendesign.co.uk');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:
use AshAllenDesign\FaviconFetcher\Facades\Favicon;
$favicon = Favicon::withFallback('google-shared-stuff')->fetch('https://ashallendesign.co.uk');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:
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:
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:
use AshAllenDesign\FaviconFetcher\Facades\Favicon;
$favicon = Favicon::driver('my-custom-driver')->fetch('https://ashallendesign.co.uk');Favicon Fetcher provides the ability for you to set the connection timeout and request timeout for
暂无开放 Issues,或尚未同步最近议题。