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 相关文章推荐
自动分页的不完整解决方案
Jan 12 PHP
php初始化对象和析构函数的简单实例
Mar 11 PHP
PHP实现采集中国天气网未来7天天气
Oct 15 PHP
php实现encode64编码类实例
Mar 24 PHP
Java中final关键字详解
Aug 10 PHP
WordPress中登陆后关闭登陆页面及设置用户不可见栏目
Dec 31 PHP
深入浅析yii2-gii自定义模板的方法
Apr 26 PHP
php简单创建zip压缩文件的方法
Apr 30 PHP
php如何执行非缓冲查询API
Jul 22 PHP
PHP 传输会话curl函数的实例详解
Sep 12 PHP
PHP常用的类封装小结【4个工具类】
Jun 28 PHP
Laravel实现通过blade模板引擎渲染视图
Oct 25 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
教你IIS6的PHP最佳配置方法
2006/09/05 PHP
PHP安装攻略:常见问题解答(一)
2006/10/09 PHP
php下过滤HTML代码的函数
2007/12/10 PHP
台湾中原大学php教程孙仲岳主讲
2008/01/07 PHP
php微信开发之图片回复功能
2018/06/14 PHP
js 获取和设置css3 属性值的实现方法
2013/05/06 Javascript
javascript时间函数大全
2014/06/30 Javascript
jquery实现用户信息修改验证输入方法汇总
2015/07/18 Javascript
js 求时间差的实现代码
2016/04/26 Javascript
AngularJS实现在ng-Options加上index的解决方法
2016/11/03 Javascript
angularjs点击图片放大实现上传图片预览
2017/02/24 Javascript
JavaScript数据结构与算法之二叉树插入节点、生成二叉树示例
2019/02/21 Javascript
微信小程序使用wx.request请求服务器json数据并渲染到页面操作示例
2019/03/30 Javascript
微信小程序实现菜单左右联动
2020/05/19 Javascript
如何将Node.js中的回调转换为Promise
2020/11/10 Javascript
python 实现插入排序算法
2012/06/05 Python
Python简单日志处理类分享
2015/02/14 Python
EM算法的python实现的方法步骤
2018/01/02 Python
Python实现的根据IP地址计算子网掩码位数功能示例
2018/05/23 Python
利用Python如何将数据写到CSV文件中
2018/06/05 Python
python 获得任意路径下的文件及其根目录的方法
2019/02/16 Python
python脚本开机自启的实现方法
2019/06/28 Python
django中间键重定向实例方法
2019/11/10 Python
Python如何避免文件同名产生覆盖
2020/06/09 Python
python Scrapy框架原理解析
2021/01/04 Python
Matplotlib中rcParams使用方法
2021/01/05 Python
python sleep和wait对比总结
2021/02/03 Python
一款纯css3实现的动画加载导航
2014/10/08 HTML / CSS
英国Lookfantastic中文网站:护肤品美妆美发购物(英国直邮)
2020/04/27 全球购物
毕业生就业推荐信范文
2013/12/01 职场文书
创建青年文明号材料
2014/05/09 职场文书
2015年幼儿园新年寄语
2014/12/08 职场文书
婚礼领导致辞大全
2015/07/28 职场文书
详解MySQL连接挂死的原因
2021/05/18 MySQL
「偶像大师 MILLION LIVE!」七尾百合子手办开订
2022/03/21 日漫
Python中itertools库的四个函数介绍
2022/04/06 Python