現在Laravelで独自サイトを製作中なのですが、localで表示されていた画像ファイルが本番環境で動作されない不具合が起きました。
以下が該当するviewとコントローラのコードになります。
single.blaade.php
1<h2>タイトル: {{$post -> title}} 2 <small>投稿日:{{ date("Y年 m月 d日",strtotime($post->created_at)) }}</small> 3</h2> 4 5<img class="logo" src="{{ asset($post -> image) }}" alt="logo"> 6 7<h5>カテゴリー:{{ $post->cat_name }}</h5> 8<h5>{{ $post->content }}</h5>
PostsController
1$post = \DB::table('posts') ->select('posts.id as post_id', 'posts.title as title','posts.content','posts.image','posts.created_at','categories.name as cat_name' ) -> where('posts.id', $id) -> leftjoin('categories', 'posts.cat_id', '=', 'categories.id') ->get() ->first();
また、画像を挿入する側のコードは以下のようになります。
create.blade.php
1 <div class="form-group"> 2 <label for="image" class="">添付ファイル</label> 3 <div class=""> 4 {{ Form::file('image', null) }} 5 </div> 6 </div>
PostsController
1 public function store(Request $request) 2 { 3 $params = $request->validate([ 4 'title' => 'required|max:50', 5 'content' => 'required|max:200', 6 'cat_id' => 'required', 7 'image' => 'required|file|image|max:4000', 8 ]); 9 10 $file = $params['image']; 11 12 $image = \Image::make(file_get_contents($file->getRealPath())); 13 $image 14 ->save(public_path().'/images/'.$file->hashName()); 15 16 $ext = $file->getClientOriginalExtension(); 17 18 if($ext == 'jpg' or $ext == 'jpeg') { 19 imagejpeg(imagecreatefromjpeg($file),public_path().'/images/'.'compre'.$file->hashName()); 20 } elseif($ext == 'png') { 21 imagepng(imagecreatefrompng($file),public_path().'/images/'.'comore'.$file->hashName()); 22 } 23 $params['image'] = '/images/'.$file->hashName(); 24 25 26 \DB::table('posts')->insert($params); 27 28 return redirect()->route('bbc.index'); 29 }
これらのコードをさくらレンタルサーバーにデプロイしたうえで表示をさせましたが、特にエラーが吐かれる様子もなく、画像の表示が行えません。
どなたか解決策が分かる方がいらっしゃいましたら、ご教授をお願いいたします。
回答1件
あなたの回答
tips
プレビュー