0
我正在嘗試將畫布數據保存爲png文件。我正在使用以下ajax代碼並繼續獲取保存的圖像,但圖像已損壞。也許我有錯誤的$ _REQUEST屬性?Ajax保存畫布圖像已損壞(解碼不正確?)
任何幫助表示讚賞。
var dataURL = canvas.toDataURL('image/png');
var imageURL;
$.ajax({
url: "saveimage.php",
type: 'POST',
dataType: 'json',
data: { 'data_url': dataURL },
complete: function(xhr, textStatus) {
// Request complete.
},
// Request was successful.
success: function(response, textStatus, xhr) {
console.log('Response: ', response);
// Conversion successful.
if (response.status_code === 200) {
imageURL = response.data.image_url;
// Paste the PNG image url into the input field.
document.querySelector('img').src = imageURL;
}
},
error: function(xhr, textStatus, errorThrown) {
// Some error occured.
console.log('Error: ', errorThrown);
}
});
}
<?php
file_put_contents('images/'. rand().'.png', base64_decode($_REQUEST['data_url']));
?>