1
0
Fork 0
mirror of https://github.com/archtechx/laravel-seo.git synced 2026-05-06 09:44:03 +00:00
laravel-seo/src/Commands/GenerateFaviconsCommand.php
Al Amin Ahamed 713ecea0d5
Intervention Image v3 support (#45)
* fix: type error to assign the drie for image manager class

* fix: update package names and handle Intervention Image versioning in favicon generation

* fix: update intervention/image version constraint in composer.json

* cleanup, run CI tests with both intervention v2 and v3

* ci: composer require --dev

* phpstan fixes

---------

Co-authored-by: Samuel Štancl <samuel@archte.ch>
2026-03-31 03:48:26 +02:00

79 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace ArchTech\SEO\Commands;
use Illuminate\Console\Command;
use Intervention\Image\ImageManager;
class GenerateFaviconsCommand extends Command
{
protected $signature = 'seo:generate-favicons {from?}';
protected $description = 'Generate favicons based on a source file';
public function handle(): int
{
$path = $this->argument('from') ?? public_path('assets/logo.png');
if (! is_string($path)) {
$this->error('The `from` argument must be a string.');
return self::FAILURE;
}
$this->info('Generating favicons...');
if (! class_exists(ImageManager::class)) {
$this->error('Intervention not available, please run `composer require intervention/image`');
return self::FAILURE;
}
if (! file_exists($path)) {
$this->error("Given icon path `{$path}` does not exist.");
return self::FAILURE;
}
// Check Intervention Image version
$interventionV3 = interface_exists('\Intervention\Image\Interfaces\DriverInterface');
if ($interventionV3) {
// v3.x implementation
$manager = new ImageManager(
new \Intervention\Image\Drivers\Imagick\Driver()
);
$this->comment('Generating ico...');
$image = $manager->read($path);
$image->resize(32, 32);
$image->save(public_path('favicon.ico'));
$this->comment('Generating png...');
$image = $manager->read($path);
$image->resize(32, 32);
$image->save(public_path('favicon.png'));
} else {
// v2.x implementation
$manager = new ImageManager(['driver' => 'imagick']); // @phpstan-ignore argument.type
$this->comment('Generating ico...');
$manager
->make($path) // @phpstan-ignore method.notFound
->resize(32, 32)
->save(public_path('favicon.ico'));
$this->comment('Generating png...');
$manager
->make($path) // @phpstan-ignore method.notFound
->resize(32, 32)
->save(public_path('favicon.png'));
}
$this->info('All favicons have been generated!');
return self::SUCCESS;
}
}