thinkphp5 上传图片压缩

1、Compress.php(该文件放在vendor文件夹中)

  1 <?php
  2 class Compress
  3 {
  4     private $src;
  5     private $image;
  6     private $imageinfo;
  7     private $percent=0.5;
  8     
  9     /*
 10     param    $src源图
 11     param    $percent压缩比例
 12     */
 13     public function __construct($src,$percent=1)
 14     {
 15         $this->src = $src;
 16         $this->percent = $percent;
 17     }
 18     
 19     
 20     
 21     /*
 22     param string $saveName 图片名(可不带扩展名用原图名)用于保存。或不提供文件名直接显示
 23     */
 24     public function compressImg($saveName='')
 25     {
 26         $this->_openImage();
 27         if(!empty($saveName))
 28         {
 29             $this->_saveImage($saveName);//保存
 30         }
 31         else
 32         {
 33             $this->_showImage();
 34         }
 35     }
 36     
 37     
 38     
 39     
 40     /*
 41     内部:打开图片
 42     */
 43     private function _openImage()
 44     {
 45         list($width, $height, $type, $attr) = getimagesize($this->src);
 46         $this->imageinfo = array(
 47             'width'=>$width,
 48             'height'=>$height,
 49             'type'=>image_type_to_extension($type,false),
 50             'attr'=>$attr
 51           );
 52         $fun = "imagecreatefrom".$this->imageinfo['type'];
 53         $this->image = $fun($this->src);
 54         $this->_thumpImage();
 55     }
 56     
 57     
 58     
 59     
 60     
 61     /**
 62     * 内部:操作图片
 63     */
 64     private function _thumpImage()
 65     {
 66         $new_width = $this->imageinfo['width'] * $this->percent;
 67         $new_height = $this->imageinfo['height'] * $this->percent;
 68         $image_thump = imagecreatetruecolor($new_width,$new_height);
 69         //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
 70         
 71         /*  处理缩放png图透明背景变黑色问题 start  */        
 72         $color=imagecolorallocate($image_thump,255,255,255); 
 73         imagecolortransparent($image_thump,$color); 
 74         imagefill($image_thump,0,0,$color); 
 75         /*  处理缩放png图透明背景变黑色问题 end  */
 76         
 77         imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
 78         imagedestroy($this->image);
 79         $this->image = $image_thump;
 80     }
 81     
 82     
 83     
 84     
 85     
 86     /**
 87     * 输出图片:保存图片则用saveImage()
 88     */
 89     private function _showImage()
 90     {
 91         header('Content-Type: image/'.$this->imageinfo['type']);
 92         $funcs = "image".$this->imageinfo['type'];
 93         $funcs($this->image);
 94     }
 95     
 96     
 97     
 98     
 99     
100     /**
101     * 保存图片到硬盘:
102     * @param  string $dstImgName  1、可指定字符串不带后缀的名称,使用源图扩展名 。2、直接指定目标图片名带扩展名。
103     */
104     private function _saveImage($dstImgName)
105     {
106         if(empty($dstImgName)) return false;
107         $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif'];   //如果目标图片名有后缀就用目标图片扩展名 后缀,如果没有,则用源图的扩展名
108         $dstExt =  strrchr($dstImgName ,".");
109         $sourseExt = strrchr($this->src ,".");
110         if(!empty($dstExt)) $dstExt =strtolower($dstExt);
111         if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);
112      
113         //有指定目标名扩展名
114         if(!empty($dstExt) && in_array($dstExt,$allowImgs))
115         {
116             $dstName = $dstImgName;
117         }
118         elseif(!empty($sourseExt) && in_array($sourseExt,$allowImgs))
119         {
120             $dstName = $dstImgName.$sourseExt;
121         }
122         else
123         {
124             $dstName = $dstImgName.$this->imageinfo['type'];
125         }
126         $funcs = "image".$this->imageinfo['type'];
127         $funcs($this->image,$dstName);
128     }
129  
130  
131  
132  
133  
134     /**
135     * 销毁图片
136     */
137     public function __destruct()
138     {
139        imagedestroy($this->image);
140     }
141 }
View Code

2、common.php 文件

 1 <?php
 2 // 应用公共文件
 3 
 4 function ajaxUploader(){
 5     $file = request()->file('file');
 6 
 7     $info = $file->validate(['size'=>20971520,'ext'=>'jpg,png,gif,jpeg'])->move('./UploadFiles_s');
 8     
 9     if($info){
10         require_once(Env::get('root_path') . 'vendor/Qrcode/Compress.php');
11         
12         $image = $info->getSaveName(); 
13         $source = $_SERVER['DOCUMENT_ROOT'] .'/UploadFiles_s/' . $image;
14         $dst_img = $_SERVER['DOCUMENT_ROOT'] .'/UploadFiles_s/' . $image;
15         
16         $percent = 0.6;  #缩放比例
17         $image = (new \Compress($source,$percent))->compressImg($dst_img);
18 
19         // 成功上传后 获取上传信息
20         $file_info = $info->getInfo();
21         $response = [
22             'code' => 0,
23             'success' => true,
24             'oldName' => $info->getFilename(),
25             'fileSuffixes' => $info->getExtension(),
26             'saveName' => $info->getSaveName(),
27             'file_info' => $file_info
28         ];
29     }else{
30         $response = [
31             'code' => 1,
32             'success' => false
33         ];
34     }
35     die(json_encode($response));
36 }

 

posted @ 2020-09-27 10:06  糖糖Y  阅读(963)  评论(0编辑  收藏  举报