为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕哥6287543 2024-01-19 20:47:49
 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');    }
查看完整描述

2 回答

?
慕仙森

TA贡献1827条经验 获得超7个赞

在 html 表单中添加 enctype

enctype="multipart/form-data"

并在您的控制器中对此进行更改:

$img = Image::make($request->file('gambar')->getRealPath());

另请检查上传文件的目录的权限


查看完整回答
反对 回复 2024-01-19
?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

确保表单具有 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');

}


查看完整回答
反对 回复 2024-01-19
  • 2 回答
  • 0 关注
  • 32 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信