【问题标题】:Save value Base64 to Image PNG use Intervention image Laravel Storage将值 Base64 保存到 Image PNG 使用干预图像 Laravel 存储
【发布时间】:2020-01-19 00:24:32
【问题描述】:

如何使用干预图像和 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/

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

【问题讨论】:

  • 将base64()数据存入数据库,并将文件保存到文件夹,是不是多余?
  • 我将数据类型更改为文本。但我希望它存储在数据库中,而不是 base64,而是链接存储,如 player-images/(value).png

标签: php laravel intervention


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我知道您可能解决了您的疑问,但我正在寻找类似的东西但我没有找到它,所以我将以下代码留给可能需要它的人。 此代码的目标是使用 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
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      相关资源
      最近更新 更多