服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - laravel实现上传图片并在页面显示的例子

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

2021-09-02 16:41上官hao PHP教程

今天小编就为大家分享一篇laravel实现上传图片并在页面显示的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

1:上传图片

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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) {
      $company = CompanyState::find($request->get('id'));
      $company->picpath_ = $request->get('id') . '.png';
      $company->save();
      return '{"statusCode":"200", "message":"上传成功", "navTabId":"uploadFeedbackImg", "forwardUrl":"evaluation/queryCompanyFeedback/' . session('plan_id') . '",
"callbackType":"forward"}';
    } else {
      return '{"statusCode":"300", "message":"上传失败","callbackType":"closeCurrent"}';
    }
  }

2:html

?
1
2
3
4
<img src="{{ url('evaluation/showImage/'.$company->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="photopath"/>

3:设置对应的路由

?
1
2
3
4
5
6
Route::group(['prefix' => 'evaluation'], function () {
  //查看图片
Route::get('/lookthrough/{company_id}', 'EvaluationController@lookthrough');
//放大图片
Route::get('/showImage/{src}', 'EvaluationController@showImage');
});

4:显示图片

?
1
2
3
4
5
6
7
8
9
10
public function lookthrough($company_id)
 {
   $company = CompanyState::getRecordById($company_id);
   return view('panels.EvaluationManagement.FeedbackInfo.FeedbackImg', ['company' => $company[0]]);
 }
public function showImage($src)
 {
   $path = storage_path() . '/feedback/' . $src//获取图片位置的方法
   return response()->file($path);
 }

以上这篇laravel上传图片显示就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_29099209/article/details/80066273

延伸 · 阅读

精彩推荐