等比例压缩图片到指定的KB大小

基本原理:

取原来的图片,长宽乘以比例,重新生成一张图片,获取这张图片的大小,如果还是超过预期大小,继续在此基础上乘以压缩比例,生成图片,直到达到预期

 1     /**
 2      * @获取远程图片的体积大小 单位byte
 3      * @date 2016/9/23
 4      * @author Jimmy
 5      * @param $uri
 6      * @param string $user
 7      * @param string $pw
 8      * @return string
 9      */
10     public static  function remoteFilesize($uri, $user='', $pw='')
11     {
12         ob_start();
13         $ch = curl_init($uri);
14         curl_setopt($ch, CURLOPT_HEADER, 1);
15         curl_setopt($ch, CURLOPT_NOBODY, 1);
16         if (!empty($user) && !empty($pw)) {
17             $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
18             curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
19         }
20         curl_exec($ch);
21         curl_close($ch);
22         $head = ob_get_contents();
23         ob_end_clean();
24         $regex = '/Content-Length:s([0-9].+?)s/';
25         preg_match($regex, $head, $matches);
26         return isset($matches[1]) ? $matches[1] : 'unknown';
27     }
 1     /**
 2      * @desc 等比例压缩图片到指定KB大小
 3      * @author Jimmy
 4      * @date 2016/9/26
 5      * @param $url 远程图片地址
 6      * @param $maxSize 需要压缩的最终适合的大小KB
 7      * @param $dirPath 文件存放的目录
 8      * @param float $ratio 文件压缩系数(大于0小于1),该值越大,得到的压缩图片约接近指定的KB大小
 9      * @return bool|string 压缩后文件存放路径
10      */
11     public static function compressImageToSize($url,$maxSize,$dirPath,$ratio=0.9){
12         $fileLength=UtilityHelper::remoteFilesize($url);
13         $originSize=getimagesize($url);
14         $originWidth=$originSize[0];
15         $originHeight=$originSize[1];
16         $varWidth = $originWidth;
17         $varHeight = $originHeight;
18         if(!file_exists($dirPath)){
19             mkdir($dirPath);
20         }
21         $desPath = $dirPath. Uuid::createUuid().'.jpg';
22         if($fileLength/1024.0>$maxSize){//需要等比例压缩处理
23             while(true){
24                 $currentWidth  = intval($varWidth*$ratio);
25                 $currentHeight = intval($varHeight*$ratio);
26                 $varWidth = $currentWidth;
27                 $varHeight = $currentHeight;
28                 //生成缩略图片
29                 $im=imagecreatefromjpeg($url);
30                 $tn=imagecreatetruecolor($currentWidth, $currentHeight);
31                 imagecopyresampled($tn, $im, 0, 0, 0, 0, $currentWidth, $currentHeight, $originWidth, $originHeight);
32                 file_exists($desPath)&&unlink($desPath);
33                 imagejpeg($tn, $desPath,100);
34                 imagedestroy($tn);
35                 $length=filesize($desPath)/1024.0;
36                 if($length<$maxSize){
37                     break;
38                 }
39             }
40         }else{
41             file_put_contents($desPath,file_get_contents($url),true);
42         }
43         if(empty($desPath)){
44             return false;
45         }else{
46             return $desPath;
47         }
48     }
原文地址:https://www.cnblogs.com/JimmyBright/p/5956392.html