2016-06-29 24 views
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; 

?> 

我的问题是,虽然图像被保存在路当我尝试打开它,我得到错误的图像不能显示,因为它包含错误 谢谢

+0

你确定你的正则表达式是“有效的”?你试过用''分割数据吗? (逗号不用于base64) –

+0

你是什么意思正则表达式? – aristeidhsP

+0

'/^data:image \ /(png | jpg); base64,/'是一个正则表达式 –

回答

0

@aristeidhsP请检查file_put_contents返回;是真正创造的形象? 如果是这样,你必须检查$ imagedata的内容:你是否正确地从图像数据中剥离了额外的东西(你的正则表达式可能无法在jpeg或gif扩展上触发)。 希望这有助于