2016-03-28 12 views
1

PhoneGapは成功を告げていますが、Laravelは成功を語っていますが、常に白です。PhoneGapからLaravelへの画像アップロードは空白/白です

のPhoneGap(アンドロイド)JSの2つの関連コードブロックは、次のとおり

Laravelで
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,/, ""); 
} 

}

写真がブランクまたは空にアップデートされます。あなたがイメージが完全にアップロードする前に、描画関数が呼び出さ意味空白または透明な画像を得る場合

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

答えて

0

:ある時点で私は、少なくともそのイメージが合法であると考えてLaravelので、trueを返したが、このコードブロックを持っています。 Javascriptは非同期で動作します。以下のコードでは、callback()関数が実行される前に2行が実行されています。

var dataURL = canvas.toDataURL("image/png"); 
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");