duanjiwang2927
duanjiwang2927
2016-10-29 08:08
浏览 109
已采纳

使用cordova将base64字符串保存为php中的图像

I am trying to save the captured image in my server as jpg/png,I decoded the string as base64 and uploaded it but when i open the image it says 'the file cant open'. I don't understand why this is happening or i just missed any code.

jquery

navigator.camera.getPicture(onSuccess, onFail, { quality: 100,destinationType: Camera.DestinationType.DATA_URL });

function onSuccess(imageURI) {
 var image = document.getElementById('dr_preview');
 image.src = imageURI;
 snapSrc = imageURI;
}
var img = document.getElementById('dr_preview');
var src = $('#dr_preview').attr('src');
console.log(src);
var dataparam = 'type=3&dr='+dr_value+'&image='+src;
$.ajax({
        type: 'POST',
        dataType: 'json',
        async: true,
        url: 'http://localhost/v_2/dr-getDrDetails.php',
        data: dataparam,
        cache: true,
        success: function(data)
        {
        }
 });

php

$data = $_POST['image'];
$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data));
$filePath = $_SERVER['DOCUMENT_ROOT']. '/v_2/scanedDR/'.$dr.'.PNG';
$file = fopen($filePath, 'w');
fwrite($file, $data);
fclose($file);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujia3441
    doujia3441 2016-10-29 09:22
    已采纳

    You can use, this function that convert base64 to image.

    function base64_to_jpeg($base64_string, $output_file) {
    $ifp = fopen($output_file, "wb"); 
    
    $data = explode(',', $base64_string);
    
    fwrite($ifp, base64_decode($data[1])); 
    fclose($ifp); 
    
    return $output_file; 
    }
    
    点赞 评论

相关推荐