Exemple #1
0
 /**
  * Resize image
  *
  * @param  string  $img  image path
  * @param  int     $w    image width
  * @param  int     $h    image height
  * @return bool
  **/
 protected function _resizeImg($img, $w, $h)
 {
     if (false == ($s = getimagesize($img))) {
         return false;
     }
     switch ($this->_options['imgLib']) {
         case 'imagick':
             if (false != ($_img = new imagick($img))) {
                 return $_img->cropThumbnailImage($w, $h) && $_img->writeImage($img);
             }
             break;
         case 'mogrify':
             exec('mogrify -scale ' . $w . 'x' . $h . '! ' . escapeshellarg($img), $o, $c);
             return 0 == $c;
             break;
         case 'gd':
             /*if ($s['mime'] == 'image/jpeg') {
             			$_img = imagecreatefromjpeg($img);
             		} elseif ($s['mime'] == 'image/png') {
             			$_img = imagecreatefrompng($img);
             		} elseif ($s['mime'] == 'image/gif') {
             			$_img = imagecreatefromgif($img);
             		} 
             		if (!$_img || false == ($_out = imagecreatetruecolor($w, $h))) {
             			return false;
             		}
             		if (!imagecopyresampled($_out, $_img, 0, 0, 0, 0, $w, $h, $s[0], $s[1])) {
             			return false;
             		}
             		if ($s['mime'] == 'image/jpeg') {
             			$r = imagejpeg($_out, $img, 100);
             		} else if ($s['mime'] == 'image/png') {
             			$r = imagepng($_out, $img, 7);
             		} else {
             			$r = imagegif($_out, $img, 7);
             		}
             		imagedestroy($_img);
             		imagedestroy($_out);
             		return $r;*/
             $res = new JO_Thumb($img);
             $res->resize($w, $h);
             return $res->save($img);
             break;
     }
 }
Exemple #2
0
 private function resizeForEditor($filename, $width = false, $height = false)
 {
     $filename = preg_replace('/uploads\\//i', '/', $filename);
     if (!file_exists($this->dirImages . $filename) || !is_file($this->dirImages . $filename)) {
         return false;
     }
     $imag_info = @getimagesize($this->dirImages . $filename);
     if (!$imag_info) {
         return;
     }
     $info = pathinfo($filename);
     $extension = $info['extension'];
     if ($height && !$width) {
         $width = round($imag_info[0] / ($imag_info[1] / $height));
     } elseif ($width && !$height) {
         $height = round($imag_info[1] / ($imag_info[0] / $width));
     }
     if (!$width || !$height) {
         return false;
     }
     $old_image = $filename;
     $tmp = substr($filename, 0, strrpos($filename, '.'));
     $filename = substr($tmp, 0, strrpos($tmp, '/')) . '/' . md5(basename($tmp)) . '-' . md5($filename);
     $new_image = 'cache' . $filename . '-' . $width . 'x' . $height . '.' . $extension;
     $new_image = str_replace('/../', '/', $new_image);
     //$new_image = 'cache' . substr($filename, 0, strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
     if (!file_exists($this->dirImages . $new_image) || filemtime($this->dirImages . $old_image) > filemtime($this->dirImages . $new_image)) {
         $path = '';
         $directories = explode('/', dirname(str_replace('../', '', $new_image)));
         foreach ($directories as $directory) {
             $path = $path . '/' . $directory;
             if (!file_exists($this->dirImages . $path)) {
                 @mkdir($this->dirImages . $path, 0777, true);
             }
         }
         $image = new JO_Thumb($this->dirImages . $old_image);
         $image->resize($width, $height, false);
         $image->save($this->dirImages . $new_image);
     }
     return $this->httpImages . $new_image;
 }