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
}

最新文章

  1. 《理解 ES6》阅读整理:函数(Functions)(六)Purpose of Functions
  2. Ubuntu 14.04 下 Chromium 出现 未安装Adobe Flash Player 问题解决
  3. iOS6新特征:UICollectionView高级使用示例之CircleLayout
  4. HTmlTableTOExcel
  5. 查看图片真正的格式,在不知道扩展名的情况下区分是jpeg还是bmp
  6. iOSpush过后返回多级界面
  7. WordPress Shareaholic 插件跨站请求伪造漏洞
  8. 主从mysql 同步设置
  9. javascript继承的写法
  10. Inno Setup入门(十一)&mdash;&mdash;完成安装后执行某些程序
  11. scoring_criteria.go
  12. Java内存溢出优化性能优化
  13. Java重写equals方法(重点讲解)
  14. 解决RabbitMQ service is already present - only up...
  15. [fixed] 解决 slf4j + log4j eclipse 可以打印日志,而在云服务器上不能打印
  16. js 中实现aop
  17. .NET中Debug模式与Release模式差别
  18. Mysql建表好的例子
  19. PAT 1088 三人行
  20. Anaconda 介绍及安装

热门文章

  1. chrome出现“由贵单位管理”原因及解决方法
  2. Selenium全屏截图,使用PIL拼接滚动截图
  3. 八、linux文件系统上的特殊权限 SUID 、GUID、Sticky
  4. Hadoop Shell命令字典
  5. 八十年代的经典 NFS和AFS
  6. postgresql could not connect to server
  7. CF1195A
  8. 方阵转置(c++)
  9. elasticsearch shield(5.0以下版本 权限认证)
  10. 【Golang】基于录制,自动生成go test接口自动化用例