Php 上传后,小尺寸图像最终变成大尺寸
当我上传400KB的图像时,它最终得到了1.3MBPhp 上传后,小尺寸图像最终变成大尺寸,php,laravel,laravel-5.2,Php,Laravel,Laravel 5.2,当我上传400KB的图像时,它最终得到了1.3MB $project = Project::findOrFail($project_id); $image_type = 'project_thumbnail'; $destinationPath = 'assets/images/projects/'.$project_id; $filename = $request->file->getClientOriginalName(); $filen
$project = Project::findOrFail($project_id);
$image_type = 'project_thumbnail';
$destinationPath = 'assets/images/projects/'.$project_id;
$filename = $request->file->getClientOriginalName();
$filename = time().'_'.$filename;
$extension = $request->file->getClientOriginalExtension();
$photo = $request->file->move($destinationPath, $filename);
$photo= Image::make($destinationPath.'/'.$filename);
$photo->resize(1024, 683, function ($constraint) {
$constraint->aspectRatio();
})->save();
$media = new \App\Media(['type'=>$image_type, 'filename'=>$filename, 'path'=>$destinationPath.'/'.$filename, 'thumbnail_path'=>$destinationPath.'/'.$filename,'extension'=>$extension]);
$project->media()->save($media);
return 1;
但是,如果我上传1.3MB的图像文件,它根本不会改变
先谢谢你
编辑1
我试过了
$photo->resize(null, function ($constraint) {
$constraint->aspectRatio();
})->save();
也
但似乎什么也没用。看起来您在保存之前正在调整图像的大小
$photo->resize(1024, 683, function ($constraint) {
$constraint->aspectRatio();
})->save();
调整大小会更改图像的大小
因此,可能发生的情况是,您的初始图像,重量为400KB,其分辨率小于1024。然后将分辨率更改为1024,这会将其大小增加到1.3MB
然后你上传1.3MB的图像,它的分辨率已经达到1024,所以它不会被
resize()
改变,它的大小保持在1.3MB。我尝试过删除resize,但同样的事情也发生了。
$photo->resize(1024, 683, function ($constraint) {
$constraint->aspectRatio();
})->save();