laravel 8.0にてサーバー上に保存した画像データをブラウザ上に表示したいと考えております。
条件として以下の2点があります。
・responce()-fileを使用して画像を表示
・公開publicにデータを保存しない
・シンボリックリンクは使用しない
現状としてエラーにならず、動きはするものの、画像が表示しない状態です。
デベロッパーツールの<img>タグの中身の状態を記述致します。
そもそも・response()-fileの挙動を理解しきれていない為、それによるエラー
の可能性もある為、可能でしたら挙動の部分も含めてご教授頂けたら幸いです。
http://localhost/HTTP/1.0 200 OK Cache-Control: public Content-Type: image/jpg Date: Sun, 19 Dec 2021 00:50:35 GMT Last-Modified: Sun, 19 Dec 2021 00:50:35 GMT
以下メイン処理
■コントローラー
```larabel
//配列展開
$fileName = [];
$randName = [];
$fileimage = [];
$productImagePath = [];
//確認画面バリデーションクリアしたら画像表示の為一度ストレージパスの取得 for ($i = 1; $i <= 10; $i++) { $fileimage[$i] = $input['file' . $i] ?? $input['name' . $i] ?? ''; if (isset($input['file' . $i])) { $fileName[$i] = $fileimage[$i]->getClientOriginalName(); //同じ画像名対策の為、乱数を生成し、画像名とマージする $min = 1; $max = 1000000000000; $ransu = mt_rand($min, $max); $randName[$i] = $ransu . $fileName[$i]; $fileimage[$i]->storeAs('gazo\hoga', $randName[$i], 'public'); $productImagePath[$i] = ('public\gazo\hoga\' . $randName[$i]); $header = [ 'Content-type' => 'image/jpg' ]; $productImagePath[$i] = img_rsp($productImagePath[$i], $header); } elseif (isset($input['productImagePath' . $i])) { $fileName[$i] = $input['fileName' . $i]; $productImagePath[$i] = $input['productImagePath' . $i]; } else { $fileName[$i] = ''; $productImagePath[$i] = ''; } } return view( 'hoge.kakunin', [ 'input' => $input, 'color' => $color, 'size' => $size, 'item' => $item, 'productImagePath' => $productImagePath, 'fileName' => $fileName, ] );
■ブレード(画像アップロード) ```laravel @for($i=1; $i<=10; $i++) <td class="row1"> <label class="file a"> <div class="file">ファイルを選択</div><input type="file" id="file{{$i}}" name="file{{$i}}" accept="image/*" onChange="imgPreView(event, 'preview{{$i}}','file{{$i}}' ,'name{{$i}}','vali{{$i}}')"> <a id="name{{$i}}" class="name">{{$input['fileName'.$i]??''}}</a> <input type="text" id="vali{{$i}}" class="vali" name="test{{$i}}" value="{{$input['fileName'.$i]??''}}"> </label> <input type="button" class="width" value="削除" onclick="clickBtn1('preview{{$i}}','file{{$i}}','name{{$i}}','vali{{$i}}','image{{$i}}','itemname{{$i}}')"> <div id="preview{{$i}}"> <input type="hidden" id='itemname{{$i}}' name="fileName{{$i}}" value="{{$fileName[$i]??$input['fileName'.$i]??''}}"> <input type="hidden" id="image{{$i}}" name="productImagePath{{$i}}" value="{{$productImagePath[$i]??$input['productImagePath'.$i]??''}}"> @if (isset($input['productImagePath'.$i])&&$input['productImagePath'.$i]!='') <img id="previewImage-preview{{$i}}" src="{{url($input['productImagePath'.$i])}}" width="180" height="200"> @endif </div> </td> @endfor
■ブレード(画像確認)
laravel
1 @for($i=1; $i<=10; $i++) <td class="row1"> 2 <div class="img_name">{{$fileName[$i]??$input['fileName'.$i]??''}}</div> 3 <div id="preview{{$i}}"> 4 @if (isset($productImagePath[$i])&&$productImagePath[$i]!='') 5 <img src="{{url($productImagePath[$i])}}" width="170" height="200"> 6 @elseif (isset($input['productImagePath'.$i])&&$input['productImagePath'.$i]!='') 7 <img src="{{url($input['productImagePath'.$i])}}" width="170" height="200"> 8 @endif 9 </div> 10 </td> 11 @endfor 12 13 14
larabel
1■関数 2function img_rsp($img_file, $headr) 3{ 4 5 return response()->file(Storage::path($img_file), $headr); 6}
あなたの回答
tips
プレビュー