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常用Output和ptions/Info函数集介绍
Jun 19 PHP
完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题
Jun 20 PHP
php使用反射插入对象示例分享
Mar 11 PHP
smarty模板引擎之内建函数用法
Mar 30 PHP
php数字每三位加逗号的功能函数
Oct 22 PHP
PHP浮点数的一个常见问题
Mar 10 PHP
Zend Framework实现多服务器共享SESSION数据的方法
Mar 22 PHP
PHP使用http_build_query()构造URL字符串的方法
Apr 02 PHP
php导出csv文件,可导出前导0实例代码
Nov 16 PHP
PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)
Nov 19 PHP
PHP执行普通shell命令流程解析
Aug 24 PHP
如何利用PHP实现上传图片功能详解
Sep 24 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
深入array multisort排序原理的详解
2013/06/18 PHP
yii2框架中使用下拉菜单的自动搜索yii-widget-select2实例分析
2016/01/09 PHP
php 类中的常量、静态属性、非静态属性的区别
2017/04/09 PHP
PHP查找一列有序数组是否包含某值的方法
2020/02/07 PHP
一个很酷的拖动层的js类,兼容IE及Firefox
2009/06/23 Javascript
再论Javascript下字符串连接的性能
2011/03/05 Javascript
Jquery Uploadify多文件上传带进度条且传递自己的参数
2013/08/28 Javascript
javascript操作table(insertRow,deleteRow,insertCell,deleteCell方法详解)
2013/12/16 Javascript
innerHTML属性,outerHTML属性,textContent属性,innerText属性区别详解
2015/03/13 Javascript
three.js中文文档学习之创建场景
2017/11/20 Javascript
jquery实现企业定位式导航效果
2018/01/01 jQuery
微信小程序实现漂亮的弹窗效果
2020/05/26 Javascript
使用vuepress搭建静态博客的示例代码
2019/02/14 Javascript
JS将时间秒转换成天小时分钟秒的字符串
2019/07/10 Javascript
jQuery 筛选器简单操作示例
2019/10/02 jQuery
Sublime Text3 配置 NodeJs 环境的方法
2020/05/20 NodeJs
js实现网页随机验证码
2020/10/19 Javascript
[48:52]DOTA2上海特级锦标赛A组小组赛#2 Secret VS CDEC第一局
2016/02/25 DOTA
[04:40]2016国际邀请赛中国区预选赛全程TOP10镜头集锦
2016/07/01 DOTA
[01:20]2018DOTA2亚洲邀请赛总决赛战队LGD晋级之路
2018/04/07 DOTA
[46:49]完美世界DOTA2联赛PWL S3 access vs Rebirth 第二场 12.19
2020/12/24 DOTA
Python 用户登录验证的小例子
2013/03/06 Python
python基础教程之python消息摘要算法使用示例
2014/02/10 Python
使用httplib模块来制作Python下HTTP客户端的方法
2015/06/19 Python
Python中将字典转换为XML以及相关的命名空间解析
2015/10/15 Python
Linux中Python 环境软件包安装步骤
2016/03/31 Python
深入flask之异步非堵塞实现代码示例
2018/07/31 Python
Python爬取qq空间说说的实例代码
2018/08/17 Python
Python日期时间Time模块实例详解
2019/04/15 Python
Python深拷贝与浅拷贝用法实例分析
2019/05/05 Python
python opencv摄像头的简单应用
2019/06/06 Python
浅谈OpenCV中的新函数connectedComponentsWithStats用法
2020/07/05 Python
django rest framework 自定义返回方式
2020/07/12 Python
Python tkinter之Bind(绑定事件)的使用示例
2021/02/05 Python
瑞典Happy Socks美国官网:购买色彩斑斓的快乐袜子
2016/10/19 全球购物
怎样写辞职信
2015/02/27 职场文书