Download the PHP package ashallendesign/favicon-fetcher without Composer

On this page you can find all versions of the php package ashallendesign/favicon-fetcher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package favicon-fetcher

Latest Version on Packagist Total Downloads PHP from Packagist GitHub license

Table of Contents

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:

Install the Package

You can install the package via Composer:

Publish the Config

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

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:

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:

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:

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:

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:

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:

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:

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:

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:

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.

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.

How to Choose a Driver

It's important to remember that the google-shared-stuff, favicon-kit, and unavatar 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:

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:

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:

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:

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:

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

HTTP Timeouts

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

The connection timeout is the time that the package will wait for a connection to be made to the website. The request timeout is the time that the package will wait for the website to respond to the request.

To do this, you can update the connect_timeout and timeout fields in the favicon-fetcher.php config file after you've published it. For example, to set the connection timeout to 5 seconds and the request timeout to 10 seconds, you could update your config file like so:

If you'd prefer that no timeout be set, you can set the values to 0.

Please note that these timeouts are applied to all HTTP requests that Favicon Fetcher makes, regardless of the driver that is being used.

TLS Verification

Favicon Fetcher uses TLS verification by default, but this can be disabled. This can be useful in development environments or situations where you might be working with self-signed certificates or certificates from an untrusted certificate authority.

You can disable the verification by updating the verify_tls field in the favicon-fetcher.php config file after you've published it.

Or by updating your .env file:

HTTP User Agent

You may find that your requests are sometimes blocked by websites when trying to retrieve a favicon. This may be due to the fact that the default Guzzle User-Agent header is passed in the requests.

Favicon Fetcher allows you to set the User-Agent header that is used in the package's requests. To do this, you can update the user_agent field in the favicon-fetcher.php config file after you've published it. For example, to set the User-Agent header to My Custom User Agent, you could update your config file like so:

The User-Agent header will be set on all HTTP requests that Favicon Fetcher makes, regardless of the driver that is being used.

The user_agent config field is already configured in the config file to read directly from a FAVICON_FETCHER_USER_AGENT field in your .env file. So, if you'd prefer to set the User-Agent header in your .env file, you could do so like this:

Storing Favicons

After fetching favicons, you might want to store them in your filesystem so that you don't need to fetch them again in the future. Favicon Fetcher provides two methods that you can use for storing the favicons: store and storeAs.

Using store

If you use the store method, a filename will automatically be generated for the favicon before storing. The method's first parameter accepts a string and is the directory that the favicon will be stored in. You can store a favicon using your default filesystem disk like so:

If you'd like to use a different storage disk, you can pass it as an optional second argument to the store method. For example, to store the favicon on S3, your code use the following:

Using storeAs

If you use the storeAs method, you will be able to define the filename that the file will be stored as. The method's first parameter accepts a string and is the directory that the favicon will be stored in. The second parameter specifies the favicon filename (excluding the file extension). You can store a favicon using your default filesystem disk like so:

If you'd like to use a different storage disk, you can pass it as an optional third argument to the storeAs method. For example, to store the favicon on S3, your code use the following:

Caching Favicons

As well as being able to store favicons, the package also allows you to cache the favicon URLs. This can be extremely useful if you don't want to store a local copy of the file and want to use the external version of the favicon that the website uses.

As a basic example, if you have a page displaying 50 websites and their favicons, we would need to find the favicon's URL on each page load. As can imagine, this would drastically increase the page load time. So, by retrieving the URLs from the cache, it would majorly improve up the page speed.

To cache a favicon, you can use the cache method available on the Favicon class. The first parameter accepts a Carbon\CarbonInterface as the cache lifetime. For example, to cache the favicon URL of https://ashallendesign.co.uk for 1 day, your code might look something like:

By default, the package will always try and resolve the favicon from the cache before attempting to retrieve a fresh version. However, if you want to disable the cache and always retrieve a fresh version, you can use the useCache method like so:

The package uses favicon-fetcher as a prefix for all the cache keys. If you'd like to change this, you can do so by changing the cache.prefix field in the favicon-fethcher config file. For example, to change the prefix to my-awesome-prefix, you could update your config file like so:

The package also provides the functionality for you to cache collections of favicons that have been retrieved using the fetchAll method. You can do this by calling the cache method on the FaviconCollection class like so:

Favicon Types

When attempting to retrieve favicons using the http driver, we may be able to determine the favicons' type (such as icon, shortcut icon, or apple-touch-icon). To get the type of the favicon, you can use the getIconType method like so:

This method can return one of four constants defined on the Favicon class: TYPE_ICON, TYPE_SHORTCUT_ICON, TYPE_APPLE_TOUCH_ICON, and TYPE_ICON_UNKNOWN.

You can make use of these constants for things like filtering. For example, if you wanted to get all the icons except the apple-touch-icon, you could do the following:

Favicon Sizes

When attempting to retrieve favicons using the http driver, we may be able to determine the favicons' sizes. To get the size of the favicon, you can use the getIconSize method like so:

It's assumed that the icons are square, so only a single integer will be returned. For example, if a favicon is 16x16px, then the getIconSize method will return 16. If the size is unknown, null will be returned.

Testing

To run the package's unit tests, run the following command:

To run Larastan for the package, run the following command:

Security

If you find any security related issues, please contact me directly at [email protected] to report it.

Contribution

If you wish to make any changes or improvements to the package, feel free to make a pull request.

To contribute to this package, please use the following guidelines before submitting your pull request:

Changelog

Check the CHANGELOG to get more information about the latest changes.

Upgrading

Check the UPGRADE guide to get more information on how to update this library to newer versions.

Credits

License

The MIT License (MIT). Please see License File for more information.

Support Me

If you've found this package useful, please consider buying a copy of Battle Ready Laravel to support me and my work.

Every sale makes a huge difference to me and allows me to spend more time working on open-source projects and tutorials.

To say a huge thanks, you can use the code BATTLE20 to get a 20% discount on the book.

👉 Get Your Copy!

Battle Ready Laravel


All versions of favicon-fetcher with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
nesbot/carbon Version ^2.0|^3.0
illuminate/cache Version ^9.0|^10.0|^11.0
illuminate/filesystem Version ^9.0|^10.0|^11.0
illuminate/http Version ^9.0|^10.0|^11.0
guzzlehttp/guzzle Version ^7.4
symfony/dom-crawler Version ^6.3 || ^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ashallendesign/favicon-fetcher contains the following files

Loading the files please wait ....