【问题标题】:Phonegap to Laravel image upload is blank/whitePhonegap 到 Laravel 图片上传是空白/白色
【发布时间】:2016-07-16 03:08:00
【问题描述】:

Phonegap 告诉我成功,Laravel 告诉我成功但它总是白色的。

Phonegap(Android)两个相关的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;
}

【问题讨论】:

    标签: php android image cordova


    【解决方案1】:

    如果您获得空白或透明图像,这意味着您的绘图函数在图像完全上传之前调用。 Javascript 异步工作。在您的回调()函数执行之前执行的以下两行代码中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 2012-02-06
      相关资源
      最近更新 更多