Parcourir la source

图片压缩的处理上传

zxb il y a 5 ans
Parent
commit
dbad67b063

+ 72 - 14
app/admin/controller/ImgController.php

@@ -24,17 +24,18 @@ class ImgController {
      */
     public function compressedImage($imgsrc, $imgdst) {  
         
-        list($width, $height, $type) = getimagesize($imgsrc);
+        list($width, $height, $type) = getimagesize($imgsrc);    //list可以将返回的数组值赋值给变量
        
+
         $new_width = $width;//压缩后的图片宽
         $new_height = $height;//压缩后的图片高
- 
-        if($width >= 600){
-            $per = 600 / $width;//计算比例
-            $new_width = $width * $per;
-            $new_height = $height * $per;
-        }
-      // echo $type.':'.$width.":".$height;die();
+ // echo $new_width;echo "<br/>";echo $new_height;echo "<br/>";
+        // if($width >= 600){
+        //     $per = 600 / $width;//计算比例
+        //     $new_width = $width * $per;
+        //     $new_height = $height * $per;
+        // }
+ // echo $new_width;echo "<br/>";echo $new_height;die();
         switch ($type) {
             case 1:
                 $giftype = check_gifcartoon($imgsrc);
@@ -44,33 +45,90 @@ class ImgController {
                     $image = imagecreatefromgif($imgsrc);
                     imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                     //90代表的是质量、压缩图片容量大小
-                    imagejpeg($image_wp, $imgdst, 100);
+                    imagejpeg($image_wp, $imgdst, 90);
                     imagedestroy($image_wp);
                     imagedestroy($image);
                 }
                 break;
             case 2:
-                header('Content-Type:image/jpeg');
+                 // header('Content-Type:image/png');echo 'fdsfsfs111';die();
                 $image_wp = imagecreatetruecolor($new_width, $new_height);
-                $image = imagecreatefromjpeg($imgsrc);
+                $image = imagecreatefromjpeg($imgsrc); 
                 imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                 //90代表的是质量、压缩图片容量大小
-                imagejpeg($image_wp, $imgdst, 100);
+                imagejpeg($image_wp, $imgdst, 90);
                 imagedestroy($image_wp);
                 imagedestroy($image);
                 break;
             case 3:
-                header('Content-Type:image/png');
+            //    header('Content-Type:image/png');
                 $image_wp = imagecreatetruecolor($new_width, $new_height);
                 $image = imagecreatefrompng($imgsrc);
                 imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                 //90代表的是质量、压缩图片容量大小
-                imagejpeg($image_wp, $imgdst, 100);
+                imagejpeg($image_wp, $imgdst, 50);
                 imagedestroy($image_wp);
                 imagedestroy($image);
                 break;
         }
     }
 
+
+     /**
+     * 缩略图
+     * @param $file
+     * @param int $type
+     * @return $this
+     */
+    public static function imageThumb($file, $type=1, $params=[]){
+        $siteInfo = cmf_get_site_info();
+        $thumbWidth = isset($siteInfo['thumb_width'])? intval($siteInfo['thumb_width']) : 0;
+        $thumbHeight = isset($siteInfo['thumb_height'])? intval($siteInfo['thumb_height']) : 0;
+
+        $width = isset($params['width'])? $params['width'] : 0;
+        $height = isset($params['height'])? $params['height'] : 0;
+        $thumbWidth = $thumbWidth>600? $thumbWidth : 600;
+        $thumbHeight = $thumbHeight>600? $thumbHeight : 600;
+        $thumbWidth = $width? $width : $thumbWidth;
+        $thumbHeight = $height? $height : $thumbHeight;
+        // 要加水印的图片
+        $realFilename = 'upload/' . $file;
+        if (!file_exists($realFilename)) {
+            return false;
+        }
+
+        $image = \think\Image::open($realFilename);
+        $filepath = dirname($realFilename);
+        $paths = explode('_',basename($realFilename));
+        $filename = end($paths);
+        $thumbfile = $filepath . '/thumb_' . $thumbWidth . '_' . $thumbHeight . '_' . $type . '_' . $filename;
+        $image->thumb($thumbWidth, $thumbHeight, $type)->save($thumbfile);
+        return preg_replace("/^upload\//", '', $thumbfile);
+    }
+
+    //压缩图片2
+
+      public static function imageThumb2($imgsrc, $imgdst, $type=3){
+       
+        list($width, $height, $type) = getimagesize($imgsrc);
+       
+        $new_width = $width;//压缩后的图片宽
+        $new_height = $height;//压缩后的图片高
+ 
+        // if($width >= 600){
+        //     $per = 600 / $width;//计算比例
+        //     $new_width = $width * $per;
+        //     $new_height = $height * $per;
+        // }
+
+        $image = \think\Image::open($imgsrc);
+     //   $filepath = dirname($imgsrc);   //不需要获取根目录
+        //$paths = explode('_',basename($realFilename));
+        //$filename = end($paths);
+      //
+        $image->thumb($new_width, $new_height, $type)->save($imgdst);
+        return preg_replace("/^upload\//", '', $imgdst);
+    }
+
 }
 ?>

+ 24 - 5
app/admin/controller/JiamengController.php

@@ -139,16 +139,33 @@ class JiamengController extends AdminBaseController{
     }
 
     public function addPost(){
+
+        // $sddsfs="upload/admin/20200623/e982ca9ae7b1a017fb1145e4e05fea9c.png";
+        // echo preg_replace('/^upload\//','',$sddsfs);
+        // $string = 'google 123, 456';
+        // $pattern = '/(\w+) (\d+), (\d+)/i';
+        // $replacement = 'runoob ${2},$3';
+        // echo preg_replace($pattern, $replacement, $string);
+       // die();
         // $onepic='.\upload\admin\20191224\d0462810bfc159fd8b16998571c2023.png';
-        ////$img=new ImgController(); 
+         $img=new ImgController(); 
         // $two='.\upload\admin\20191224\d0462810bfc159fd8b16998571c2023.png';
         // $img->compressedImage($onepic,$two);
         // die();
-       
          // echo file_exists('./upload/admin/20191224/d0462810bfc159fd8b16998571c2023.png').'sdfsf';die;
-         //// $param = $this->request->param();
-         //// $imgsrc="./upload/".$param['thumb'];
-         //// $img->compressedImage($imgsrc,$imgsrc);
+         $param = $this->request->param();
+         // $imgsrc="./upload/".$param['thumb'];
+         
+         $imgsrc="./upload/admin/20200720/20200720090354.png";
+        // $imgsrc="./upload/admin/20200720/5a6a8b37372fd2f74790540687b94fd0.jpg";
+
+      // echo $imgsrc;die();
+        $img->compressedImage($imgsrc,$imgsrc); 
+         //$img->imageThumb2($imgsrc,$imgsrc,1);
+
+
+         die();
+         // die();
          //echo file_exists($img);echo $img;
         //var_dump($param);die();
         isset($param['thumb'])&&$param['thumb']!='' or $this->error('请上传标题图片');
@@ -175,6 +192,8 @@ class JiamengController extends AdminBaseController{
             $param['author']=$this->uname;
         }
 
+        //进行图片的上传压缩
+
         $res = Db::name('Jiameng')->insertGetId($param);
         $data['id'] = $res;
         Db::name('Jiameng_data')->insert($data);