周末在家做技术调研,读取一行图片并将其转换为base64字符串。
代码如下:
<!--?php
/**
* Created by PhpStorm.
* User: yuqingzhang
* Date: 16/8/6
* Time: 19:46
*/
class changeformat
{
public function changeImageToString()
{
$file = "zhima/hello.jpg";
if($fp = fopen($file,"rb", 0))
{
$gambar = fread($fp,filesize($file));
fclose($fp);
$base64 = chunk_split(base64_encode($gambar));
echo 'str is :'.base64_encode($gambar).'<br/-->';
//Result
$encode = '<img src="https://zhangyuqing.cn/wp-content/uploads/2016/08/gifbase64.png' . $base64 .'" />';
echo $encode;
}
}
}
$changeFormat = new changeformat();
$changeFormat->changeImageToString();