欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php源碼之將圖片轉(zhuǎn)化為data/base64數(shù)據(jù)流實例詳解

 更新時間:2016年11月27日 12:36:32   投稿:lqh  
在網(wǎng)站開發(fā)中,我們可以看到有的網(wǎng)站將圖片轉(zhuǎn)化為base64數(shù)據(jù)流,這樣做的好處有兩點,一是減少服務(wù)器http請求,二是可以將圖片作為字符串存儲在數(shù)據(jù)庫中,即圖片可以直接從數(shù)據(jù)庫中讀取,那么php如何將圖片轉(zhuǎn)化為data/base64字符串呢?,需要的朋友可以參考下

php源碼之將圖片轉(zhuǎn)化為data/base64數(shù)據(jù)流

這里我們分享一個將圖片轉(zhuǎn)換為base64編碼格式的方法:

<?php
$img = 'test.jpg';
$base64_img = base64EncodeImage($img);
 
echo '<img src="' . $base64_img . '" />';
/* 作者:http://www.manongjc.com */
function base64EncodeImage ($image_file) {
  $base64_image = '';
  $image_info = getimagesize($image_file);
  $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
  $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
  return $base64_image;
}
?>

通過上面的方法轉(zhuǎn)換后得到的base64編碼字符串,可以存放到數(shù)據(jù)庫中,需要時可以直接從數(shù)據(jù)庫中讀取,減少訪問圖片時的請求數(shù)量。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論