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));  


  • 2021-01-16 09:31:14

    Xcode两种引入图片的方法

    如果是纯代码,图片名需要手动添加@2x,@3x等倍数标识,且需要指明后缀.png,.jpg;IB添加图片的话只需要指明后缀就好,不用添加倍数标识。

  • 2021-01-16 09:39:32

    iOS 更改状态栏、导航栏颜色,电池颜色

    注意事项,两种方法设置View controller-based status bar appearance 的值不一样,并且如果你的plist里面没有View controller-based status bar appearance,你需要新建一个。然后就可以成功了。

  • 2021-01-16 09:45:19

    tabbar的titlePositionAdjustment设置文字距离

    指定相应的数据去偏移一个位置,向右或者向下为正值,向左或者向上为负值,不过首先你得有一个相对位置的坐标。而tabbarItem文字的坐标是底部为x轴,y轴则是tabbarItem的centerX;

  • 2021-01-16 09:49:13

    Cocoapods如何查看项目中引入库的版本号

    项目中已经安装过Cocoapods,并生成了Podfile.lock文件。 打开终端,cd命令切换到项目中的Podfile.lock文件目录下,执行命令:cat Podfile.lock 即可。也可以用文本方式打开 Podfile.lock 文件。示例如下: