将值 Base64 保存到 Image PNG 使用干预图像 Laravel 存储

如何使用干预图像和 laravel 存储将 Base64 值转换为 Image PNG?


public function userLogout(Request $request) {

  $data = $request->all();

  if ($request->isMethod('post')) {

    $poster = explode(";base64", $request->picture);

    $image_type = explode("image/", $poster[0]);

    $mime_type = '.'.$image_type[1];

    $image_base = base64_decode($poster[1]);


    $data['picture'] = Storage::disk('player-images')->put($image_base, file_get_contents($poster));


    $path = public_path('storage/player-images/'.$image_base);


    Image::make($poster)->save($path);


    $data['picture'] = 'player-images/' . $image_base;

    User::where('name', Auth::user()->name)->update($data);


  }

  return view('gallery');

}

我收到一条错误消息:


“file_get_contents() 期望参数 1 是有效路径,给定数组”


这是我的ajax函数


var canvas = document.getElementById('canvas');

var dataUrl = canvas.toDataURL('image/png');

$(document).ready(function(){

    $('#save').click(function(e){

        e.preventDefault();


        $.ajax({

          headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},

          type: "POST",

          url: "/gallery",

          data: {

            picture: dataUrl,

          }

        }).done(function(o) {

            console.log("saved");

        });

    });

});

如何将 base64 值保存到诸如 player-images/blabla.png 之类的数据库中,并将图像存储到路径 public/storage/player-images/


对不起,我的英语不好。谢谢。


温温酱
浏览 189回答 2
2回答

梵蒂冈之花

我解决了!我像这样更改了控制器中的功能。public function userLogout(Request $request){  $data = $request->all();  if ($request->isMethod('post')) {    if (preg_match('/^data:image\/(\w+);base64,/', $data['picture'])) {      $value = substr($data['picture'], strpos($data['picture'], ',') + 1);      $value = base64_decode($value);      $imageName = time()  . '.png';      $val = Storage::disk('player-images')->put($imageName, $value);      $path = public_path('storage/player-images/'.$imageName);      Image::make($data['picture'])->resize(304, 277)->save($path);      $data['picture'] = 'player-images/' . $imageName;      User::where('name', Auth::user()->name)->update(['picture' => $data['picture']]);    }  }  return view('gallery');}

三国纷争

我知道您可能解决了您的疑问,但是我一直在寻找类似的东西但没有找到,因此我将以下代码留给可能需要它的人。此代码的目标是使用 URL 从 Pixabay上的图像。我对类似问题的解决方案:public function store(){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; $url = json_decode(request('photo')); //Photo is the field that I fill in the view, that contain a URL to my image in pixabay;&nbsp; &nbsp; $contents = file_get_contents($url);&nbsp; &nbsp; $name = substr($url, strrpos($url, '/') + 1);&nbsp; &nbsp; $blob =&nbsp; base64_encode($this->resizeImage($contents));&nbsp; &nbsp; Photos::firstOrCreate(['photo' => $name,'thumb' => $blob]); //Photos is my model}&nbsp; &nbsp; &nbsp;&nbsp;private function resizeImage($contents) : string {&nbsp; &nbsp; return (string) Image::make($contents)->resize(75, 75)->encode('data-url');&nbsp; // Very important this cast to String, without it you can not save correctly the binary in your DB;&nbsp; &nbsp;}&nbsp; &nbsp;&nbsp;查看:为了在我的视图中使用代码:当我返回查看模型时,我使用“for”打印所有图像,如下所示:@foreach($photos as $element)&nbsp; &nbsp; <img src="{{ base64_decode($element->thumb) }}" alt="" >@endforeach#Warning 非常重要,您的数据库中有一个 Blob 字段,因此在 Laravel Migrate 的 Schema 中,您必须输入以下内容: $table->binary('thumb');你可以在我的 git 上查看我的代码:lucasfranson10/multisafepay
打开App,查看更多内容
随时随地看视频慕课网APP