php – 在屬性 src 使用 data url 的寫法

php 使用data url 的function :

function data_url($file, $mime)
{
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}

使用Codeigniter,在Controller 裡面的function 可以這樣寫:
public function dataurl()
{
$this->load->helper('file');
$base64 = base64_encode(read_file(你的圖片實際位置));
return $url = 'data:jpg;base64,' . $base64; //我這邊圖片類型直接寫jpg,就不寫判斷了
}

可參考:
http://www.player.idv.tw/prog/index.php/Data_URI_scheme#PHP
http://blog.darkthread.net/post-2010-11-05-data-uri.aspx

 


發表迴響