首页 正文

php上传图片后压缩代码

       884      2017-11-30    

最近在使用ci框架需要上传一些图片,之前都是上什么图片就显示什么图片,慢慢的发现这样占用的空间太大了,比如上传一张照片就好几兆,对带宽的需求也很大。百度了一下找到以下方法并且在CI上使用,特此记录下来。

下面是上传文件的基本逻辑

//上传文件路径
          $path = 'uploads/'.$_SESSION["UPLOADCONTROLLER"].'/title_img/';    //图片存放根目录 根据自己项目路径而定
          $file = $_FILES['download']['name'];
          $folder = $path.date('Ymd')."/";
          $pre = rand(999,9999).time();
          $ext = strrchr($file,'.');
          $newName = $pre.$ext;
          
          
          $out = ['msg'=>'','status'=>'error','img_url'=>''];
          
          
          if(!is_dir(FCPATH.$folder))
          {
              if(!mkdir(FCPATH.$folder, 0777, true)){
                  $out['msg'] = '图片目录创建失败!';
                  echo json_encode($out);
                  exit;
              }
          }
          $im = $_FILES['download']['tmp_name']; //上传图片资源
          $maxwidth="1056"; //设置图片的最大宽度
          $maxheight="500"; //设置图片的最大高度
          $imgname = $folder.$newName;  //图片存放路径 根据自己图片路径而定
          $filetype=$_FILES['download']['type'];//图片类型
          $result = $this->thumbImage($im,$maxwidth,$maxheight,$imgname,$filetype);
          if($result){
              $msg = '图片上传成功';
              $status = 'success';
              $out['img_url'] = $this->config->item('ftpHostname').$folder.$newName;
          }else{
             $msg = '图片上传失败';
          }
          
          $status = 1;
          $msg = "上传成功";

下面是压缩文件处理的主要方法,特此记录下来。

    //压缩图片
  private function thumbImage($im,$maxwidth,$maxheight,$name,$filetype)
  {
      switch ($filetype) {
          case 'image/pjpeg':
          case 'image/jpeg':
              $im = imagecreatefromjpeg($im);    //PHP图片处理系统函数
              break;
          case 'image/gif':
              $im = imagecreatefromgif($im);
              break;
          case 'image/png':
              $im = imagecreatefrompng($im);
              break;
          case 'image/wbmp':
              $im = imagecreatefromwbmp($im);
              break;
      }
  
  
      $resizewidth_tag = $resizeheight_tag = false;
      $pic_width = imagesx($im);
      $pic_height = imagesy($im);
  
  
      if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
      {
          $resizewidth_tag = $resizeheight_tag = false;
  
          if($maxwidth && $pic_width>$maxwidth)
          {
              $widthratio = $maxwidth / $pic_width;
              $resizewidth_tag = true;
          }
  
  
          if($maxheight && $pic_height>$maxheight)
          {
              $heightratio = $maxheight / $pic_height;
              $resizeheight_tag = true;
          }
  
  
          if($resizewidth_tag && $resizeheight_tag)
          {
              if($widthratio < $heightratio)
                  $ratio = $widthratio;
              else
                  $ratio = $heightratio;
          }
  
  
          if($resizewidth_tag && !$resizeheight_tag)
              $ratio = $widthratio;
  
  
          if($resizeheight_tag && !$resizewidth_tag)
              $ratio = $heightratio;
  
  
          $newwidth = $pic_width * $ratio;
          $newheight = $pic_height * $ratio;
  
  
  
          if(function_exists("imagecopyresampled"))
          {
              $newim = imagecreatetruecolor($newwidth,$newheight);//PHP图片处理系统函数
              imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP图片处理系统函数
          }
          else
          {
              $newim = imagecreate($newwidth,$newheight);
              imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
          }
  
  
          switch ($filetype) {
              case 'image/pjpeg' :
              case 'image/jpeg' :
                  $result = imagejpeg($newim,$name);
                  break;
              case 'image/gif' :
                  $result = imagegif($newim,$name);
                  break;
              case 'image/png' :
                  $result = imagepng($newim,$name);
                  break;
              case 'image/wbmp' :
                  $result = imagewbmp($newim,$name);
                  break;
          }
          imagedestroy($newim);
      }
      else
      {
          switch ($filetype) {
              case 'image/pjpeg' :
              case 'image/jpeg' :
                  $result = imagejpeg($im,$name);
                  break;
              case 'image/gif' :
                  $result = imagegif($im,$name);
                  break;
              case 'image/png' :
                  $result = imagepng($im,$name);
                  break;
              case 'image/wbmp' :
                  $result = imagewbmp($im,$name);
                  break;
          }
      }
      return $result; 
  }


我要打赏