Search
Site Essentials for Statamic

Site Essentials for Statamic

Generating Favicons

There are two primary ways to trigger favicon image generation: using the command line and programmatically, such as from listening to Statamic events.

Before we can generate favicons, we need to let Site Essentials for Statamic know what image to use as the source image. This is done by using the Favicon facade's getSourceUsing method. The returned value of this method must be the path to the source image on the local filesystem, or null if an image is not available.

As an example, this is how we might use an asset stored within a global set:

1<?php
2 
3namespace App\Providers;
4 
5use Illuminate\Support\ServiceProvider;
6use Statamic\Facades\GlobalSet;
7use Stillat\StatamicSiteEssentials\Support\Facades\Favicons;
8 
9class AppServiceProvider extends ServiceProvider
10{
11 /**
12 * Bootstrap any application services.
13 */
14 public function boot(): void
15 {
16 Favicons::getSourceUsing(function () {
17 $settings = GlobalSet::find('site_settings')->inDefaultSite();
18 
19 if (! $settings) {
20 return null;
21 }
22 
23 $favicon = $settings['favicon'];
24 
25 if (! $favicon) {
26 return null;
27 }
28 
29 $assetContainer = $favicon->container();
30 
31 return $assetContainer->disk()->path($favicon->path());
32 });
33 }
34 
35}

If you have a static file you'd like to use a favicon source, you can also simply return a path to that as well:

1<?php
2 
3namespace App\Providers;
4 
5use Illuminate\Support\ServiceProvider;
6use Stillat\StatamicSiteEssentials\Support\Facades\Favicons;
7 
8class AppServiceProvider extends ServiceProvider
9{
10 /**
11 * Bootstrap any application services.
12 */
13 public function boot(): void
14 {
15 Favicons::getSourceUsing(function () {
16 return resource_path('my/favicon.svg');
17 });
18 }
19 
20}

#Generating Favicons Using the Command Line

Favicons can be generated from the command line by running the following Artisan command from the root of your project:

1php artisan site-essentials:generate-favicons

#Programmatically Generating Favicons

We can programmatically generate favicons using the Favicons facade:

1<?php
2 
3use Stillat\StatamicSiteEssentials\Support\Facades\Favicons;
4 
5Favicons::generate();

Typical use-cases for doing this are listening for changes to assets contained in a global set, or some other entry that contains a configurable favicon.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.