Favicon Fetcher
repository·master·Indexed 41 days ago
https://github.com/ash-jc-allen/favicon-fetcherA Laravel package for programmatically retrieving favicons from websites. It supports multiple fetching strategies via drivers including http, google-shared-stuff, favicon-kit, unavatar, favicon-grabber, and duck-duck-go. Features include support for fallback values, caching, filesystem storage, and the ability to retrieve all available favicon sizes for a site via FaviconCollection.
What's inside Favicon Fetcher
- Favicon Fetcher is a Laravel package designed to fetch favicons from websites. It provides various methods to retrieve favicon data and supports multiple drivers for different fetching strategies.
Configure and use different drivers
masterThe package supports multiple drivers for fetching favicons.
Available Drivers:
http: Parses HTML for icon links or guesses based on defaults. Does not use external APIs (better privacy, potentially lower accuracy).google-shared-stuff: Uses Google Shared Stuff API.favicon-kit: Uses Favicon Kit API.unavatar: Uses Unavatar API.favicon-grabber: Uses Favicon Grabber API.duck-duck-go: Uses DuckDuckGo Icons API.
Usage:
- Set Default: Change the
defaultkey inconfig/favicon-fetcher.php. - On-the-fly: Use
Favicon::driver('driver-name'). - Fallbacks: Use
Favicon::withFallback('driver-name')to try a second driver if the first fails.
use AshAllenDesign\FaviconFetcher\Facades\Favicon; // Use a specific driver $favicon = Favicon::driver('google-shared-stuff')->fetch('https://ashallendesign.co.uk'); // Use a fallback driver $favicon = Favicon::withFallback('google-shared-stuff')->fetch('https://ashallendesign.co.uk');Understand Caching Format Changes in v2.0.0+
masterStarting with v2.0.0, the cache format changed from storing a simple string (the favicon URL) to storing an array that includes the favicon's size and type.
Important Considerations:
- Legacy Data: The package provides minor backwards-compatible support for items cached before v2.0.0. However, for these legacy items, the
Faviconclass'stypeandsizeproperties will not be set. - Future Deprecation: Backwards compatibility for the old string-based cache format is expected to be removed in v3.0.0. Only array-based cache entries will be supported then.
- Legacy Data: The package provides minor backwards-compatible support for items cached before v2.0.0. However, for these legacy items, the
Create a custom driver
masterTo implement custom fetching logic, create a class that implements the
AshAllenDesign\FaviconFetcher\Contracts\Fetcherinterface and register it using theextendmethod.use AshAllenDesign\FaviconFetcher\Contracts\Fetcher; use AshAllenDesign\FaviconFetcher\Favicon; use AshAllenDesign\FaviconFetcher\Facades\Favicon; class MyCustomDriver implements Fetcher { public function fetch(string $url): ?Favicon { // Custom logic } public function fetchOr(string $url, mixed $default): mixed { // Custom logic } } // Register the driver Favicon::extend('my-custom-driver', new MyCustomDriver()); // Use the driver $favicon = Favicon::driver('my-custom-driver')->fetch('https://example.com');Install Favicon Fetcher via Composer
masterTo install the package, run the following Composer command in your Laravel project terminal:
composer require ashallendesign/favicon-fetcherRequirements for Favicon Fetcher
masterThe package requires the following minimum versions to function correctly:
- PHP: 8.0+
- Laravel: 8.0+
Publish the Favicon Fetcher Configuration
masterAfter installing the package, publish its configuration file to your application's
configdirectory using the Artisan command:php artisan vendor:publish --provider="AshAllenDesign\FaviconFetcher\FaviconFetcherProvider"Configure HTTP settings and TLS
masterGlobal HTTP settings can be configured in
config/favicon-fetcher.phpor via.env:connect_timeout: Time to wait for a connection (seconds).timeout: Time to wait for a response (seconds).verify_tls: Boolean to enable/disable TLS verification. Set tofalsefor self-signed certs.user_agent: Custom User-Agent string for all requests.
Environment Variables:
FAVICON_FETCHER_VERIFY_TLS(boolean)FAVICON_FETCHER_USER_AGENT(string)
Publish the FaviconFetcher configuration
masterTo customize the package behavior, you can publish the configuration file to your application's
configdirectory. This allows you to modify settings like drivers and timeouts without changing the package source.Run the following Artisan command to publish the config:
php artisan vendor:publish --tag=favicon-fetcher-configRegister the FaviconFetcher service provider
masterThe
FaviconFetcherProvideris a Laravel Service Provider that integrates the package into your application. It merges the default configuration and binds thefavicon-fetcherservice to the application container using theFetcherManagerclass.To use the package, ensure this provider is registered in your Laravel application's
config/app.php(for older Laravel versions) or is automatically discovered if using Laravel's package auto-discovery.// The package binds the manager to the 'favicon-fetcher' key in the container $fetcher = app('favicon-fetcher');Handle Connection Exceptions in v3.0.0
masterWhen upgrading to v3.0.0, note that the exception thrown during failed requests has changed. Instead of
Illuminate\Http\Client\ConnectionException, the package now throwsAshAllenDesign\FaviconFetcher\Exceptions\ConnectionException.This exception is thrown when:
- The URL has a valid structure but the site does not exist.
- The HTTP client exceeds the connection timeout.
- The HTTP client exceeds the request timeout.
Fetch a favicon with a fallback value
masterUse the
fetchOrmethod to provide a default value if a favicon cannot be found. The second argument can be a string (the default URL) or aClosure. If aClosureis used, it receives the$urlbeing queried as its first argument.use AshAllenDesign\FaviconFetcher\Facades\Favicon; // Using a string fallback $favicon = Favicon::fetchOr('https://ashallendesign.co.uk', 'https://example.com/favicon.ico'); // Using a Closure fallback $favicon = Favicon::fetchOr('https://ashallendesign.co.uk', function ($url) { return 'https://example.com/favicon.ico'; });