将Base64字符串转换为图像文件?

Bad*_*dal 132 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' );
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误invalid image,这里有什么不对吗?

Aus*_*rst 247

问题是data:image/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; 
}
Run Code Online (Sandbox Code Playgroud)

  • 我有非常聪明的解决方案$ filename_path = md5(time().uniqid()).".jpg"; $解码= BASE64_DECODE($ base64_string_img); file_put_contents("上传/".$ filename_path,$解码); (24认同)
  • @DanieleTesta,你必须提供一些基准来量化更高效:) (2认同)

aaa*_*789 41

您需要删除data:image/png;base64,图像数据开头所说的部分.之后是实际的base64数据.

只需删除所有内容base64,(包括调用base64_decode()数据之前),你就可以了.


小智 17

我正在使用的简单方法:

file_put_contents($output_file, file_get_contents($base64_string));
Run Code Online (Sandbox Code Playgroud)

这很有效,因为file_get_contents可以从URI读取数据,包括data:// URI.


Shi*_*kin 13

也许是这样的

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这很有魅力!非常感谢!正是我要找的。只是稍微改变了一下以符合我的特定要求,并让一切正常工作。 (2认同)

小智 6

这是一个旧线程,但如果您想上传具有相同扩展名的图像-

    $image = $request->image;
    $imageInfo = explode(";base64,", $image);
    $imgExt = str_replace('data:image/', '', $imageInfo[0]);      
    $image = str_replace(' ', '+', $imageInfo[1]);
    $imageName = "post-".time().".".$imgExt;
    Storage::disk('public_feeds')->put($imageName, base64_decode($image));
Run Code Online (Sandbox Code Playgroud)

您可以在 laravel 的 filesystem.php- 中创建“public_feeds”

   'public_feeds' => [
        'driver' => 'local',
        'root'   => public_path() . '/uploads/feeds',
    ],
Run Code Online (Sandbox Code Playgroud)