php 处理图片 将图片转成base64
2024-09-04 23:31:04
1.直接将图片路径传入下面该方法就可以了
//将图片转成base64 public function imgToBase64($img_file){ $img_base64 = '';
if ($img_file){
$img_file = APP_PUBLIC.$img_file;
if (file_exists($img_file)) { $app_img_file = $img_file; // 图片路径
$img_info = getimagesize($app_img_file); // 取得图片的大小,类型等 //echo '<pre>' . print_r($img_info, true) . '</pre><br>';
$fp = fopen($app_img_file, "r"); // 图片是否可读权限 if ($fp) {
$filesize = filesize($app_img_file);
$content = fread($fp, $filesize);
$file_content = chunk_split(base64_encode($content)); // base64编码
switch ($img_info[2]) { //判读图片类型
case 1: $img_type = "gif";
break;
case 2: $img_type = "jpg";
break;
case 3: $img_type = "png";
break;
} $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码 }
fclose($fp);
}else{
$img_base64 = null;
}
}else{
$img_base64 = null;
} return $img_base64; //返回图片的base64
}
最新文章
- 《理解 ES6》阅读整理:函数(Functions)(六)Purpose of Functions
- Ubuntu 14.04 下 Chromium 出现 未安装Adobe Flash Player 问题解决
- iOS6新特征:UICollectionView高级使用示例之CircleLayout
- HTmlTableTOExcel
- 查看图片真正的格式,在不知道扩展名的情况下区分是jpeg还是bmp
- iOSpush过后返回多级界面
- WordPress Shareaholic 插件跨站请求伪造漏洞
- 主从mysql 同步设置
- javascript继承的写法
- Inno Setup入门(十一)&mdash;&mdash;完成安装后执行某些程序
- scoring_criteria.go
- Java内存溢出优化性能优化
- Java重写equals方法(重点讲解)
- 解决RabbitMQ service is already present - only up...
- [fixed] 解决 slf4j + log4j eclipse 可以打印日志,而在云服务器上不能打印
- js 中实现aop
- .NET中Debug模式与Release模式差别
- Mysql建表好的例子
- PAT 1088 三人行
- Anaconda 介绍及安装