diff --git a/assets/views/components/extensions/favicon.blade.php b/assets/views/components/extensions/favicon.blade.php new file mode 100644 index 0000000..bafadca --- /dev/null +++ b/assets/views/components/extensions/favicon.blade.php @@ -0,0 +1,2 @@ + + diff --git a/composer.json b/composer.json index 6c0e997..13e3322 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ "require": { "php": "^8.0", "illuminate/support": "^8.24", - "imliam/laravel-blade-helper": "^1.0" + "imliam/laravel-blade-helper": "^1.0", + "intervention/image": "^2.7" }, "require-dev": { "orchestra/testbench": "^6.9", diff --git a/src/SEOManager.php b/src/SEOManager.php index d036034..c45e866 100644 --- a/src/SEOManager.php +++ b/src/SEOManager.php @@ -5,7 +5,9 @@ declare(strict_types=1); namespace ArchTech\SEO; use Closure; +use Exception; use Illuminate\Support\Str; +use Intervention\Image\ImageManager; /** * @method $this title(string $title = null, ...$args) Set the title. @@ -176,6 +178,36 @@ class SEOManager return $this->set('image', "https://s.useflipp.com/{$template}.png?s={$signature}&v={$query}"); } + /** Enable favicon extension. */ + public function favicon(string $path): static + { + $this->extensions['favicon'] = true; + + $doesntHaveFavicon = ! file_exists(public_path('favicon.ico')); + $sourceIconDoesntExist = ! file_exists($path); + + if ($sourceIconDoesntExist) { + throw new Exception("Given icon path `{$path}` does not exist."); + } + + if ($doesntHaveFavicon) { + // GD driver doesn't support .ico, that's why we use ImageMagick. + $manager = new ImageManager(['driver' => 'imagick']); + + $manager + ->make($path) + ->resize(32, 32) + ->save(public_path('favicon.ico')); + + $manager + ->make($path) + ->resize(32, 32) + ->save(public_path('favicon.png')); + } + + return $this; + } + /** Append canonical URL tags to the document head. */ public function withUrl(): static { diff --git a/tests/Pest/FaviconTest.php b/tests/Pest/FaviconTest.php new file mode 100644 index 0000000..3a1d914 --- /dev/null +++ b/tests/Pest/FaviconTest.php @@ -0,0 +1,14 @@ +favicon('i-dont-exist.png'); +})->throws(Exception::class, 'Given icon path `i-dont-exist.png` does not exist.'); + +test('it should generate two favicons', function() { + seo()->favicon(__DIR__ . '/../stubs/logo.png'); + + assertFileExists(public_path('favicon.ico')); + assertFileExists(public_path('favicon.png')); +}); diff --git a/tests/stubs/logo.png b/tests/stubs/logo.png new file mode 100644 index 0000000..afac3bb Binary files /dev/null and b/tests/stubs/logo.png differ