LoginSignup
0
0

More than 3 years have passed since last update.

laravel 画像アップロードとリサイズ

Posted at

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

``

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0