码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • PHP图片压缩,再转base64传输


    1. /**
    2. * 获取图片的Base64编码(不支持url)
    3. * @param $img_file
    4. * @return string
    5. */
    6. function img_to_base64($img_file) {
    7. $img_base64 = '';
    8. if (file_exists($img_file)) {
    9. $app_img_file = $img_file; // 图片路径
    10. $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
    11. //echo '
      ' . print_r($img_info, true) . '

      ';
    12. $fp = fopen($app_img_file, "r"); // 图片是否可读权限
    13. if ($fp) {
    14. $filesize = filesize($app_img_file);
    15. $content = fread($fp, $filesize);
    16. $file_content = base64_encode($content); // base64编码
    17. switch ($img_info[2]) { //判读图片类型
    18. case 1: $img_type = "gif";
    19. break;
    20. case 2: $img_type = "jpg";
    21. break;
    22. case 3: $img_type = "png";
    23. break;
    24. }
    25. $img_base64 = $file_content;//合成图片的base64编码
    26. // file_put_contents('/usr/local/paperless/apache/11.txt',$img_base64. PHP_EOL, FILE_APPEND);
    27. }
    28. fclose($fp);
    29. }
    30. return $img_base64; //返回图片的base64
    31. }
    32. /**
    33. * 压缩图片大小
    34. * @param $img_file
    35. * @param int $percent
    36. * @param int $quality
    37. * @param int $new_width 指定的宽度
    38. * @param int $new_height 指定的高度
    39. */
    40. function imgcompress($img_file,$percent=1,$quality=100,$new_width=0,$new_height=0){
    41. list($width, $height, $type, $attr) = getimagesize($img_file);
    42. $type = image_type_to_extension($type,false);
    43. $fun = "imagecreatefrom".$type;
    44. $image = $fun($img_file);
    45. if ($new_height==0 && $new_width==0){
    46. //如果没有指定宽高,则按比例计算
    47. $new_width = $width * $percent;
    48. $new_height = $height * $percent;
    49. }
    50. $image_thump = imagecreatetruecolor($new_width,$new_height);
    51. //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
    52. imagecopyresampled($image_thump,$image,0,0,0,0,$new_width,$new_height,$width,$height);
    53. imagedestroy($image);
    54. // unlink($img_file);
    55. $image = $image_thump;
    56. $funcs = "image".$type;
    57. if ($type == 'jpeg'){
    58. $funcs($image,$img_file,$quality);
    59. }else{
    60. $funcs($image,$img_file);
    61. }
    62. }
    63. imgcompress($outpath,1,70,1024,600);
    64. //图片转base64存储
    65. $img_base64 = img_to_base64($outpath);
    66. unlink($outpath);

  • 相关阅读:
    【2023云栖】郭瑞杰:阿里云搜索产品智能化升级
    一张图搞清楚wait、sleep、join、yield四者区别,面试官直接被征服!
    Sqoop数据导入 第3关:Mysql导入数据至Hive中
    bin.zip和bin.tar.gz以及src.zip和src.tar.gz以及rpm和dmg的区别
    数据结构-二叉树
    Java模拟抽奖。奖池有以下几个奖项:【2,1888,588,388,2888】打印出抽奖结果,要求随机且不重复。两种方法(代码和优化后的代码)
    【Redis学习1】Redis持久化机制详解
    python爬虫1
    Python(3)对象
    机器人相关课程考核材料归档实施细则2022版本
  • 原文地址:https://blog.csdn.net/licanfeng1/article/details/128039375
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号