Laravel 图像干预调整大小并放入存储

Sve*_*art 5 laravel intervention

当用户上传图片时,我想以多种格式存储它。我处理图像的代码:

$img = Image::make($file)->encode('png');
if($img->width()>3000){
    $img->resize(3000, null, function ($constraint) {
        $constraint->aspectRatio();
    });
}
if($img->height()>3000){
    $img->resize(null, 3000, function ($constraint) {
        $constraint->aspectRatio();
    });
}
$uid = Str::uuid();
$fileName = Str::slug($item->name . $uid).'.png';

$high =  clone $img;
Storage::put(  $this->getUploadPath($bathroom->id, $fileName, "high"), $high);


$med =  clone  $img;
$med->fit(1000,1000);

Storage::put(  $this->getUploadPath($bathroom->id, $fileName, "med"), $med);

$thumb = clone   $img;
$thumb->fit(700,700);
Storage::put(  $this->getUploadPath($bathroom->id, $fileName, "thumb"), $thumb);
Run Code Online (Sandbox Code Playgroud)

如您所见,我尝试了一些变体。

我也试过:

    $thumb = clone   $img;
    $thumb->resize(400, 400, function ($constraint) {
        $constraint->aspectRatio();
    });
    Storage::put(  $this->getUploadPath($fileName, "thumb"), $thumb);
Run Code Online (Sandbox Code Playgroud)

getUploadPath 函数:

public function  getUploadPath($id, $filename, $quality = 'high'){
    return 'public/img/bathroom/'.$id.'/'.$quality.'/'.$filename;
}
Run Code Online (Sandbox Code Playgroud)

我希望图像适合 xpx x xpx 而不缩放或降级质量。图像按预期创建和存储,但未调整图像大小。如何使图像调整大小?

BAB*_*AFI 4

$thumb->stream();在通过 Facade 保存之前,您需要对其进行流式传输 ( ) Storage,如下所示:

$thumb = clone   $img;
$thumb->resize(400, 400, function ($constraint) {
    $constraint->aspectRatio();
});

$thumb->stream();

Storage::put(  $this->getUploadPath($fileName, "thumb"), $thumb);
Run Code Online (Sandbox Code Playgroud)