上传图片压缩与删除该文件加下的全部文件

上传图片压缩以及删除该文件加下的全部文件 拿来就用系列!!!

图片压缩以及裁剪

方法

<?php  
namespace app\index\controller;
use app\BaseController;

    /** 
    
    * desription 压缩图片 
    
    * @param sting $imgsrc 图片路径 
    
    * @param string $imgdst 压缩后保存路径 
    
    */
class Imgsize extends BaseController{
    
    public  function imgsizeadd($imgsrc,$imgadd,$percent=1){  

        list($width,$height,$type)=getimagesize($imgsrc);  
        
        /* $new_width = ($width>600?600:$width)*0.9;  
        
         $new_height =($height>600?600:$height)*0.9;  */
        $new_width = $width * $percent; 
        $new_height = $height * $percent; 
        switch($type){  
        
            case 1:  
        
            $giftype=$this->checkgifcartoon($imgsrc); 
        
            $image_wp=imagecreatetruecolor($new_width, $new_height);  
        
            $image = imagecreatefromgif($imgsrc);  
        
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);  
        
            imagejpeg($image_wp, $imgadd,75);  
        
            imagedestroy($image_wp);  
        
            break;  
        
            case 2:  
        
            $image_wp=imagecreatetruecolor($new_width, $new_height);  
        
            $image = imagecreatefromjpeg($imgsrc);  
        
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);  
        
            imagejpeg($image_wp, $imgadd,75);  
        
            imagedestroy($image_wp);  
        
            break;  
        
            case 3:  
        
            $image_wp=imagecreatetruecolor($new_width, $new_height);  
        
            $image = imagecreatefrompng($imgsrc);  
        
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);  
        
            imagejpeg($image_wp, $imgadd,75);  
        
            imagedestroy($image_wp);  
        
            break;  
        
         }  

    }  

    /** 
    
    * desription 判断是否gif动画 
    
    * @param sting $image_file图片路径 
    
    * @return boolean t 是 f 否 
    
    */

    public function checkgifcartoon($image_file){  
    
         $fp = fopen($image_file,'rb');  
        
         $image_head = fread($fp,1024);  
        
         fclose($fp);  
        
         return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true;  
    
    }  


    public function imgcompress($file,$percent=1){
        
        header("Content-type: image/jpeg"); 
    
        $percent = 1; //图片压缩比 
        
        list($width, $height) = getimagesize($file); //获取原图尺寸 
         
        
        //缩放尺寸 
        
        $newwidth = $width * $percent; 
        
        $newheight = $height * $percent; 
        
        $src_im = imagecreatefromjpeg($file); 
        
        $dst_im = imagecreatetruecolor($newwidth, $newheight); 
        
        imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
        
        imagejpeg($dst_im,$file); //输出压缩后的图片 
        
        imagedestroy($dst_im); 
        
        imagedestroy($src_im);
        
        return true;
    }
}

?>

调用

//引入命名
use app\index\controller\Imgsize;

//调用以tp6为例
$imgsize=new Imgsize($this->app);

//压缩图片$imgsrc原图片路径, $imgsrc2新图片目的路径               
$imgsize->imgsizeadd($imgsrc,$imgsrc2,0.5);

//裁剪图片尺寸
$imgsize->imgcompress($video_img,1);

删除该文件加下的全部文件

public function deldir($dir) {
      //先删除目录下的文件:
      $dh=opendir($dir);
      while ($file=readdir($dh)) {
        if($file!="." && $file!="..") {
          $fullpath=$dir."/".$file;
          if(!is_dir($fullpath)) {
              unlink($fullpath);
          } else {
              
              $this->deldir($fullpath);
          }
        }
      }
     
      closedir($dh);
      //删除当前文件夹:
     
     if(rmdir($dir)) {
        return true;
     } else {
        return false;
     }
     
}
$this->deldir(root_path().'public/static/daka/uploads');

0 评论
内联反馈
查看所有评论