php实现等比例压缩图片


Posted in PHP onJuly 26, 2018

本文实例为大家分享了php实现等比例压缩图片的具体代码,供大家参考,具体内容如下

/**
   * 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:风雨欲来 路在何方?
Oct 09 PHP
php Static关键字实用方法
Jun 04 PHP
php查看网页源代码的方法
Mar 13 PHP
PHP实现WebService的简单示例和实现步骤
Mar 27 PHP
PHP中file_get_contents函数抓取https地址出错的解决方法(两种方法)
Sep 22 PHP
php自定义函数实现汉字转换utf8编码的方法
Sep 29 PHP
DEDE实现转跳属性文档在模板上调用出转跳地址
Nov 04 PHP
php正则判断是否为合法身份证号的方法
Mar 16 PHP
老生常谈PHP面向对象之标识映射
Jun 21 PHP
tp5(thinkPHP5)操作mongoDB数据库的方法
Jan 20 PHP
使用swoole 定时器变更超时未支付订单状态的解决方案
Jul 24 PHP
thinkphp3.2框架中where条件查询用法总结
Aug 13 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
PHP上传文件及图片到七牛的方法
Jul 25 #PHP
详解PHP版本兼容之openssl调用参数
Jul 25 #PHP
You might like
Apache环境下PHP利用HTTP缓存协议原理解析及应用分析
2010/02/16 PHP
php操作mongoDB实例分析
2014/12/29 PHP
php 截取中英文混合字符串的方法
2018/05/31 PHP
php与阿里云短信接口接入操作案例分析
2020/05/27 PHP
PHP isset()及empty()用法区别详解
2020/08/29 PHP
W3C Group的JavaScript1.8 新特性介绍
2009/05/19 Javascript
Javascript中正则表达式的全局匹配模式分析
2011/04/26 Javascript
javascript实现网页字符定位的方法
2015/07/14 Javascript
Bootstrap 布局组件(全)
2016/07/18 Javascript
JS实现页面打印功能
2017/03/16 Javascript
vue实现选项卡及选项卡切换效果
2018/04/24 Javascript
解决IOS端微信H5页面软键盘弹起后页面下方留白的问题
2019/06/05 Javascript
解决layer.open弹出框不能获取input框的值为空的问题
2019/09/10 Javascript
vue中 数字相加为字串转化为数值的例子
2019/11/07 Javascript
解决vue项目input输入框双向绑定数据不实时生效问题
2020/08/05 Javascript
vue内置组件keep-alive事件动态缓存实例
2020/10/30 Javascript
Python中struct模块对字节流/二进制流的操作教程
2017/01/21 Python
Python实现的FTP通信客户端与服务器端功能示例
2018/03/28 Python
Python利用ORM控制MongoDB(MongoEngine)的步骤全纪录
2018/09/13 Python
python ipset管理 增删白名单的方法
2019/01/14 Python
分析运行中的 Python 进程详细解析
2019/06/22 Python
pandas分区间,算频率的实例
2019/07/04 Python
python实现爬虫抓取小说功能示例【抓取金庸小说】
2019/08/09 Python
python+opencv3生成一个自定义纯色图教程
2020/02/19 Python
Python进程间通信multiprocess代码实例
2020/03/18 Python
opencv 图像加法与图像融合的实现代码
2020/07/08 Python
英国著名书店:Foyles
2018/12/01 全球购物
你所在的项目是如何确定版本号的
2015/12/28 面试题
医药工作岗位求职信分享
2013/12/31 职场文书
小学班长竞选演讲稿
2014/04/24 职场文书
2014县政府领导班子三严三实对照检查材料思想汇报
2014/09/26 职场文书
新闻稿件写作技巧
2015/07/18 职场文书
退休欢送会致辞
2015/07/31 职场文书
少先大队干部竞选稿
2015/11/20 职场文书
一个家长教育孩子的心得体会
2016/01/15 职场文书
《中华上下五千年》读后感3篇
2019/11/29 职场文书