首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在laravel-9中难以使用干预/图像

在laravel-9中难以使用干预/图像
EN

Stack Overflow用户
提问于 2022-10-11 04:30:32
回答 1查看 48关注 0票数 0

上传之前,我试图调整图像的大小,但没有工作,但我能够上传图像,以下是我的控制器:

代码语言:javascript
复制
public function store(Request $request)
{


    $validatedData = $request->validate([
        'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:20480',

       ]);
       $imageFile = $request->file('image');
    
       $name = $imageFile->getClientOriginalName();    
       $path = $request->file('image')->store('images', 'public');
       $resize = Image::make($imageFile)->resize(50, 50)->stream();
       $save = new Gallery;
       
       $save->image = $path;
       $save->status = 1;
       $save->user_id = Auth::id();
       $save->save();

     return redirect('gallery')->with('success', 'Image has been uploaded')->with('image',$name);
}
EN

回答 1

Stack Overflow用户

发布于 2022-10-11 04:40:34

尝试下面的示例源代码。我在Laravel版本8的一个项目中完成了图像大小调整功能。

代码语言:javascript
复制
<?php
if($request->hasFile('dealimg')){
    $file = $request->File('dealimg');
    $original_name = $file->getClientOriginalName();  
    $file_ext = $file->getClientOriginalExtension();  
    $destinationPath = 'uploads/deals';
    $file_name = "deal".time().uniqid().".".$file_ext;
    // $path = $request->deal_img->store('uploads'); 

    $resize_image = Image::make($file->getRealPath()); //for Resize the Image
    $resize_image->resize(150, 150, function($constraint){ //resize with 150 x 150 ratio
        $constraint->aspectRatio();
    })->save(public_path($destinationPath) . '/' . $file_name); 
    
    $deal_img = $destinationPath."/".$file_name;  
    $request->request->add(['deal_img' => $deal_img]);
}
?>

希望这个对你有帮助..。看看这个

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74022996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档