PHP等比例压缩图片的实例代码


Posted in PHP onJuly 26, 2018

具体代码如下所示:

/**
   * desription 压缩图片
   * @param sting $imgsrc 图片路径
   * @param string $imgdst 压缩后保存路径
   */
  public function compressedImage($imgsrc, $imgdst) {
    list($width, $height, $type) = getimagesize($imgsrc);
    $new_width = $width;//压缩后的图片宽
    $new_height = $height;//压缩后的图片高
    if($width >= 600){
      $per = 600 / $width;//计算比例
      $new_width = $width * $per;
      $new_height = $height * $per;
    }
    switch ($type) {
      case 1:
        $giftype = check_gifcartoon($imgsrc);
        if ($giftype) {
          header('Content-Type:image/gif');
          $image_wp = imagecreatetruecolor($new_width, $new_height);
          $image = imagecreatefromgif($imgsrc);
          imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
          //90代表的是质量、压缩图片容量大小
          imagejpeg($image_wp, $imgdst, 90);
          imagedestroy($image_wp);
          imagedestroy($image);
        }
        break;
      case 2:
        header('Content-Type:image/jpeg');
        $image_wp = imagecreatetruecolor($new_width, $new_height);
        $image = imagecreatefromjpeg($imgsrc);
        imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        //90代表的是质量、压缩图片容量大小
        imagejpeg($image_wp, $imgdst, 90);
        imagedestroy($image_wp);
        imagedestroy($image);
        break;
      case 3:
        header('Content-Type:image/png');
        $image_wp = imagecreatetruecolor($new_width, $new_height);
        $image = imagecreatefrompng($imgsrc);
        imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        //90代表的是质量、压缩图片容量大小
        imagejpeg($image_wp, $imgdst, 90);
        imagedestroy($image_wp);
        imagedestroy($image);
        break;
    }
  }

总结

以上所述是小编给大家介绍的PHP等比例压缩图片的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

PHP 相关文章推荐
Win2003服务器安全加固设置--进一步提高服务器安全性
May 23 PHP
PHP 魔术函数使用说明
May 14 PHP
PHP字符编码问题之GB2312 VS UTF-8解决方法
Jun 23 PHP
PHP spl_autoload_register实现自动加载研究
Dec 06 PHP
php中sprintf与printf函数用法区别解析
Feb 17 PHP
Discuz批量替换帖子内容的方法(使用SQL更新数据库)
Jun 23 PHP
php中获取主机名、协议及IP地址的方法
Nov 18 PHP
Codeigniter通过SimpleXML将xml转换成对象的方法
Mar 19 PHP
PHP输出一个等腰三角形的方法
May 12 PHP
PHP对文件夹递归执行chmod命令的方法
Jun 19 PHP
thinkphp3.x连接mysql数据库的方法(具体操作步骤)
May 19 PHP
Laravel日志用法详解
Oct 09 PHP
PHP实现非阻塞模式的方法分析
Jul 26 #PHP
php实现等比例压缩图片
Jul 26 #PHP
PHP输出Excel PHPExcel的方法
Jul 26 #PHP
PHP微信H5支付开发实例
Jul 25 #PHP
php app支付宝回调(异步通知)详解
Jul 25 #PHP
php支付宝APP支付功能
Jul 29 #PHP
PHP多个图片压缩成ZIP的方法
Aug 18 #PHP
You might like
php中用加号与用array_merge合并数组的区别深入分析
2013/06/03 PHP
关于js和php对url编码的处理方法
2014/03/04 PHP
PHP文件系统管理(实例讲解)
2017/09/19 PHP
非常不错的一个javascript 类
2006/11/07 Javascript
关于恒等于(===)和非恒等于(!==)
2007/08/20 Javascript
textarea 控制输入字符字节数(示例代码)
2013/12/27 Javascript
多个$(document).ready()的执行顺序实例分析
2014/07/26 Javascript
JavaScript常用的返回,自动跳转,刷新,关闭语句汇总
2015/01/13 Javascript
javascript实现动态加载CSS
2015/01/26 Javascript
在linux中使用包管理器安装node.js
2015/03/13 Javascript
vueJS简单的点击显示与隐藏的效果【实现代码】
2016/05/03 Javascript
JavaScript语言精粹经典实例(整理篇)
2016/06/07 Javascript
jquery把int类型转换成字符串类型的方法
2016/10/07 Javascript
详解性能更优越的小程序图片懒加载方式
2018/07/18 Javascript
详解angular部署到iis出现404解决方案
2018/08/14 Javascript
element-ui上传一张图片后隐藏上传按钮功能
2019/05/22 Javascript
通过微信公众平台获取公众号文章的方法示例
2019/12/25 Javascript
JavaScript实现前端倒计时效果
2021/02/09 Javascript
[11:27]《一刀刀一天》之DOTA全时刻20:TI4总奖金突破920W TS赛事分析
2014/06/18 DOTA
Python列表list数组array用法实例解析
2014/10/28 Python
django model去掉unique_together报错的解决方案
2016/10/18 Python
Python中的集合介绍
2019/01/28 Python
更新修改后的Python模块方法
2019/03/03 Python
python 两个数据库postgresql对比
2019/10/21 Python
利用4行Python代码监测每一行程序的运行时间和空间消耗
2020/04/22 Python
Python实现图片查找轮廓、多边形拟合、最小外接矩形代码
2020/07/14 Python
.net C#面试题
2012/08/28 面试题
文员岗位职责
2013/11/09 职场文书
小学生爱国演讲稿
2014/04/25 职场文书
道德大讲堂实施方案
2014/05/14 职场文书
流动人口婚育证明
2014/10/19 职场文书
教师党的群众路线教育实践活动学习心得体会
2014/10/30 职场文书
2014年保洁员工作总结
2014/11/19 职场文书
2016年第二十届“母亲节暨幸福工程救助贫困母亲活动日”活动总结
2016/04/06 职场文书
pdf论文中python画的图Type 3 fonts字体不兼容的解决方案
2021/04/24 Python
MySQL数据库实验实现简单数据库应用系统设计
2022/06/21 MySQL