Created
March 18, 2023 13:34
-
-
Save sh-sh-dev/a3f447f7986bad65e7e1e61578e3e96a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\Storage; | |
use Intervention\Image\Facades\Image; | |
class EncodeToWebp extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'encode-to-webp'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Encode photos to webp'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
* @throws \Exception | |
*/ | |
public function handle() | |
{ | |
$files = Storage::files('public/images/landing'); | |
foreach ($files as $file) { | |
$filename = explode('.', last(explode('/', $file)))[0]; | |
$originalImage = Storage::get($file); | |
$newImage = Image::make($originalImage); | |
$newImage->encode('webp', 80); | |
$newImage->save('storage/app/public/images/landing/' . $filename . '.webp'); | |
} | |
return self::SUCCESS; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment