PHP图片压缩方法

//图片压缩
function ImageCondens($filepase){
    list($new_width,$new_height,$imgtype)=getimagesize($filepase);
	// dump($new_width.','.$new_height.','.$imgtype);
    if($new_width>640){//640为自定义宽度
        $scaling=$new_width/640;//缩放比例
        $picwidth=($new_width/$scaling);
        $picheight=($new_height/$scaling);
    }else{
        $picwidth=$new_width;
        $picheight=$new_height;
    }
    set_time_limit(0);//设置脚本执行时间无限长
  	$flie="flexbuilder_linux_install_a5_112409.bin";
    switch ($imgtype){
        case 1:
         $fp=fopen($filepase,'rb');
         $image_head = fread($fp,10737418240);
            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,$filepase,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,$filepase,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,$filepase,100);
            imagedestroy($img_wp);
            break;
    }
  }

阅读更多
想对作者说点什么?

博主推荐

换一批

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