php 將圖片文件轉成base64編碼的方法


php 將圖片文件轉成base64編碼的方法
<pre>
<?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;
}

print_r(fileToBase64('bd_logo1.png'));
exit();
?>
</pre>
base64編碼直接 在瀏覽器地址欄輸入就可以看到圖片啦。。
ps:mime_content_type函數在 php5.3 以上就被廢棄了, 如果要用就要重新開啟


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM