为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
温温酱 2022-01-14 16:49:11
如何使用干预图像和 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/对不起,我的英语不好。谢谢。
查看完整描述

2 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

我解决了!我像这样更改了控制器中的功能。


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');

}


查看完整回答
反对 回复 2022-01-14
?
三国纷争

TA贡献1804条经验 获得超7个赞

我知道您可能解决了您的疑问,但是我一直在寻找类似的东西但没有找到,因此我将以下代码留给可能需要它的人。此代码的目标是使用 URL 从 Pixabay上的图像。


我对类似问题的解决方案:


public function store(){       

    $url = json_decode(request('photo')); //Photo is the field that I fill in the view, that contain a URL to my image in pixabay;

    $contents = file_get_contents($url);

    $name = substr($url, strrpos($url, '/') + 1);

    $blob =  base64_encode($this->resizeImage($contents));

    Photos::firstOrCreate(['photo' => $name,'thumb' => $blob]); //Photos is my model

}      


private function resizeImage($contents) : string {

    return (string) Image::make($contents)->resize(75, 75)->encode('data-url');  // Very important this cast to String, without it you can not save correctly the binary in your DB;   

}

    

查看:为了在我的视图中使用代码:当我返回查看模型时,我使用“for”打印所有图像,如下所示:


@foreach($photos as $element)

    <img src="{{ base64_decode($element->thumb) }}" alt="" >

@endforeach

#Warning 非常重要,您的数据库中有一个 Blob 字段,因此在 Laravel Migrate 的 Schema 中,您必须输入以下内容: $table->binary('thumb');


你可以在我的 git 上查看我的代码:lucasfranson10/multisafepay


查看完整回答
反对 回复 2022-01-14
  • 2 回答
  • 0 关注
  • 188 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号