mirror of
https://github.com/lsky-org/lsky-pro.git
synced 2025-01-07 03:16:46 +08:00
增加生成缩略图命令
This commit is contained in:
parent
928727c149
commit
bf26cb6699
58
app/Console/Commands/MakeThumbnails.php
Normal file
58
app/Console/Commands/MakeThumbnails.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user