Home  >  Article  >  Backend Development  >  How to convert php files to base64

How to convert php files to base64

藏色散人
藏色散人Original
2021-07-23 10:47:257905browse

How to convert PHP files to base64: First create a PHP sample file; then use the "function fileToBase64($file){...}" method to convert the file to base64 for output.

How to convert php files to base64

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How to convert php files to base64?

File to base64 output

Data URI scheme is defined in RFC2397. The purpose is to embed some small data directly into the web page, thus No more loading from external files.

Advantages: Reduce the number of http connections.

Disadvantages: Files in this format will not be cached by the browser.

It is not recommended to encode files "more than 20KB", as files that are too large will be counterproductive.

data: The basic usage format of URI is as follows:

data:[<MIME-type>][;base64|charset=some_charset],<data>

mime-type is the mime type of embedded data, for example, a png picture is image/png.

If it is followed by base64, it means that the following data is encoded in base64.

Data URI scheme supports the following types:

data:,文本数据
data:text/plain,文本数据
data:text/html,HTML代码
data:text/html;base64,base64编码的HTML代码
data:text/css,CSS代码
data:text/css;base64,base64编码的CSS代码
data:text/javascript,Javascript代码
data:text/javascript;base64,base64编码的Javascript代码
data:image/gif;base64,base64编码的gif图片数据
data:image/png;base64,base64编码的png图片数据
data:image/jpeg;base64,base64编码的jpeg图片数据
data:image/x-icon;base64,base64编码的icon图片数据

For example: 4106185be1c45c1c4993a25f6888ac66 After converting to data uri, it will be

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABLAEsDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2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MNk14b64vCjLGtu0Wxlzk7jnrwDVC88ex2Mtij6bDeQ3GnI8m85dWbOAPT61h6vF4bVCbW4lid1B/dZcDI7g5xWXpWlLczxzi789ExtBXPA6DrWqSSuc3K5SsjtNL11bzTWC2ckNy3yQqjlt5PHPA9a7ez8J3iWcSzeekoXDKh4B9BXE6RHMusWEdqAJxMu3jIGP/rV7HG0GwebeKH7gNXNXdWSXs7L1OpRUHZ6niGpW9slwUtp/NQqBuxjJx6Vkjdaz+YA4BBVwjlCR9RVuBQYdx5bcOfwFTTxoU+7XZujJocttLbaemqabazTLGT5t4r7ngzxhlGCM+p496v6b4516xj2peR3UechLlNxHtnrisizd7dmlhdo3QgqVYj/APWPat7VNLsU1JAlsirJZNOwXgb/ACycgduecDiosQ9Nye2+JmqiYi/0+0uYCW3xopQkHoAeeBXHa1qck8wksYXht0l3rG/z5XPQ+uKqRyO7ruYnAx+FPWRhkBjgqCfrRszRL3SjqOpwXDyS/YrYAk/LsIP5jH8q6XRb0XGh28YRFhgykYKjePbd1I9B2rnbuCKVJGdASsZYHpyK623gigCQxIFjVQQopvVWIpJqTZ0/gbQjquo3N7OzJFbKEUDjczD+i/8AoVenQaFYLAo8rPuTWB4HRU8C6ZKow8u+SRu7MZCMn8AB+Fdraoptk47etZtRcrWFKrNK9z//2Q==" />

php conversion method:

/** 文件转base64输出
* @param  String $file 文件路径
* @return String base64 string
*/
function fileToBase64($file){
    $base64_file = &#39;&#39;;
    if(file_exists($file)){
        $mime_type= mime_content_type($file);
        $base64_data = base64_encode(file_get_contents($file));
        $base64_file = &#39;data:&#39;.$mime_type.&#39;;base64,&#39;.$base64_data;
    }
    return $base64_file;
}
 
/** base64转文件输出
* @param  String $base64_data base64数据
* @param  String $file        要保存的文件路径
* @return boolean
*/
function base64ToFile($base64_data, $file){
    if(!$base64_data || !$file){
        return false;
    }
    return file_put_contents($file, base64_decode($base64_data), true);
}

demo:

//file to base64
<img src="<?=fileToBase64(&#39;1.jpg&#39;) ?>" />
 
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etZtRcrWFKrNK9z//2Q==&#39;;
 
if(base64ToFile($data, $file)){
    echo &#39;<img src="&#39;.$file.&#39;" />&#39;;
}

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert php files to base64. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn