溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php的文件怎么轉(zhuǎn)base64

發(fā)布時(shí)間:2021-07-23 17:20:13 來源:億速云 閱讀:389 作者:chen 欄目:編程語言

這篇文章主要講解了“php的文件怎么轉(zhuǎn)base64”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“php的文件怎么轉(zhuǎn)base64”吧!

php文件轉(zhuǎn)base64的方法:首先創(chuàng)建一個(gè)PHP示例文件;然后通過“function fileToBase64($file){...}”方法將文件轉(zhuǎn)base64輸出即可。

本文操作環(huán)境:Windows7系統(tǒng)、PHP7.1版,DELL G3電腦

php 文件怎么轉(zhuǎn)base64?

文件轉(zhuǎn)base64輸出

Data URI scheme是在RFC2397中定義的,目的是將一些小的數(shù)據(jù),直接嵌入到網(wǎng)頁中,從而不用再?gòu)耐獠课募d入。

優(yōu)點(diǎn):減少http連接數(shù)。

缺點(diǎn):這種格式的文件不會(huì)被瀏覽器緩存。

不建議將“超過20KB”的文件編碼使用,太大的文件反而起反作用。

data: URI的基本使用格式如下:

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

mime-type是嵌入數(shù)據(jù)的mime類型,比如png圖片就是image/png。

如果后面跟base64,說明后面的data是采用base64方式進(jìn)行編碼的

Data URI scheme支持的類型有:

data:,文本數(shù)據(jù)
data:text/plain,文本數(shù)據(jù)
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圖片數(shù)據(jù)
data:image/png;base64,base64編碼的png圖片數(shù)據(jù)
data:image/jpeg;base64,base64編碼的jpeg圖片數(shù)據(jù)
data:image/x-icon;base64,base64編碼的icon圖片數(shù)據(jù)

例如:<img src="http://avatar.csdn.net/B/D/B/3_fdipzone.jpg" /> 轉(zhuǎn)為data uri后為

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABLAEsDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh5iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD2VAxVvu5wMDA9Kqzu2f4cf7orQCGNiDg8D+VNkt0mQkHawrz4RtGzOnm1uY0rjDOSoA5JIHFeVeLvGOni+eC1t0kkQkNMowWP4dq6z4k6z/wj+jNbRgm5uhhWDfcHr/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Qjquo3N7OzJFbKEUDjczD+i/8AoVenQaFYLAo8rPuTWB4HRU8C6ZKow8u+SRu7MZCMn8AB+Fdraoptk47etZtRcrWFKrNK9z//2Q==" />

php轉(zhuǎn)換方法:

/** 文件轉(zhuǎn)base64輸出
* @param  String $file 文件路徑
* @return String base64 string
*/
function fileToBase64($file){
    $base64_file = '';
    if(file_exists($file)){
        $mime_type= mime_content_type($file);
        $base64_data = base64_encode(file_get_contents($file));
        $base64_file = 'data:'.$mime_type.';base64,'.$base64_data;
    }
    return $base64_file;
}
 
/** base64轉(zhuǎn)文件輸出
* @param  String $base64_data base64數(shù)據(jù)
* @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('1.jpg') ?>" />
 
//base64 to file
$file = "test.jpg";
$data = '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABLAEsDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh5iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFB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7gNXNXdWSXs7L1OpRUHZ6niGpW9slwUtp/NQqBuxjJx6Vkjdaz+YA4BBVwjlCR9RVuBQYdx5bcOfwFTTxoU+7XZujJocttLbaemqabazTLGT5t4r7ngzxhlGCM+p496v6b4516xj2peR3UechLlNxHtnrisizd7dmlhdo3QgqVYj/APWPat7VNLsU1JAlsirJZNOwXgb/ACycgduecDiosQ9Nye2+JmqiYi/0+0uYCW3xopQkHoAeeBXHa1qck8wksYXht0l3rG/z5XPQ+uKqRyO7ruYnAx+FPWRhkBjgqCfrRszRL3SjqOpwXDyS/YrYAk/LsIP5jH8q6XRb0XGh38YRFhgykYKjePbd1I9B2rnbuCKVJGdASsZYHpyK623gigCQxIFjVQQopvVWIpJqTZ0/gbQjquo3N7OzJFbKEUDjczD+i/8AoVenQaFYLAo8rPuTWB4HRU8C6ZKow8u+SRu7MZCMn8AB+Fdraoptk47etZtRcrWFKrNK9z//2Q==';
 
if(base64ToFile($data, $file)){
    echo '<img src="'.$file.'" />';
}

感謝各位的閱讀,以上就是“php的文件怎么轉(zhuǎn)base64”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)php的文件怎么轉(zhuǎn)base64這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI