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 相关文章推荐
杏林同学录(五)
Oct 09 PHP
php 来访国内外IP判断代码并实现页面跳转
Dec 18 PHP
如何用php生成扭曲及旋转的验证码图片
Jun 07 PHP
浅析PHP原理之变量(Variables inside PHP)
Aug 09 PHP
PHP移动文件指针ftell()、fseek()、rewind()函数总结
Nov 18 PHP
php使用PDO方法详解
Dec 27 PHP
php二维数组合并及去重复的方法
Mar 04 PHP
PHP常用工具类大全附全部代码下载
Dec 07 PHP
PHP7 新特性详细介绍
Sep 06 PHP
laravel中短信发送验证码的实现方法
Apr 25 PHP
Yii框架实现对数据库的CURD操作示例
Sep 03 PHP
redis+php实现微博(三)微博列表功能详解
Sep 23 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边学边教》(04.编写简易的通讯录――视频教程1)
2006/12/13 PHP
一个PHP操作Access类(PHP+ODBC+Access)
2007/01/02 PHP
PHP 出现乱码和Sessions验证问题的解决方法!
2008/12/06 PHP
浅析php单例模式
2014/11/25 PHP
PHP函数extension_loaded()用法实例
2015/01/19 PHP
PHP多维数组遍历方法(2种实现方法)
2015/12/10 PHP
php微信公众号开发之翻页查询
2018/10/20 PHP
PHP explode()函数用法讲解
2019/02/15 PHP
laravel5.6实现数值转换
2019/10/23 PHP
基于jquery的点击链接插入链接内容的代码
2012/07/31 Javascript
JS实现在网页中弹出一个输入框的方法
2015/03/03 Javascript
jQuery使用deferreds串行多个ajax请求
2016/08/22 Javascript
jQuery判断邮箱格式对错实例代码讲解
2017/04/12 jQuery
文本溢出插件jquery.dotdotdot.js使用方法详解
2017/06/22 jQuery
小程序实现授权登陆的解决方案
2018/12/02 Javascript
通过js随机函数Math.random实现乱序
2020/05/19 Javascript
ElementUI 修改默认样式的几种办法(小结)
2020/07/29 Javascript
python实现百度关键词排名查询
2014/03/30 Python
python通过正则查找微博@(at)用户的方法
2015/03/13 Python
python在指定目录下查找gif文件的方法
2015/05/04 Python
用python脚本24小时刷浏览器的访问量方法
2018/12/07 Python
如何在django里上传csv文件并进行入库处理的方法
2019/01/02 Python
Django 设置多环境配置文件载入问题
2020/02/25 Python
使用Python获取当前工作目录和执行命令的位置
2020/03/09 Python
联想韩国官网:Lenovo Korea
2018/05/10 全球购物
C#如何判断当前用户是否输入某个域
2015/12/07 面试题
宿舍打麻将检讨书
2014/01/24 职场文书
寄语是什么意思
2014/04/10 职场文书
《学会合作》教学反思
2014/04/12 职场文书
2014小学语文教学工作总结
2014/12/17 职场文书
大学生逃课检讨书
2015/05/04 职场文书
唐山大地震观后感
2015/06/05 职场文书
校园歌手大赛主持词
2015/07/03 职场文书
vue+element ui实现锚点定位
2021/06/29 Vue.js
Python Matplotlib绘制条形图的全过程
2021/10/24 Python
Python读取和写入Excel数据
2022/04/20 Python