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

使用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 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; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考