laravel实现上传图片并在页面显示的例子

网络编程 2021-07-04 23:05www.168986.cn编程入门
今天长沙网络推广就为大家分享一篇laravel实现上传图片并在页面显示的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随长沙网络推广过来看看吧

1:上传图片

public function updateFeedbackImg(Request $request)
  {
    $bool = false;
    $upload_file = $request->file("pic");
    if ($upload_file->isValid()) {
      $realPath = $upload_file->getRealPath();
      $bool = Storage::disk('feedback')->put($request->get('id') . '.png', file_get_contents($realPath));
    }
    if ($bool == true) {
      $pany = CompanyState::find($request->get('id'));
      $pany->picpath_ = $request->get('id') . '.png';
      $pany->save();
      return '{"statusCode":"200", "message":"上传成功", "navTabId":"uploadFeedbackImg", "forwardUrl":"evaluation/queryCompanyFeedback/' . session('plan_id') . '",
"callbackType":"forward"}';
    } else {
      return '{"statusCode":"300", "message":"上传失败","callbackType":"closeCurrent"}';
    }
  }

2:html

 <img src="{{ url('evaluation/showImage/'.$pany->picpath_) }}"
     οnclick="this.width+=500;this.height+=500; javascript:window.open(this.src);"
     style="cursor:pointer; width: 500px; height: 800px;border:1px solid #000000"
     name="phoath"/>

3:设置对应的路由

Route::group(['prefix' => 'evaluation'], function () {
  //查看图片
Route::get('/lookthrough/{pany_id}', 'EvaluationController@lookthrough');
//放大图片
Route::get('/showImage/{src}', 'EvaluationController@showImage');
});

4显示图片

 public function lookthrough($pany_id)
  {
    $pany = CompanyState::getRecordById($pany_id);
    return view('panels.EvaluationManagement.FeedbackInfo.FeedbackImg', ['pany' => $pany[0]]);
  }
 public function showImage($src)
  {
    $path = storage_path() . '/feedback/' . $src;  //获取图片位置的方法
    return response()->file($path);
  }

以上这篇laravel上传图片显示就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by