我正在尝试使用“干预\图像库”保存图像。我发现一个错误图像源不可读

 public function store(Request $request)

    {

      

        $this->validate($request, [

            'judul'       => 'required',

            'category_id' => 'required',

            'konten'     => 'required',

            'gambar'      => 'required',

        ]);


        $gambar = $request->gambar;

        $new_gambar = time().$gambar->getClientOriginalName();

        

        $post = Posts::create([

            'judul'       => $request->judul,

            'category_id' => $request->category_id,

            'konten'      => $request->konten,

            'gambar'      =>  'public/uploads/posts/'.$new_gambar,

            'slug'        => Str::slug($request->judul),

            'users_id'    => Auth::id()

        ]);


        $img = Image::make('public/uploads/',$gambar->getRealPath())->resize(300,

300)->保存('public/uploads/', $gambar->getClientOriginalName()); $gambar->move('上传', $new_gambar); $post->tags()->attach($request->tags);


        return redirect('post');

    }


慕哥6287543
浏览 37回答 2
2回答

慕仙森

在 html 表单中添加 enctypeenctype="multipart/form-data"并在您的控制器中对此进行更改:$img = Image::make($request->file('gambar')->getRealPath());另请检查上传文件的目录的权限

繁星点点滴滴

确保表单具有 enctype:<form class="form" ... enctype="multipart/form-data">变更控制器use Intervention\Image\ImageManagerStatic as Image;public function store(Request $request){&nbsp; $this->validate($request, [&nbsp; &nbsp; // 'judul'&nbsp; &nbsp; &nbsp; &nbsp;=> 'required',&nbsp; &nbsp; //if judul column type is varchar need to set max varchar value or less&nbsp; &nbsp; //varchar max 255, if higer than 255 strings, extra string will be truncated&nbsp; &nbsp; 'judul'&nbsp; &nbsp; &nbsp; &nbsp;=> 'required|string|max:200',&nbsp;&nbsp; &nbsp; // 'category_id' => 'required',&nbsp; &nbsp; //category should exist&nbsp; &nbsp; 'category_id' => 'required|exists:categories,id',&nbsp; &nbsp; 'konten'&nbsp; &nbsp; &nbsp; => 'required',&nbsp; &nbsp; // 'gambar'&nbsp; &nbsp; &nbsp; => 'required',&nbsp; &nbsp; //validating image is successfully uploaded and is image format&nbsp; &nbsp; 'gambar'&nbsp; &nbsp; &nbsp; => 'required|file|image|mimes:jpg,jpeg,png',&nbsp; &nbsp; //validation for tags, assuming 1 input <select name="tags[]" multiple="multiple"/>&nbsp; &nbsp; 'tags'&nbsp; &nbsp; &nbsp; &nbsp; => 'array',&nbsp; &nbsp; 'tags.*'&nbsp; &nbsp; &nbsp; &nbsp; => 'exists:tags,id'//each value of input select exists in tags table&nbsp; ]);&nbsp; // $gambar = $request->gambar;&nbsp;&nbsp; //get the file from <input type="file" name="gambar"/>&nbsp; $gambar = $request->file('gambar');&nbsp;&nbsp; $new_gambar = time().$gambar->getClientOriginalName();&nbsp; //make path to save image: sample public path&nbsp; $file_path = public_path("uploads/post/{$new_gambar}");&nbsp;&nbsp; $img = Image::make($gambar)&nbsp; ->resize(300,300)&nbsp; ->save($file_path);&nbsp; $post = Posts::create([&nbsp; &nbsp; 'judul'&nbsp; &nbsp; &nbsp; &nbsp;=> $request->judul,&nbsp; &nbsp; 'category_id' => $request->category_id,&nbsp; &nbsp; 'konten'&nbsp; &nbsp; &nbsp; => $request->konten,&nbsp; &nbsp; // 'gambar'&nbsp; &nbsp; &nbsp; =>&nbsp; 'public/uploads/posts/'.$new_gambar,&nbsp; &nbsp; //should maake the image first&nbsp; &nbsp; 'gambar'&nbsp; &nbsp; &nbsp; => $file_path,&nbsp; &nbsp; 'slug'&nbsp; &nbsp; &nbsp; &nbsp; => Str::slug($request->judul),&nbsp; &nbsp; 'users_id'&nbsp; &nbsp; => Auth::id() // <- if it is to get current logged in user, use&nbsp; Auth::user()->id&nbsp; ]);&nbsp; // $gambar->move('uploads', $new_gambar); //let Intervention do this for you&nbsp; // $post->tags()->attach($request->tags);&nbsp;&nbsp; //if tags exists (get values frominput select)&nbsp; $post->tags()->sync($request->input('tags', []));&nbsp; //$request->input('tags', []) <- if input tags is not null get value, else use empty array&nbsp; //if route have name e.g Route::get('post', 'PostController@post')->name('post');&nbsp; //return redirect()->route('post');&nbsp; return redirect('post');}
打开App,查看更多内容
随时随地看视频慕课网APP