第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將值 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() 期望參數(shù) 1 是有效路徑,給定數(shù)組”這是我的ajax函數(shù)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 之類的數(shù)據(jù)庫中,并將圖像存儲到路徑 public/storage/player-images/對不起,我的英語不好。謝謝。
查看完整描述

2 回答

?
梵蒂岡之花

TA貢獻1900條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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 非常重要,您的數(shù)據(jù)庫中有一個 Blob 字段,因此在 Laravel Migrate 的 Schema 中,您必須輸入以下內(nèi)容: $table->binary('thumb');


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


查看完整回答
反對 回復 2022-01-14
  • 2 回答
  • 0 關注
  • 189 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號