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

问题描述

 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)->save('public/uploads/', $gambar->getClientOriginalName()); $gambar->move('上传', $new_gambar); $post->tags()->attach($request->tags);

        return redirect('post');
    }

标签: phplaravel

解决方案


确保表单具有 enctype:

<form class="form" ... enctype="multipart/form-data">

更改控制器

use Intervention\Image\ImageManagerStatic as Image;

public function store(Request $request)
{
  $this->validate($request, [
    // 'judul'       => 'required',
    //if judul column type is varchar need to set max varchar value or less
    //varchar max 255, if higer than 255 strings, extra string will be truncated
    'judul'       => 'required|string|max:200', 
    // 'category_id' => 'required',
    //category should exist
    'category_id' => 'required|exists:categories,id',
    'konten'      => 'required',
    // 'gambar'      => 'required',
    //validating image is successfully uploaded and is image format
    'gambar'      => 'required|file|image|mimes:jpg,jpeg,png',
    //validation for tags, assuming 1 input <select name="tags[]" multiple="multiple"/>
    'tags'        => 'array',
    'tags.*'        => 'exists:tags,id'//each value of input select exists in tags table
  ]);

  // $gambar = $request->gambar; 
  //get the file from <input type="file" name="gambar"/>
  $gambar = $request->file('gambar'); 
  $new_gambar = time().$gambar->getClientOriginalName();
  //make path to save image: sample public path
  $file_path = public_path("uploads/post/{$new_gambar}"); 

  $img = Image::make($gambar)
  ->resize(300,300)
  ->save($file_path);

  $post = Posts::create([
    'judul'       => $request->judul,
    'category_id' => $request->category_id,
    'konten'      => $request->konten,
    // 'gambar'      =>  'public/uploads/posts/'.$new_gambar,
    //should maake the image first
    'gambar'      => $file_path,
    'slug'        => Str::slug($request->judul),
    'users_id'    => Auth::id() // <- if it is to get current logged in user, use  Auth::user()->id
  ]);

  // $gambar->move('uploads', $new_gambar); //let Intervention do this for you
  // $post->tags()->attach($request->tags); 
  //if tags exists (get values frominput select)
  $post->tags()->sync($request->input('tags', []));
  //$request->input('tags', []) <- if input tags is not null get value, else use empty array
  //if route have name e.g Route::get('post', 'PostController@post')->name('post');
  //return redirect()->route('post');
  return redirect('post');
}

推荐阅读