将Base64字符串转换为图像文件后,保存到服务器后返回无效图像

时间:2017-09-30 15:17:56

标签: php html5-canvas

我尝试将base64字符串转换为png图像,在我打开它后保存到服务器后,它显示错误,其中显示“读取PNG图像文件时发生致命错误:IDAT中的解压缩错误”。 / p>

此处 base64数据

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAgAElEQVR4nNy9aZMcx5EtCvRSVblnxh6Rey29YCVAguAOihQ5FEWRkijNkNSMlhk9XY3dazPP7txn9uz9+fM+eERWdrI532AwQ5mldaOqugF0x0n34+e4+53T8xMsr7PVKc5Wpzg5uzt9Pn/u9PwEJ2d3p9fDc+E9t32/5fc+X5/des1fOz0/ufHaJl5jtTmf/rzanPvnTrGJ1zhbnSLJYkTJBpt4jTiNsInXWKcrrJJzRMkKcbpGlGwQJRvERYK0ShAXEaJ8g3W6wiZbIyljxGmENI+wYTGSkq60ypBWGYoqRcZSFCpDxgtkIkMq1ih0gkxkyMsEJd+AyRi5zJDzDBnLUcgKTFdgukTOUxQyQaZzcFNBWAZuGCpVQDiGQpQobAmmS3BToTQZKlUiFwmEy2FajarmUDaHqzmkKiE6hcJWUIajMCWEZihlgVLFyHQOoUsUKkcuM2QiRakLZCJFyhMkLEbK6f+V8hS5zJHyFHERTVdeJojyeLroZ0zXndf2cXIHJ2d3cff0Du6e3pkOaDic8wMbDnp47jaA3Dm5+T3mVzjU4bWz1emPDnz4uNqc4+Ts7vTcEhzraDU9v47OcB6fTSCg51Y3rijZ+M/PkGQbJNkGaR4hK2JkBQEgyjfYZGtsMv+L98DJGB0YOugpcpaiEDkqVSKtPCh4ilLmyBh9ZP7KeUpfxzOUugTTFUpdojIVSlmgEBkqVaBSBZimw81liVLmqEwJ7Ti04/QeUyLjMXTLoWoBZThsI2EbCdNIyEaCOw7hmAdchUoV4LZAVTNUgoCRiQy5ZqhUhrSKkbEEOU+RsARJldBHf8VFNN1Ekmwz/UySMp1uIq81QEIkmF/zQ73anOPs7HjA56+dnN2dnrt7eudHd/4AqPC++WtzEMyBOAfNPIIEQISP4evjNKJDHZ9jHZ1hE9[…]

这里是 php代码

$f = $handler->sanitizer($_POST['file']);
$file = $handler->sanitizer($_POST['files']);
$desc = $handler->sanitizer($_POST['desc']);
$d = $handler->sanitizer($_POST['tm']);
$status = 'Sorry something went wrong please try again';
$file_data = '';
$data = $file;

if(!empty($f) && !empty($file)){
    /**
     * file to be saved
     */
    $dir = "../users/$un/profiles/";

    $file = explode(";base64,", $file);
    $type_aux = explode("image/", $file[0]);
    $type = $type_aux[1];
    $filename =  "fr".substr(sha1(microtime()),0,20).'.'.$type;


    $new_file = $dir.$filename;

    /**
     * saving and decoding file
     */
    $upload = file_put_contents($new_file,
        base64_decode(preg_replace("#^data:image/\w+;base64,#i", '', $data)));

    if($upload){
        chmod($new_file,0777);
        $status = 'changed';
        $file_data = str_replace('..','',$filename);

        $f = explode('/',$f);
        $f = end($f);
        $path = "../users/$un/profiles/$f";
        $handler->Delete($path);
    }

}


echo json_encode(array('status'=>$status,'file'=>$file_data));

1 个答案:

答案 0 :(得分:1)

我通过用 + sign

替换替换base64后找到的任何空格来解决问题

str_replace(“”,“+”,$ data);

这是更新php代码

$f = $handler->sanitizer($_POST['file']);
$file = $handler->sanitizer($_POST['files']);
$desc = $handler->sanitizer($_POST['desc']);
$d = $handler->sanitizer($_POST['tm']);
$status = 'Sorry something went wrong please try again';
$file_data = '';
$data = $file;

if(!empty($f) && !empty($file)){
    /**
     * file to be saved
     */
    $dir = "../users/$un/profiles/";

    $file = explode(";base64,", $file);
    $type_aux = explode("image/", $file[0]);
    $type = $type_aux[1];
    $filename =  "fr".substr(sha1(microtime()),0,20).'.'.$type;


    $new_file = $dir.$filename;
    $data = preg_replace("#^data:image/\w+;base64,#i", '', $data);

    /**
     * Replace the space found in the data with + sign to work
     */

    $data = str_replace(' ','+',$data);


    /**
     * saving and decoding file
     */
    $upload = file_put_contents($new_file,
        base64_decode($data));

    if($upload){
        chmod($new_file,0777);
        $status = 'changed';
        $file_data = str_replace('..','',$filename);

        $f = explode('/',$f);
        $f = end($f);
        $path = "../users/$un/profiles/$f";
        $handler->Delete($path);
    }

}


echo json_encode(array('status'=>$status,'file'=>$file_data));