1
我有一个ajax功能,需要一个div,并将其制作成图片,然后将其发布到php上进行保存。在服务器上保存图片错误
<script>
$("#capture").click(function() {
html2canvas([document.getElementById('printableArea')], {
onrendered: function (canvas) {
var imagedata = canvas.toDataURL('image/png');
var imgdata = imagedata.replace(/^data:image\/(png|jpg);base64,/, "");
//ajax call to save image inside folder
$.ajax({
url: 'save_image.php',
data: {
imgdata:imgdata
},
type: 'post',
success: function (response) {
console.log(response);
$('#image_id img').attr('src', response);
}
});
}
})
});
</script>
然后,我有保存图像PHP将其保存在服务器上
<?php
$imagedata = base64_decode($_POST['imgdata']);
$filename = md5(uniqid(rand(), true));
//path where you want to upload image
$file = '/home/a7784524/public_html/barcodeimage/'.$filename.'.png';
$imageurl = 'http://panosmoustis.netai.net/barcodeimage/'.$filename.'.png';
file_put_contents($file,$imagedata);
echo $imageurl;
?>
我的问题是,虽然图像被保存在路当我尝试打开它,我得到错误的图像不能显示,因为它包含错误 谢谢
你确定你的正则表达式是“有效的”?你试过用''分割数据吗? (逗号不用于base64) –
你是什么意思正则表达式? – aristeidhsP
'/^data:image \ /(png | jpg); base64,/'是一个正则表达式 –