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 综合布线实例设计,就好看好看不恐怖可好滤镜好聚
  • ¥15 使用moviepy库视频合并时出错
  • ¥30 FLUENT液固传质UDF
  • ¥15 怎么看梯度直方图以,怎么判断梯度消失/爆炸,怎么解决
  • ¥15 aspnetdll文件访问拒绝
  • ¥15 wpf中在模版中寻找元素
  • ¥15 MFC平台生成指定圆
  • ¥15 jmeter出现403
  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关