zoukankan      html  css  js  c++  java
  • php图片压缩两个类

    不是压缩尺寸,是图片的占用空间,第二个比较好用

    1.

    
    
    class image_blur{
      
    /** 
         * 图片高斯模糊(适用于png/jpg/gif格式) 
         * @param $srcImg 原图片 
         * @param $savepath 保存路径 
         * @param $savename 保存名字 
         * @param $positon 模糊程度  
         * 
         *基于Martijn Frazer代码的扩充, 感谢 Martijn Frazer 
         */  
        public function gaussian_blur($srcImg,$savepath=null,$savename=null,$blurFactor=3){  
            $gdImageResource=$this->image_create_from_ext($srcImg);  
            $srcImgObj=$this->blur($gdImageResource,$blurFactor);  
            $temp = pathinfo($srcImg);  
            $name = $temp['basename'];  
            $path = $temp['dirname'];  
            $exte = $temp['extension'];  
            $savename = $savename ? $savename : $name;  
            $savepath = $savepath ? $savepath : $path;  
            $savefile = $savepath .'/'. $savename;  
            $srcinfo = @getimagesize($srcImg);  
              
            switch ($srcinfo[2]) {  
                case 1: imagegif($srcImgObj, $savefile); break;  
                case 2: imagejpeg($srcImgObj, $savefile); break;  
                case 3: imagepng($srcImgObj, $savefile); break;  
                default: return '保存失败'; //保存失败  
            }  
      
            return $savefile;  
            imagedestroy($srcImgObj);  
        }  
      
        /** 
        * Strong Blur 
        * 
        * @param  $gdImageResource  图片资源 
        * @param  $blurFactor          可选择的模糊程度  
        *  可选择的模糊程度  0使用   3默认   超过5时 极其模糊 
        * @return GD image 图片资源类型 
        * @author Martijn Frazer, idea based on http://stackoverflow.com/a/20264482 
        */  
        private function blur($gdImageResource, $blurFactor = 3)  
        {  
            // 模糊因子必须是一个整数 
            $blurFactor = round($blurFactor);  
      
            $originalWidth = imagesx($gdImageResource);  
            $originalHeight = imagesy($gdImageResource);  
      
            $smallestWidth = ceil($originalWidth * pow(0.5, $blurFactor));  
            $smallestHeight = ceil($originalHeight * pow(0.5, $blurFactor));  
      
            // for the first run, the previous image is the original input  
            $prevImage = $gdImageResource;  
            $prevWidth = $originalWidth;  
            $prevHeight = $originalHeight;  
      
            // scale way down and gradually scale back up, blurring all the way  
            for($i = 0; $i < $blurFactor; $i += 1)  
            {      
                // determine dimensions of next image  
                $nextWidth = $smallestWidth * pow(2, $i);  
                $nextHeight = $smallestHeight * pow(2, $i);  
      
                // resize previous image to next size  
                $nextImage = imagecreatetruecolor($nextWidth, $nextHeight);  
                imagecopyresized($nextImage, $prevImage, 0, 0, 0, 0,   
                  $nextWidth, $nextHeight, $prevWidth, $prevHeight);  
      
                // apply blur filter  
                imagefilter($nextImage, IMG_FILTER_GAUSSIAN_BLUR);  
      
                // now the new image becomes the previous image for the next step  
                $prevImage = $nextImage;  
                $prevWidth = $nextWidth;  
                $prevHeight = $nextHeight;  
            }  
      
            // scale back to original size and blur one more time  
            imagecopyresized($gdImageResource, $nextImage,   
            0, 0, 0, 0, $originalWidth, $originalHeight, $nextWidth, $nextHeight);  
            imagefilter($gdImageResource, IMG_FILTER_GAUSSIAN_BLUR);  
      
            // clean up  
            imagedestroy($prevImage);  
      
            // return result  
            return $gdImageResource;  
        }  
      
        private function image_create_from_ext($imgfile)  
        {  
            $info = getimagesize($imgfile);  
            $im = null;  
            switch ($info[2]) {  
            case 1: $im=imagecreatefromgif($imgfile); break;  
            case 2: $im=imagecreatefromjpeg($imgfile); break;  
            case 3: $im=imagecreatefrompng($imgfile); break;  
            }  
            return $im;  
        }  
      
    } 

    2.

    class Compress
    {
        private $src;
        private $image;
        private $imageinfo;
        private $percent=0.5;
        
        /*
        param    $src源图
        param    $percent压缩比例
        */
        public function __construct($src,$percent=1)
        {
            $this->src = $src;
            $this->percent = $percent;
        }
        
        
        
        /*
        param string $saveName 图片名(可不带扩展名用原图名)用于保存。或不提供文件名直接显示
        */
        public function compressImg($saveName='')
        {
            $this->_openImage();
            if(!empty($saveName))
            {
                $this->_saveImage($saveName);//保存
            }
            else
            {
                $this->_showImage();
            }
        }
        
        
        
        
        /*
        内部:打开图片
        */
        private function _openImage()
        {
            list($width, $height, $type, $attr) = getimagesize($this->src);
            $this->imageinfo = array(
                'width'=>$width,
                'height'=>$height,
                'type'=>image_type_to_extension($type,false),
                'attr'=>$attr
              );
            $fun = "imagecreatefrom".$this->imageinfo['type'];
            $this->image = $fun($this->src);
            $this->_thumpImage();
        }
        
        
        
        
        
        /**
        * 内部:操作图片
        */
        private function _thumpImage()
        {
            $new_width = $this->imageinfo['width'] * $this->percent;
            $new_height = $this->imageinfo['height'] * $this->percent;
            $image_thump = imagecreatetruecolor($new_width,$new_height);
            //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
            imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
            imagedestroy($this->image);
            $this->image = $image_thump;
        }
        
        
        
        
        
        /**
        * 输出图片:保存图片则用saveImage()
        */
        private function _showImage()
        {
            header('Content-Type: image/'.$this->imageinfo['type']);
            $funcs = "image".$this->imageinfo['type'];
            $funcs($this->image);
        }
        
        
        
        
        
        /**
        * 保存图片到硬盘:
        * @param  string $dstImgName  1、可指定字符串不带后缀的名称,使用源图扩展名 。2、直接指定目标图片名带扩展名。
        */
        private function _saveImage($dstImgName)
        {
            if(empty($dstImgName)) return false;
            $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif'];   //如果目标图片名有后缀就用目标图片扩展名 后缀,如果没有,则用源图的扩展名
            $dstExt =  strrchr($dstImgName ,".");
            $sourseExt = strrchr($this->src ,".");
            if(!empty($dstExt)) $dstExt =strtolower($dstExt);
            if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);
         
            //有指定目标名扩展名
            if(!empty($dstExt) && in_array($dstExt,$allowImgs))
            {
                $dstName = $dstImgName;
            }
            elseif(!empty($sourseExt) && in_array($sourseExt,$allowImgs))
            {
                $dstName = $dstImgName.$sourseExt;
            }
            else
            {
                $dstName = $dstImgName.$this->imageinfo['type'];
            }
            $funcs = "image".$this->imageinfo['type'];
            $funcs($this->image,$dstName);
        }
     
     
     
     
     
        /**
        * 销毁图片
        */
        public function __destruct()
        {
           imagedestroy($this->image);
        }
    }
    
    $source = 'test.jpg';
    $dst_img = 'test.jpg';
    $percent = 1;  #原图压缩,不缩放
    $image = (new Compress($source,$percent))->compressImg($dst_img);
  • 相关阅读:
    【Qt开发】 V4L2_CAP_VIDEO_OVERLAY与V4L2_CAP_VIDEO_CAPTURE的区别
    【Qt开发】QThread 实用技巧、误区----但文档中没有提到
    【Qt开发】QThread 实用技巧、误区----但文档中没有提到
    【Qt开发】事件循环与线程 二
    【Qt开发】事件循环与线程 二
    【Qt开发】事件循环与线程 一
    【Qt开发】事件循环与线程 一
    【Qt开发】QThread介绍
    【Qt开发】QThread介绍
    【Linux开发】如何查看Linux kernel的内置模块驱动列表和进程ID
  • 原文地址:https://www.cnblogs.com/zhangxiangdong/p/7743870.html
Copyright © 2011-2022 走看看