增加生成缩略图命令

This commit is contained in:
Wisp X 2022-03-14 10:35:41 +08:00
parent 928727c149
commit bf26cb6699
2 changed files with 64 additions and 1 deletions

View File

@ -0,0 +1,58 @@
<?php
namespace App\Console\Commands;
use App\Models\Image;
use App\Services\ImageService;
use Illuminate\Console\Command;
use League\Flysystem\FilesystemException;
use Symfony\Component\Console\Helper\ProgressBar;
class MakeThumbnails extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'lsky:thumbnails';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Make images thumbnails.';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$progress = new ProgressBar($this->output, Image::query()->count());
$progress->setMessage('获取图片处理中...');
$progress->start();
$service = new ImageService();
/** @var Image $image */
foreach (Image::query()->whereNotNull('strategy_id')->cursor() as $image) {
try {
$service->makeThumbnail(
image: $image,
data: $image->filesystem()->read($image->pathname),
force: true,
);
$progress->advance();
} catch (\Throwable $e) {
$this->error("缩略图生成失败, {$e->getMessage()}");
}
}
$progress->finish();
return 0;
}
}

View File

@ -93,6 +93,8 @@ class Image extends Model
'permission' => 'integer',
];
protected ?Filesystem $filesystem = null;
protected static function booted()
{
static::creating(function (self $image) {
@ -206,7 +208,10 @@ class Image extends Model
public function filesystem(): Filesystem
{
return new Filesystem((new ImageService())->getAdapter($this->strategy));
if (is_null($this->filesystem)) {
$this->filesystem = new Filesystem((new ImageService())->getAdapter($this->strategy));
}
return $this->filesystem;
}
public function user(): BelongsTo