1
Phonegap告訴我成功,Laravel告訴我成功,但它總是白色。Phonegap到Laravel圖像上傳是空白/白
PhoneGap的(機器人)JS的兩個相關的代碼塊是:
function sendImage(src) {
window.localStorage.setItem('csrf', csrf);
src = (src == 'library') ? Camera.PictureSourceType.PHOTOLIBRARY : Camera.PictureSourceType.CAMERA;
navigator.camera.getPicture(success, fail, {quality: 45, sourceType: src});
function success(imageData) {
imageData = getBase64Image(imageData);
userId = window.localStorage.getItem("user_id");
var url = 'https://example.com/profile/update/picture';
var params = {photo: 'image/png;space,' + imageData, user_id: userId};
//csrf = window.localStorage.getItem("csrf");
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: params,
headers: {"x-csrf-token": 'notoken'},
async: false,
success: function(res)
{
if(res.success)
{
alert("Success!");
}
},
complete: function(res)
{
},
error: function(res)
{
alert("Error = " + JSON.stringify(res));
}
});
}
function fail(error){
alert('You are FAIL');
}
function getBase64Image(img) {
// Create an empty canvas element
var canvas = document.createElement("canvas");
// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
//use this block to not draw image unless image is loaded
var callback = function(image) {
if(!image) image = this;
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(image, 0, 0);
}
//check if image is loaded
if(img.complete) {
callback(img);
}else {
img.onload = callback;
}
//get the data-URL formatted image
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
}
在Laravel照片被更新爲空白或爲空。有一次,我在那裏有這個代碼塊被返回true,所以Laravel至少認爲該圖像是合法的:如果你獲得空白或透明的圖像,這意味着所謂的繪圖函數圖像完全上傳之前
public static function check_base64_image($base64) {
$img = imagecreatefromstring(base64_decode($base64));
if (!$img) {
return false;
}
imagepng($img, 'tmp.png');
$info = getimagesize('tmp.png');
unlink('tmp.png');
if ($info[0] > 0 && $info[1] > 0 && $info['mime']) {
return true;
}
return false;
}