php图片等比压缩代码,支持jpg,png,gif,验证gif动画

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bzxcl/article/details/53421692
<?php
/**
 * Created by PhpStorm.
 * User: as
 * Date: 2016-12-01
 * Time: 8:38
 */
//图片压缩
function ImageCondens($filepase){
    list($new_width,$new_height,$imgtype)=getimagesize($filepase);
    if($new_width>550){//550为自定义宽度
        $scaling=$new_width/550;//缩放比例
        $picwidth=($new_width/$scaling);
        $picheight=($new_height/$scaling);
    }else{
        $picwidth=$new_width;
        $picheight=$new_height;
    }
    switch ($imgtype){
        case 1:
         $fp=fopen($filepase,'rb');
         $image_head = fread($fp,1024);
            fclose($fp);
         if(preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)){//屏蔽gif动画
             echo "<script>alert('系统不支持GIF动画图片');</script>";
         }else{
             if($image_head){
                 header('Content-Type:image/gif');
                 $img_wp=imagecreatetruecolor($picwidth,$picheight);
                 $img=imagecreatefromgif($filepase);
                 imagecopyresampled($img_wp,$img,0,0,0,0,$picwidth,$picheight,$new_width,$new_height);
                 imagejpeg($img_wp,null,100);
                 imagedestroy($img_wp);
             }
         }
         break;
        case 2:
            header('Content-Type:image/jpeg');
            $img_wp=imagecreatetruecolor($picwidth,$picheight);
            $img = imagecreatefromjpeg($filepase);
            imagecopyresampled($img_wp,$img,0,0,0,0,$picwidth,$picheight,$new_width,$new_height);
            imagejpeg($img_wp,null,100);
            imagedestroy($img_wp);
            break;
        case 3:
            header('Content-Type:image/png');
            $img_wp=imagecreatetruecolor($picwidth,$picheight);
            $img = imagecreatefrompng($filepase);
            imagecopyresampled($img_wp,$img,0,0,0,0,$picwidth,$picheight,$new_width,$new_height);
            imagejpeg($img_wp,null,100);
            imagedestroy($img_wp);
            break;
    }

}

ImageCondens("images/3.jpg");

阅读更多

没有更多推荐了,返回首页