mirror of
https://github.com/archtechx/laravel-seo.git
synced 2025-12-12 18:04:03 +00:00
parent
5ca343a116
commit
7b24a50bd6
5 changed files with 113 additions and 34 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:
|
By default, no favicon links will be included. You can manually enable the extension by calling:
|
||||||
|
|
||||||
```php
|
```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. If no path argument is given we'll fallback to `public/assets/logo.png`.
|
||||||
|
|
||||||
We'll generate a 32x32px `public/favicon.ico` & `public/favicon.png` icon. This should be sufficient for most cases.
|
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.**
|
**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;
|
namespace ArchTech\SEO;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Exception;
|
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Intervention\Image\ImageManager;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @method $this title(string $title = null, ...$args) Set the title.
|
* @method $this title(string $title = null, ...$args) Set the title.
|
||||||
|
|
@ -179,36 +177,10 @@ class SEOManager
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Enable favicon extension. */
|
/** 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;
|
$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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace ArchTech\SEO;
|
namespace ArchTech\SEO;
|
||||||
|
|
||||||
|
use ArchTech\SEO\Commands\GenerateFaviconsCommand;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use ImLiam\BladeHelper\BladeHelperServiceProvider;
|
use ImLiam\BladeHelper\BladeHelperServiceProvider;
|
||||||
use ImLiam\BladeHelper\Facades\BladeHelper;
|
use ImLiam\BladeHelper\Facades\BladeHelper;
|
||||||
|
|
@ -20,6 +21,12 @@ class SEOServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
$this->loadViewsFrom(__DIR__ . '/../assets/views', 'seo');
|
$this->loadViewsFrom(__DIR__ . '/../assets/views', 'seo');
|
||||||
|
|
||||||
|
if ($this->app->runningInConsole()) {
|
||||||
|
$this->commands([
|
||||||
|
GenerateFaviconsCommand::class,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
$this->publishes([
|
$this->publishes([
|
||||||
__DIR__ . '/../assets/views' => resource_path('views/vendor/seo'),
|
__DIR__ . '/../assets/views' => resource_path('views/vendor/seo'),
|
||||||
], 'seo-views');
|
], 'seo-views');
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,43 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use ArchTech\SEO\Commands\GenerateFaviconsCommand;
|
||||||
|
|
||||||
|
use function Pest\Laravel\artisan;
|
||||||
|
use function PHPUnit\Framework\assertFileDoesNotExist;
|
||||||
use function PHPUnit\Framework\assertFileExists;
|
use function PHPUnit\Framework\assertFileExists;
|
||||||
|
|
||||||
test("it should throw an exception if the given source icon doesn't exist", function () {
|
// Clean up generated files
|
||||||
seo()->favicon('i-dont-exist.png');
|
beforeEach(function () {
|
||||||
})->throws(Exception::class, 'Given icon path `i-dont-exist.png` does not exist.');
|
$files = [
|
||||||
|
'favicon.ico',
|
||||||
|
'favicon.png',
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($files as $file) {
|
||||||
|
@unlink(public_path($file));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
test('it should generate two favicons', function () {
|
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.ico'));
|
||||||
assertFileExists(public_path('favicon.png'));
|
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();
|
||||||
|
|
||||||
|
assertFileDoesNotExist(public_path('favicon.ico'));
|
||||||
|
assertFileDoesNotExist(public_path('favicon.png'));
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue