【发布时间】:2020-07-12 09:57:35
【问题描述】:
当用户上传图片时,我想以多种格式存储它。 我处理图像的代码:
$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 而不缩放或降低质量。 图像按预期创建和存储,但未调整图像大小。如何调整图片大小?
【问题讨论】:
-
你试过
$img->save();吗?
标签: laravel intervention