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

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 "";
         }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");

你可能感兴趣的:(php)