mirror of
https://github.com/archtechx/laravel-seo.git
synced 2025-12-12 09:54:03 +00:00
wip
This commit is contained in:
parent
5ca343a116
commit
03c7fa5eee
5 changed files with 98 additions and 35 deletions
12
README.md
12
README.md
|
|
@ -104,9 +104,19 @@ seo()->twitterTitle('About us')
|
|||
By default, no favicon links will be included. You can manually enable the extension by calling:
|
||||
|
||||
```php
|
||||
seo()->favicon('path/to/logo.png');
|
||||
seo()->favicon();
|
||||
```
|
||||
|
||||
## Generating favicons
|
||||
|
||||
To generate favicon, run:
|
||||
|
||||
```
|
||||
php artisan seo:generate-favicons public/path-to/logo.png
|
||||
```
|
||||
|
||||
from the artisan console.
|
||||
|
||||
We'll generate a 32x32px `public/favicon.ico` & `public/favicon.png` icon. This should be sufficient for most cases.
|
||||
|
||||
**Please keep in mind that you need to install the [imagick](https://pecl.php.net/package/imagick) php extension and [intervention/image](http://image.intervention.io/) composer package.**
|
||||
|
|
|
|||
61
src/Commands/GenerateFaviconsCommand.php
Normal file
61
src/Commands/GenerateFaviconsCommand.php
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
<?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 Command::FAILURE;
|
||||
}
|
||||
|
||||
$this->info('Generating favicons...');
|
||||
|
||||
if (! class_exists(ImageManager::class)) {
|
||||
$this->error('Intervention not available, please run `composer require intervention/image`');
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
if (! file_exists($path)) {
|
||||
$this->error("Given icon path `{$path}` does not exist.");
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// GD driver doesn't support .ico, that's why we use ImageMagick.
|
||||
$manager = new ImageManager(['driver' => 'imagick']);
|
||||
|
||||
$this->comment('Generating ico...');
|
||||
|
||||
$manager
|
||||
->make($path)
|
||||
->resize(32, 32)
|
||||
->save(public_path('favicon.ico'));
|
||||
|
||||
$this->comment('Generating png...');
|
||||
|
||||
$manager
|
||||
->make($path)
|
||||
->resize(32, 32)
|
||||
->save(public_path('favicon.png'));
|
||||
|
||||
$this->info('All favicons have been generated!');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
@ -5,9 +5,7 @@ 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.
|
||||
|
|
@ -179,36 +177,10 @@ class SEOManager
|
|||
}
|
||||
|
||||
/** Enable favicon extension. */
|
||||
public function favicon(string $path): static
|
||||
public function favicon(): static
|
||||
{
|
||||
if (! class_exists(ImageManager::class)) {
|
||||
throw new Exception('Intervention not available, please run `composer require intervention/image`');
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace ArchTech\SEO;
|
||||
|
||||
use ArchTech\SEO\Commands\GenerateFaviconsCommand;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use ImLiam\BladeHelper\BladeHelperServiceProvider;
|
||||
use ImLiam\BladeHelper\Facades\BladeHelper;
|
||||
|
|
@ -20,6 +21,12 @@ class SEOServiceProvider extends ServiceProvider
|
|||
{
|
||||
$this->loadViewsFrom(__DIR__ . '/../assets/views', 'seo');
|
||||
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->commands([
|
||||
GenerateFaviconsCommand::class,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->publishes([
|
||||
__DIR__ . '/../assets/views' => resource_path('views/vendor/seo'),
|
||||
], 'seo-views');
|
||||
|
|
|
|||
|
|
@ -1,14 +1,27 @@
|
|||
<?php
|
||||
|
||||
use ArchTech\SEO\Commands\GenerateFaviconsCommand;
|
||||
|
||||
use function Pest\Laravel\artisan;
|
||||
use function PHPUnit\Framework\assertFileExists;
|
||||
|
||||
test("it should throw an exception if the given source icon doesn't exist", function () {
|
||||
seo()->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');
|
||||
seo()->favicon();
|
||||
|
||||
$from = __DIR__ . '/../stubs/logo.png';
|
||||
|
||||
artisan(GenerateFaviconsCommand::class, [
|
||||
'from' => $from,
|
||||
])->assertSuccessful();
|
||||
|
||||
assertFileExists(public_path('favicon.ico'));
|
||||
assertFileExists(public_path('favicon.png'));
|
||||
});
|
||||
|
||||
test('it should fail because the from path is incorrect', function () {
|
||||
seo()->favicon();
|
||||
|
||||
artisan(GenerateFaviconsCommand::class, [
|
||||
'from' => 'i/dont/exist.png',
|
||||
])->assertFailed();
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue