2017-09-30 6 views
-2

base64 문자열을 PNG 이미지로 변환하려고 시도했지만 서버를 열었을 때 서버에 저장 한 후 '치명적인 오류 읽기'오류가 표시됩니다. PNG 이미지 파일 : IDAT '의 압축 풀기 오류입니다. 여기서버에 저장 한 후 Base64 문자열을 이미지 파일로 변환

64 기수 데이터 : 여기

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

당신이 게시 한 Base64로 문자열이 불완전합니다. '[...]'이 유효하지 않습니다. – glennsl

+0

@glennsl base64 데이터 위에 게시했습니다. 웹 콘솔에서 복사 했으므로 [...]는 데이터를 반환하여 해당 데이터를 base64로 볼 때 계속 표시합니다. 이미지에서 완벽하게 작동합니다. –

+0

그렇다면 왜 그것을 포함시켜야하며, 그렇게 말하지 않을까요? 또한보십시오 [mcve] – glennsl

답변

1

+ 기호, Base64로 교체 후 발견 된 공간에 대체하여 문제를 해결

str_replace ("", "+", $ d ata);

다음은 업데이트 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)); 
관련 문제