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 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题