php实现等比例压缩图片

juzi4年前技术1447
/**
     * 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

相关文章

An another FPM instance seems to already listen on /tmp/php-cgi-71.sock

今天在更新配置重启php-fpm服务的时候遇到了这样的一个问题:An another FPM instance seems to already listen on /tmp/php-cgi-71.s...

php 发送邮件(实例)

function sendMail($to,$title,$content){    //引入PHPMailer的核心文件 使用require_once包含避免出现PHPMaile...

windows下php安装redis扩展

windows下php安装redis扩展

亲身安装遇到的坑,百度的安装很多文章缺胳膊少腿,会发现按步骤安装完还是没有服务,下面把安装步骤与注意的地方写下了,用php5.6.27 +nts 举例首先确定电脑已经安装好了redis并且能使用,没有...

php操作redis常用方法

参考:https://www.cnblogs.com/aipiaoborensheng/p/5666005.html1,connect描述:实例连接到一个Redis.参数:host: string,p...

php上传大文件配置

php.ini配置文件中的默认文件上传大小为2M, 默认upload_max_filesize = 2M,即文件上传的大小为2M,如果你想上传超过8M的文件,比如20M,你必须设定upload_max...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。