quocvuongdn

#php: Imagemagick resize image

Jun 5th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.31 KB | None | 0 0
  1. public function compress_image(){
  2.       if($_POST['image']){         
  3.         //Lấy đường dẫn hình
  4.           $img = $_POST['image'];
  5.           $path = $this->img_cfg['root_path'];
  6.           $mCfg = null;
  7.           //Cắt đường dẫn hình
  8.           $imgExp = explode("/", $img);
  9.  
  10.           //Nếu không rỗng thì tiến hành lấy folder là avatars hoặc shares
  11.           if(!empty($imgExp)){
  12.           $objImg = new Imagick($path.$img);
  13.             if(strtolower($imgExp[1]) == 'avatars'){
  14.             $mCfg = $this->img_cfg['size']['avatar'];
  15.             } elseif(strtolower($imgExp[1]) == 'shares'){
  16.             $mCfg = $this->img_cfg['size']['share'];
  17.             }
  18.           $mCfg= array_reverse($mCfg);
  19.           foreach ($mCfg as $key => $value) {
  20.             $oSize = explode(",", $value);
  21.             $w = $oSize[0];
  22.             $h = $oSize[1];
  23.             $nName = $key.'_'.$imgExp[3];
  24.             $objImg->resizeImage($w, $h, Imagick::IMGTYPE_TRUECOLOR , 1);
  25.             $objImg->thumbnailImage($w, $h);
  26.             $objImg->setImageCompressionQuality(100);
  27.             $newPath = $path.$imgExp[0].'/'.$imgExp[1].'/'.$imgExp[2].'/'.$nName;
  28.             $objImg->writeImageFile(fopen($newPath, "wb"));
  29.             //$objImg = new Imagick($newPath);
  30.           }
  31.           $objImg->destroy();
  32.  
  33.           }
  34.       }
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment