복붙노트

Base64 문자열을 이미지 파일로 변환 하시겠습니까?

PHP

Base64 문자열을 이미지 파일로 변환 하시겠습니까?

내 base64 이미지 문자열을 이미지 파일로 변환하려고합니다. 이것은 내 Base64 문자열입니다.

http://pastebin.com/ENkTrGNG

다음 코드를 사용하여 이미지 파일로 변환 :

function base64_to_jpeg( $base64_string, $output_file ) {
    $ifp = fopen( $output_file, "wb" ); 
    fwrite( $ifp, base64_decode( $base64_string) ); 
    fclose( $ifp ); 
    return( $output_file ); 
}

$image = base64_to_jpeg( $my_base64_string, 'tmp.jpg' );

그러나 잘못된 이미지 오류가 발생합니다. 여기서 뭐가 잘못 되었나요?

해결법

  1. ==============================

    1.문제는 데이터 : 이미지 / png; base64가 인코딩 된 콘텐츠에 포함된다는 것입니다. 이렇게하면 base64 함수가 이미지 데이터를 디코딩 할 때 잘못된 이미지 데이터가 생성됩니다. 문자열을 디코딩하기 전에 함수에서 해당 데이터를 제거하십시오.

    문제는 데이터 : 이미지 / png; base64가 인코딩 된 콘텐츠에 포함된다는 것입니다. 이렇게하면 base64 함수가 이미지 데이터를 디코딩 할 때 잘못된 이미지 데이터가 생성됩니다. 문자열을 디코딩하기 전에 함수에서 해당 데이터를 제거하십시오.

    function base64_to_jpeg($base64_string, $output_file) {
        // open the output file for writing
        $ifp = fopen( $output_file, 'wb' ); 
    
        // split the string on commas
        // $data[ 0 ] == "data:image/png;base64"
        // $data[ 1 ] == <actual base64 string>
        $data = explode( ',', $base64_string );
    
        // we could add validation here with ensuring count( $data ) > 1
        fwrite( $ifp, base64_decode( $data[ 1 ] ) );
    
        // clean up the file resource
        fclose( $ifp ); 
    
        return $output_file; 
    }
    
  2. ==============================

    2.이미지 데이터의 시작 부분에있는 data : image / png; base64 부분을 제거해야합니다. 실제 base64 데이터가 그 다음에옵니다.

    이미지 데이터의 시작 부분에있는 data : image / png; base64 부분을 제거해야합니다. 실제 base64 데이터가 그 다음에옵니다.

    Base64를 포함하여 모든 것을 제거하고 (데이터에서 base64_decode ()를 호출하기 전에) 괜찮을 것입니다.

  3. ==============================

    3.아마 이것처럼

    아마 이것처럼

    function save_base64_image($base64_image_string, $output_file_without_extension, $path_with_end_slash="" ) {
        //usage:  if( substr( $img_src, 0, 5 ) === "data:" ) {  $filename=save_base64_image($base64_image_string, $output_file_without_extentnion, getcwd() . "/application/assets/pins/$user_id/"); }      
        //
        //data is like:    data:image/png;base64,asdfasdfasdf
        $splited = explode(',', substr( $base64_image_string , 5 ) , 2);
        $mime=$splited[0];
        $data=$splited[1];
    
        $mime_split_without_base64=explode(';', $mime,2);
        $mime_split=explode('/', $mime_split_without_base64[0],2);
        if(count($mime_split)==2)
        {
            $extension=$mime_split[1];
            if($extension=='jpeg')$extension='jpg';
            //if($extension=='javascript')$extension='js';
            //if($extension=='text')$extension='txt';
            $output_file_with_extension=$output_file_without_extension.'.'.$extension;
        }
        file_put_contents( $path_with_end_slash . $output_file_with_extension, base64_decode($data) );
        return $output_file_with_extension;
    }
    
  4. ==============================

    4.

    if($_SERVER['REQUEST_METHOD']=='POST'){
    $image_no="5";//or Anything You Need
    $image = $_POST['image'];
    $path = "uploads/$image_no.png";
    
    file_put_contents($path,base64_decode($image));
    echo "Successfully Uploaded";
    }
    
  5. ==============================

    5.내가 사용하는 쉬운 방법 :

    내가 사용하는 쉬운 방법 :

    file_put_contents($output_file, file_get_contents($base64_string));
    
  6. ==============================

    6.

    $datetime = date("Y-m-d h:i:s");
    $timestamp = strtotime($datetime);
    $image = $_POST['image'];
    $imgdata = base64_decode($image);
    $f = finfo_open();
    $mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);
    $temp=explode('/',$mime_type);
    $path = "uploads/$timestamp.$temp[1]";
    file_put_contents($path,base64_decode($image));
    echo "Successfully Uploaded->>> $timestamp.$temp[1]";
    
  7. from https://stackoverflow.com/questions/15153776/convert-base64-string-to-an-image-file by cc-by-sa and MIT license