您好,登錄后才能下訂單哦!
這篇文章主要講解了“php的文件怎么轉base64”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“php的文件怎么轉base64”吧!
php文件轉base64的方法:首先創建一個PHP示例文件;然后通過“function fileToBase64($file){...}”方法將文件轉base64輸出即可。
本文操作環境:Windows7系統、PHP7.1版,DELL G3電腦
php 文件怎么轉base64?
文件轉base64輸出
Data URI scheme是在RFC2397中定義的,目的是將一些小的數據,直接嵌入到網頁中,從而不用再從外部文件載入。
優點:減少http連接數。
缺點:這種格式的文件不會被瀏覽器緩存。
不建議將“超過20KB”的文件編碼使用,太大的文件反而起反作用。
data: URI的基本使用格式如下:
data:[<MIME-type>][;base64|charset=some_charset],<data>
mime-type是嵌入數據的mime類型,比如png圖片就是image/png。
如果后面跟base64,說明后面的data是采用base64方式進行編碼的
Data URI scheme支持的類型有:
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圖片數據
例如:<img src="http://avatar.csdn.net/B/D/B/3_fdipzone.jpg" /> 轉為data uri后為
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABLAEsDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQF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uVHJ6Jg7mH47a7DXXRrHKxu6qwZ+p4ry7W7i2haP7ZaLPEXwwDYIPt+v5VUYpWS2OlW9m29zZk8Z2MNk14b64vCjLGtu0Wxlzk7jnrwDVC88ex2Mtij6bDeQ3GnI8m85dWbOAPT61h7vF4bVCbW4lid1B/dZcDI7g5xWXpWlLczxzi789ExtBXPA6DrWqSSuc3K5SsjtNL11bzTWC2ckNy3yQqjlt5PHPA9a7ez8J3iWcSzeekoXDKh5B9BXE6RHMusWEdqAJxMu3jIGP/rV7HG0GwebeKH7gNXNXdWSXs7L1OpRUHZ6niGpW9slwUtp/NQqBuxjJx6Vkjdaz+YA4BBVwjlCR9RVuBQYdx5bcOfwFTTxoU+7XZujJocttLbaemqabazTLGT5t4r7ngzxhlGCM+p496v6b4516xj2peR3UechLlNxHtnrisizd7dmlhdo3QgqVYj/APWPat7VNLsU1JAlsirJZNOwXgb/ACycgduecDiosQ9Nye2+JmqiYi/0+0uYCW3xopQkHoAeeBXHa1qck8wksYXht0l3rG/z5XPQ+uKqRyO7ruYnAx+FPWRhkBjgqCfrRszRL3SjqOpwXDyS/YrYAk/LsIP5jH8q6XRb0XGh38YRFhgykYKjePbd1I9B2rnbuCKVJGdASsZYHpyK623gigCQxIFjVQQopvVWIpJqTZ0/gbQjquo3N7OzJFbKEUDjczD+i/8AoVenQaFYLAo8rPuTWB4HRU8C6ZKow8u+SRu7MZCMn8AB+Fdraoptk47etZtRcrWFKrNK9z//2Q==" />
php轉換方法:
/** 文件轉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轉文件輸出 * @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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ow8u+SRu7MZCMn8AB+Fdraoptk47etZtRcrWFKrNK9z//2Q=='; if(base64ToFile($data, $file)){ echo '<img src="'.$file.'" />'; }
感謝各位的閱讀,以上就是“php的文件怎么轉base64”的內容了,經過本文的學習后,相信大家對php的文件怎么轉base64這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。