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

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

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

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

這里我們分享一個(gè)將圖片轉(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;
}
?>

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

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

相關(guān)文章

最新評(píng)論