2016-04-22 30 views
1

我試圖將base64圖像上傳到服務器。 我已經嘗試了很多答案,但我總是得到相同的錯誤。Laravel 4 - 反序列化():在保存base64圖像時偏移時出錯

error

鏈接,我試圖

How to save a PNG image server-side, from a base64 data string

https://www.reddit.com/r/laravel/comments/39wclp/how_to_convert_an_base64_image_string_to_an_image/

Convert Base64 string to an image file?

下面這段代碼應該工作。

$image = base64_decode(Input::get('image_cropped')); 
$image_name= 'file_name.png'; 
$path = public_path() . "/images/" . $image_name; 
file_put_contents($path, $image); 
+0

向我們展示失敗的base64編碼PNG圖像的示例。 'unserialize()'也不解碼base64編碼的字符串。 –

+0

base64 png圖片非常大。 –

+1

此錯誤與您的圖像創建無關。你寫會議商店的東西。它的laravel框架會話存儲處理程序相關。 – di3

回答

0

如果您檢查從請求中發送的圖像編碼開始與data:image/png;base64形式,所以儘量用:

$img = explode(',', Input::get('image_cropped')); 
$img_name = 'file_name.png'; 
file_put_contents(public_path().'/images/'.$img_name, base64_decode($img[1])); 

這爲我工作。