php - 我正在尝试使用“干预\图像库”保存图像。我发现一个错误图像源不可读
问题描述
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'); }
解决方案
确保表单具有 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');
}
推荐阅读
- eclipse - 如何使用颜色语法突出显示在 Spring Tool Suite/Eclipse 中显示百里香模板的注释部分?
- cpu - 如何触发CPU突发操作?
- project-organization - 如何更改 Azure DevOps 上的组织所有者?
- docker - 来自守护程序的 Docker 错误响应:处理 tar 文件时出错(退出状态 1):设备上没有剩余空间
- azure - 如何在 azure 中自动运行 powershell 脚本?
- python - Vigenere Cipher 仅在字符上输出
- python-3.x - NameError:实例化自定义 OAuth2 类后未定义名称“请求”错误
- javascript - 返回新洗牌元素的索引
- java - Intellij 无法设置 VM 选项
- java - 如何为 Java 程序提供用户输入,在 Jenkins 中作为自由样式项目运行?