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 相关文章推荐
dedecms模版制作使用方法
Apr 03 PHP
php数组函数序列之array_combine() - 数组合并函数使用说明
Oct 29 PHP
利用php+mcDropdown实现文件路径可在下拉框选择
Aug 07 PHP
phpmyadmin打开很慢的解决方法
Apr 21 PHP
php小技巧之过滤ascii控制字符
May 14 PHP
PHP积分兑换接口实例
Feb 09 PHP
PHP将进程作为守护进程的方法
Mar 19 PHP
PHP微信企业号开发之回调模式开启与用法示例
Nov 25 PHP
PHP多线程模拟实现秒杀抢单
Feb 07 PHP
PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能
May 31 PHP
Yii支持多域名cors原理的实现
Dec 05 PHP
PHP实现计算器小功能
Aug 28 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
如何在HTML 中嵌入 PHP 代码
2015/05/13 PHP
Yii的Srbac插件用法详解
2016/07/14 PHP
PHP基于socket实现的简单客户端和服务端通讯功能示例
2017/07/10 PHP
jquery Moblie入门—hello world的示例代码学习
2013/01/08 Javascript
js获取RadioButtonList的Value/Text及选中值等信息实现代码
2013/03/05 Javascript
浮动的div自适应居中显示的js代码
2013/12/23 Javascript
JavaScript中的bold()方法使用详解
2015/06/08 Javascript
jQuery实现图片左右滚动特效
2020/04/20 Javascript
javascript:void(0)点击登录没反应怎么解决
2015/11/13 Javascript
JavaScript提升性能的常用技巧总结【经典】
2016/06/20 Javascript
BootStrap3使用错误记录及解决办法
2016/12/22 Javascript
ES6中Array.copyWithin()函数的用法实例详解
2017/09/16 Javascript
javascript function(函数类型)使用与注意事项小结
2019/06/10 Javascript
JS数据类型STRING使用实例解析
2019/12/18 Javascript
[42:32]完美世界DOTA2联赛循环赛 Magma vs PXG BO2第二场 10.28
2020/10/28 DOTA
[33:33]完美世界DOTA2联赛PWL S2 FTD.C vs SZ 第二场 11.27
2020/11/30 DOTA
零基础写python爬虫之爬虫的定义及URL构成
2014/11/04 Python
使用python实现个性化词云的方法
2017/06/16 Python
python根据unicode判断语言类型实例代码
2018/01/17 Python
python3.6+django2.0开发一套学员管理系统
2018/03/03 Python
Python实现高斯函数的三维显示方法
2018/12/29 Python
django框架使用views.py的函数对表进行增删改查内容操作详解【models.py中表的创建、views.py中函数的使用,基于对象的跨表查询】
2019/12/12 Python
python词云库wordCloud使用方法详解(解决中文乱码)
2020/02/17 Python
Python基于gevent实现高并发代码实例
2020/05/15 Python
使用Keras 实现查看model weights .h5 文件的内容
2020/06/09 Python
python如何支持并发方法详解
2020/07/25 Python
python实现磁盘日志清理的示例
2020/11/05 Python
利用HTML5+CSS3实现3D转换效果实例详解
2017/05/02 HTML / CSS
会计自荐书
2013/12/02 职场文书
书法兴趣小组活动总结
2014/07/07 职场文书
公务员党的群众路线教育实践活动学习心得体会
2014/10/30 职场文书
大学生创业事迹材料
2014/12/30 职场文书
2016廉政教育学习心得体会
2016/01/25 职场文书
《猴王出世》教学反思
2016/02/23 职场文书
2019年销售部季度工作计划3篇
2019/10/09 职场文书
PHP控制循环操作的时间
2021/04/01 PHP