您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

问题是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; 
}
其他 2022/1/1 18:17:15 有536人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶