laravelでの画像アップロードとリサイズについてまとめていきたいと思います。
こちらで画像アップロード機能の説明を行っています。よかったら見てください。
Post.Controller.php
public function store(PostRequest $request)
{
$post = new Post;
//ファイルが存在しているか
if($request->hasFile('path')){
$file = $request->file('path');//ファイルを取得
$filename = $file->getClientOriginalName();//ファイル名取得
Image::make($file)
->resize(300, 300)
->save(public_path( 'storage/post_image/' . $filename ));
$post->path = $filename;
}
$post->save();
return redirect('/post')->with('success', '投稿しました!');
}
$file = $request->file('path');
でformからのファイルを取得してファイル名を
$filename = $file->getClientOriginalName();
で取得。
Image::make($file)
->resize(300, 300)
->save(public_path( 'storage/post_image/' . $filename ))
リサイズしたいファイルを渡し、サイズ指定し、保存。
public_pathでは画像を読み込む必要があるので、storage/〇〇としてください
保存:storage/app/public
読込:public/storage
``