Skip to content

Instantly share code, notes, and snippets.

@sh-sh-dev
Created March 18, 2023 13:34
Show Gist options
  • Save sh-sh-dev/a3f447f7986bad65e7e1e61578e3e96a to your computer and use it in GitHub Desktop.
Save sh-sh-dev/a3f447f7986bad65e7e1e61578e3e96a to your computer and use it in GitHub Desktop.
<?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