gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:11:34 25 4
gpt4 key购买 nike

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

$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);
如您所见,我尝试了一些变体。
我也试过:
    $thumb = clone   $img;
$thumb->resize(400, 400, function ($constraint) {
$constraint->aspectRatio();
});
Storage::put( $this->getUploadPath($fileName, "thumb"), $thumb);
getUploadPath 函数:
public function  getUploadPath($id, $filename, $quality = 'high'){
return 'public/img/bathroom/'.$id.'/'.$quality.'/'.$filename;
}
我希望图像适合 xpx x xpx 而不缩放或降级质量。
图像按预期创建和存储,但未调整图像大小。如何使图像调整大小?

最佳答案

在通过 $thumb->stream(); 保存之前,您需要流式传输它( Storage )立面如下:

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

$thumb->stream();

Storage::put( $this->getUploadPath($fileName, "thumb"), $thumb);

关于Laravel 图像干预调整大小并放入存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62377351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com