Skip to content

Commit

Permalink
Feat: implement image compressor commands
Browse files Browse the repository at this point in the history
  • Loading branch information
alibaghernia committed May 12, 2024
1 parent ba3c1de commit a32a92b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
48 changes: 48 additions & 0 deletions app/Console/Commands/ConvertImagesToWebp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Imagick\Driver;

class ConvertImagesToWebp extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:images-to-webp';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle()
{
$basePath = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public';

$files = \File::files($basePath);
foreach ($files as $file) {
$imageType = exif_imagetype($file->getPathname());
if ($imageType === false) {
continue;
}
if ($imageType === IMAGETYPE_WEBP) {
dump($file->getPathname());
continue;
}
$manager = new ImageManager(new Driver());
$image = $manager->read($file->getPathname());
$image->toWebp()->save($basePath . '\\' . $file->getBasename());
}

}
}
47 changes: 47 additions & 0 deletions app/Console/Commands/ReduceImagesSizes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Intervention\Image\Drivers\Imagick\Driver;
use Intervention\Image\ImageManager;

class ReduceImagesSizes extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:reduce-images-sizes';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle()
{
$basePath = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public';

$files = \File::files($basePath);
foreach ($files as $file) {

if (exif_imagetype($file->getPathname()) === false) {
continue;
}

$manager = new ImageManager(new Driver());
$image = $manager->read($file->getPathname());
$image->scaleDown(900);
$image
->save($basePath . DIRECTORY_SEPARATOR . $file->getBasename());
}

}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"filament/spatie-laravel-translatable-plugin": "^3.2",
"guzzlehttp/guzzle": "^7.2",
"hekmatinasser/verta": "^8.4",
"intervention/image": "^3.3",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.3",
"laravel/tinker": "^2.8",
Expand Down

0 comments on commit a32a92b

Please sign in to comment.