php图片文件、二进制流、base64格式相互转化

2020-01-10 19:42:00

1,php 图片文件、二进制流、base64格式相互转化

$image = 'E:/www/logo.png';  //图片文件地址
 
$type = getimagesize($image)['mime'];    //获取图片类型
$imgData = file_get_contents($image);    //获取图片二进制流
 
//输出二进制图片
ob_clean();    //清除缓冲区,防止出现“图像因其本身有错无法显示'的问题
header("Content-Type:{$type}");
echo $imgData; //输出图片
 
//或者把此文件地址作为img标签src地址输出
//<img src="imgdata.php">

2,二进制流转化为图片文件

$file = "E:/www/logo2.png";
file_put_contents( $file, $imgData);


3,二进制流转化为base64格式图片

$type = getimagesizefromstring($imageData)['mime']; //获取二进制流图片格式
$base64String = 'data:' . $type . ';base64,' . chunk_split(base64_encode($imageData));
 
//格式如:
'data:image/png;base64,iVBORw0...此处省略...RZV0P=';
 
 
//输出图片
echo "<img src='{$base64String }'>";

4,base64格式图片转化为二进制流

//截取data:image/png;base64, 这个逗号后的字符
$array = explode(',', $base64String); 
 
//对截取后的字符使用base64_decode进行解码,此为二进制流图片
$imgData = base64_decode(end($array));  


  • 2017-11-06 01:00:17

    撤销git add

    如何撤销git add,不小心执行了git add . 操作,但是又不能提交所有的文件,因为对应不同的分支,现在怎么样可以将git add 撤销回来

  • 2017-11-10 00:06:15

    CORS: credentials mode is 'include'

    XMLHttpRequest cannot load http://localhost/Foo.API/token. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:5000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.