【问题标题】:Laravel return image preview from base64Laravel 从 base64 返回图像预览
【发布时间】:2018-01-29 07:37:11
【问题描述】:

我有 base64 图像,我想返回图像预览而不是 base64 代码。

我试过了

return response(base64_decode($results->getBase64Image()), 200, ['Content-Type' => 'image/png']);

但它返回一个奇怪的输出。

【问题讨论】:

  • 返回是什么意思??你想回到哪里??
  • 我试过但结果相同@linktoahref
  • 我要返回图片预览@Sohel0415
  • 先保存文件,再从磁盘显示

标签: image laravel base64


【解决方案1】:

Laravel 5.8 及以上版本...

直接将字符串放入response()... 并添加header('Content-Type', 'image/png')

例如:

$raw_image_string = base64_decode($base64_img_string);

return response($raw_image_string)->header('Content-Type', 'image/png');

【讨论】:

    【解决方案2】:

    我刚刚找到了答案。 imagecreatefromjpeg() php

    $image = imagecreatefromstring(base64_decode($results->getBase64Image()));
            header('Content-type: image/png');
            return imagejpeg($image);
    

    【讨论】:

      【解决方案3】:

      您可以将base64 代码返回到视图,并在视图中使用<img> 标签呈现图像

      $base64code = $results->getBase64Image();
      return view('preview', compact('base64code'));
      

      刀片文件:

      <img src="data:image/png;base64, {{ $base64code }}" alt="Image Preview" />
      

      【讨论】:

      • 很抱歉,我想将图像渲染为返回而不是从视图/刀片返回。
      • 你的意思是让最终用户下载图像预览?
      • 只是图像的预览,就像你在地址栏输入base64一样。
      【解决方案4】:

      我已经用这个.. 为 base 64 图像))))

      function store()
      {
          $data = $this->request->img;
          list($type, $data) = explode(';', $data);
          list(, $data) = explode(',', $data);
      
          $directory_bucekts = implode('/',str_split(strtolower(str_random(2))));
      
          Storage::disk('public')->makeDirectory('images/shopdata/'.$directory_bucekts, true, true);
      
      //        mkdir(storage_path('app/public/images/shopdata/'.$directory_bucekts), 755, true);
      
          $data         = base64_decode($data);
      
          $fileName     = time().rand(100,1000).'.jpg';
          $path         = storage_path('app/public/images/shopdata/'.$directory_bucekts);
      
          file_put_contents($path .'/'. $fileName, $data);
      
          return response()->make(['path' =>  '/storage/images/shopdata/' . $directory_bucekts. '/' .$fileName]);
      
      }
      

      【讨论】:

      • 您的示例中的基础,是否可以不存储图像?
      • 你必须先保存图片,然后在预览中获取图片
      • 哦..我明白了..我会试试的。
      【解决方案5】:

      试试这个

      class ImagesController extends Controller{
      
        public function store(Request $request, YourImageModel $imageModel){
          
          $image = base64_decode($imageModel->base64Content);
          $path = Storage::putFile('images', $image);
          
          return response()->file($path);
      
        }
      
      }
      

      你可以阅读 Request File uploadingFile UploadingFile Responses 的文档,它们都在 Laravel 的文档中。

      祝你有美好的一天:)

      【讨论】:

        【解决方案6】:

        试试这个:

                $data = base64_decode($results->getBase64Image());
                $image_name= time().'_test_.png';
                $path = public_path() .'/'. $image_name;
        
                file_put_contents($path, $data);
        
                //serve the image
               return  response()->file($path);
        

        Docs

        【讨论】:

        • 谢谢,但它只是将图像保存到公共文件夹,有没有办法创建图像预览?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-05
        • 2017-06-13
        • 2011-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多