2013-03-01 94 views
104

我想将我的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'); 

但我得到的invalid image错误,什么是错在这里吗?

+0

的[如何保存为PNG图像服务器端,从一个base64数据串]可能的复制(https://stackoverflow.com/questions/ 11511511/how-to-save-a-png-image-server-side-from-base64-data-string) – nyedidikeke 2017-06-27 10:56:24

回答

187

问题是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; 
} 
+16

我有非常智能的解决方案 $ filename_path = md5(time().uniqid())。“ .JPG“; $ decoded = base64_decode($ base64_string_img); file_put_contents(“uploads /".$ filename_path,$ encoded); – 2015-02-06 12:09:11

+0

我刚刚没有任何前缀左右原始base64数据左右。因此我不得不将$ data [1]更改为$ data [0]。 – rcpfuchs 2016-04-27 06:05:41

+1

@rcpfuchs如果你刚刚使用base64,那么需要使用$ data,直接使用它就像写在问题 – Anant 2017-02-02 16:35:23

38

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

就剥夺一切直至并包括base64,(调用数据base64_decode())之前,你会被罚款。

6

也许这样

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; 
} 
1
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"; 
} 
+3

中一样欢迎使用Stack Overflow!感谢您的代码片段,它可能会提供一些有限的即时帮助。通过展示*为什么*这是一个很好的解决方案,并且使它对未来的读者更有用,一个正确的解释[将大大提高](// meta.stackexchange.com/q/114762)其长期价值其他类似的问题。请[编辑]你的答案以添加一些解释,包括你所做的假设。 – 2017-10-12 13:13:07

+0

这不是一个答案,甚至与原始问题有关。 – 2018-01-13 20:08:08

相关问题