Skip to content

Commit

Permalink
Added the optimizer command and the config file to make the path cust…
Browse files Browse the repository at this point in the history
…omizable
  • Loading branch information
abdelhamiderrahmouni committed Feb 7, 2024
1 parent 7868afb commit 9fa4ac5
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 54 deletions.
12 changes: 4 additions & 8 deletions composer.json
Expand Up @@ -17,8 +17,8 @@
],
"require": {
"php": "^8.1",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^10.0"
"spatie/image-optimizer": "^1.7",
"spatie/laravel-package-tools": "^1.14.0"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand All @@ -35,8 +35,7 @@
},
"autoload": {
"psr-4": {
"AbdelhamidErrahmouni\\ImageOptimizerCommand\\": "src/",
"AbdelhamidErrahmouni\\ImageOptimizerCommand\\Database\\Factories\\": "database/factories/"
"AbdelhamidErrahmouni\\ImageOptimizerCommand\\": "src/"
}
},
"autoload-dev": {
Expand Down Expand Up @@ -74,10 +73,7 @@
"laravel": {
"providers": [
"AbdelhamidErrahmouni\\ImageOptimizerCommand\\ImageOptimizerCommandServiceProvider"
],
"aliases": {
"ImageOptimizerCommand": "AbdelhamidErrahmouni\\ImageOptimizerCommand\\Facades\\ImageOptimizerCommand"
}
]
}
},
"minimum-stability": "dev",
Expand Down
4 changes: 4 additions & 0 deletions config/image-optimizer-command.php
Expand Up @@ -3,4 +3,8 @@
// config for AbdelhamidErrahmouni/ImageOptimizerCommand
return [

/*
* The path to the assets folder. Default is public/assets.
*/
'assets_path' => 'public/assets',
];
1 change: 0 additions & 1 deletion phpstan.neon.dist
Expand Up @@ -6,7 +6,6 @@ parameters:
paths:
- src
- config
- database
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
Expand Down
19 changes: 0 additions & 19 deletions src/Commands/ImageOptimizerCommandCommand.php

This file was deleted.

82 changes: 82 additions & 0 deletions src/Commands/OptimizerCommand.php
@@ -0,0 +1,82 @@
<?php

namespace AbdelhamidErrahmouni\ImageOptimizerCommand\Commands;

use Illuminate\Console\Command;
use Spatie\ImageOptimizer\OptimizerChainFactory;
use Symfony\Component\Finder\Finder;

class OptimizerCommand extends Command
{
protected $signature = 'images:compress
{path? : The path to the assets folder. Default is in config file (public/assets).}
{--details : Display the output of the optimization process.}';

protected $description = 'Compress all static images in the provided folder recursively!';

protected $errors = [];

protected $assetsDir = '';

public function handle(): int
{
$this->assetsDir = $this->argument('path') ?? config('image-optimizer-command.assets_path');

$finder = new Finder();

$this->info('🔍 Loading images from '.$this->assetsDir.'directory.');

// Find all files within the assets directory
$finder->files()->in($this->assetsDir);

$this->info('🚀 Optimizing images...');
$this->newLine();

$bar = $this->output->createProgressBar(count($finder));
if (! $this->option('details')) {
$bar->start();
}

foreach ($finder as $file) {
$extension = strtolower(pathinfo($file->getFilename(), PATHINFO_EXTENSION));
$supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp'];

if (in_array($extension, $supportedExtensions)) {
try {
$optimizerChain = OptimizerChainFactory::create();
$optimizerChain->optimize($file->getPathname());

if ($this->option('details')) {
$this->info("Optimized: {$file->getPathname()}");
} else {
$bar->advance();
}

} catch (\Exception $e) {
$this->errors[$file->getFilename()] = $e->getMessage();
}
}
}

if (! $this->option('details')) {
$bar->finish();
}

$this->newLine(2);
$this->info('🥳 All images in the '.$this->assetsDir.' folder have been optimized.');

$this->reportErrors();

return self::SUCCESS;
}

protected function reportErrors()
{
if (count($this->errors) > 0) {
$this->error('Some errors occurred during the optimization:');
foreach ($this->errors as $filename => $error) {
$this->error("{$filename}: {$error}");
}
}
}
}
16 changes: 0 additions & 16 deletions src/Facades/ImageOptimizerCommand.php

This file was deleted.

11 changes: 2 additions & 9 deletions src/ImageOptimizerCommandServiceProvider.php
Expand Up @@ -2,24 +2,17 @@

namespace AbdelhamidErrahmouni\ImageOptimizerCommand;

use AbdelhamidErrahmouni\ImageOptimizerCommand\Commands\OptimizerCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use AbdelhamidErrahmouni\ImageOptimizerCommand\Commands\ImageOptimizerCommandCommand;

class ImageOptimizerCommandServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
/*
* This class is a Package Service Provider
*
* More info: https://github.com/spatie/laravel-package-tools
*/
$package
->name('image-optimizer-command')
->hasConfigFile()
->hasViews()
->hasMigration('create_image-optimizer-command_table')
->hasCommand(ImageOptimizerCommandCommand::class);
->hasCommand(OptimizerCommand::class);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Expand Up @@ -2,9 +2,9 @@

namespace AbdelhamidErrahmouni\ImageOptimizerCommand\Tests;

use AbdelhamidErrahmouni\ImageOptimizerCommand\ImageOptimizerCommandServiceProvider;
use Illuminate\Database\Eloquent\Factories\Factory;
use Orchestra\Testbench\TestCase as Orchestra;
use AbdelhamidErrahmouni\ImageOptimizerCommand\ImageOptimizerCommandServiceProvider;

class TestCase extends Orchestra
{
Expand Down

0 comments on commit 9fa4ac5

Please sign in to comment.