如何使用base64解码编码图像字符串

时间:2016-06-11 09:02:04

标签: php

我正在尝试此代码,但没有将img作为输出。请帮助我或建议任何其他方式。显示图像。

<?php 
function base64_to_jpeg($base64_string, $output_file) {


    $data = explode(',', $base64_string);

print_r($data[1]);
    return $output_file; 
}

 $my_base64_string = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMYAAAB4CAIAAAAmORoPAAANN0lEQVR4nO3ae0xT5xsH8EOBFjpEWqoirFy01bWDGUpYKK5DN/ACgrhLCHSYEQdxMZmCM5tdZCSyuQWdji1ZWciEUOYuYRuMsbiigGyIKwxhRuy4lMvGFJFByzqlt/1xfnlzdk5ba/v+/ns+f/U8fc7znnP8Sk8P+DkcDuLB7d+/n3yhUqkIgnj77beVSqVCoSAIoru7u7GxEXW6qpPUajVZZ84hnThxgiCIo0ePNjY2dnd3ozrqR3PQW1NTU8w6moPa0LrR0dEEQaD5CoVCqVQy56Djj4mJQXOY14G57uTkpNPrQJuD1iWRx8Ocg5D9tDrzerq/PtQ5tOtMO54H4PAK2r2vr6+vr48giJqaGvKtmpoa6nxXdbQ6WWfOISUlJSUlJTkcjpKSEuqOqJ95Fk7raA6C1iU30fySkhKnc9DxU+fc93iSkpJcXQfaHLQu9XiYcxCyn1ZnXk/314c6h3adacfjORbznxkAX0CkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgB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K2tZT66pBGJRM8995z7Hj8/P/LjBn3oREZGUhtef/31LVu2dHV1kb8Z5XA4ycnJarWaFhGCIKRS6YoVK8jXtI8wVEc+++wztVrd3NxsNpvlcnlTU9PDDz/MPDy5XN7f33/q1KnW1tbw8PD6+nrmr28FAgFajrYueVdElZGR0dPTc/r06ZaWlsjIyObmZqc3jmw2+8KFC++//357e7vVan3++eePHDnCvJVOTk622Wzk65SUFOpbLNZ/7nO4XG53d/fJkye7urpYLFZRUVFZWZmPeSLgj1sAdvBXnQAziBTADCIFMPsXfuHh4eJXJyUAAAAASUVORK5CYII=";
  $image = base64_to_jpeg( $my_base64_string, 'tmp.jpg' );                 
?>

<img src="tmp.jpg">

2 个答案:

答案 0 :(得分:1)

<?php 

 $my_base64_string = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMYAAAB4CAIAAAAmORoPAAANN0lEQVR4nO3ae0xT5xsH8EOBFjpEWqoirFy01bWDGUpYKK5DN/ACgrhLCHSYEQdxMZmCM5tdZCSyuQWdji1ZWciEUOYuYRuMsbiigGyIKwxhRuy4lMvGFJFByzqlt/1xfnlzdk5ba/v+/ns+f/U8fc7znnP8Sk8P+DkcDuLB7d+/n3yhUqkIgnj77beVSqVCoSAIoru7u7GxEXW6qpPUajVZZ84hnThxgiCIo0ePNjY2dnd3ozrqR3PQW1NTU8w6moPa0LrR0dEEQaD5CoVCqVQy56Djj4mJQXOY14G57uTkpNPrQJuD1iWRx8Ocg5D9tDrzerq/PtQ5tOtMO54H4PAK2r2vr6+vr48giJqaGvKtmpoa6nxXdbQ6WWfOISUlJSUlJTkcjpKSEuqOqJ95Fk7raA6C1iU30fySkhKnc9DxU+fc93iSkpJcXQfaHLQu9XiYcxCyn1ZnXk/314c6h3adacfjORbznxkAX0CkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQKYAZRApgBpECmEGkAGYQ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ltUVMTn81taWiYmJnQ6HfpTBeTy5ctPPfWUQqHYunXr1NTU2bNnjxw5cvz4cWqPVqs9deoU+XpycnJmZgb91Nm6detrr71Ga87KysrMzJTL5aOjo/X19VVVVQcPHqStu7y8vGXLlunp6b1797LZ7HPnztlsNp1OFxYWRm3btWsXeettt9u1Wm1KSsrKlSvJt7755hv0uymCIMxmc2pqqtFoVCqVLBaroaEhLCzs8uXLnnzddsOne6nr169/+eWXgYGBa9euzczMrK2tZT66pBGJRM8995z7Hj8/P/LjBn3oREZGUhtef/31LVu2dHV1kb8Z5XA4ycnJarWaFhGCIKRS6YoVK8jXtI8wVEc+++wztVrd3NxsNpvlcnlTU9PDDz/MPDy5XN7f33/q1KnW1tbw8PD6+nrmr28FAgFajrYueVdElZGR0dPTc/r06ZaWlsjIyObmZqc3jmw2+8KFC++//357e7vVan3++eePHDnCvJVOTk622Wzk65SUFOpbLNZ/7nO4XG53d/fJkye7urpYLFZRUVFZWZmPeSLgj1sAdvBXnQAziBTADCIFMPsXfuHh4eJXJyUAAAAASUVORK5CYII=";

  $data = explode(',', $my_base64_string );
  $image = base64_decode($data[1]);        
  $file = UPLOAD_DIR . uniqid() . '.png'; 
  $success = file_put_contents($file, $image);
  print $success ? $file : 'Unable to save the file.';        
?>

输出 enter image description here

答案 1 :(得分:0)

在html中使用base64字符串作为src。

<img alt="Embedded Image" src="data:image/png;base64..." />