Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 1.79 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.      * Resizes current image and returns self
  3.      *
  4.      * @param integer $width
  5.      * @param integer $height
  6.      *
  7.      * @throws InvalidArgumentException
  8.      * @throws RuntimeException
  9.      *
  10.      * @return Image_Image
  11.      */
  12.     public function resize($width, $height, $mode = self::RESIZE_INSET)
  13.     {
  14.         if ($width < 1 || $height < 1) {
  15.             throw new InvalidArgumentException(
  16.                 'Width and height of the resize must be positive integers'
  17.             );
  18.         }
  19.  
  20.         try {
  21.             $size = $this->getSize();
  22.  
  23.             switch ($mode) {
  24.                 case self::RESIZE_INSET:
  25.                     $ratio = min($width/$size['width'], $height/$size['height']);
  26.                     break;
  27.  
  28.                 case self::RESIZE_OUTBOUND:
  29.                     $ratio = max($width/$size['width'], $height/$size['height']);
  30.                     break;
  31.  
  32.                 default:
  33.                     throw new InvalidArgumentException(
  34.                         'Invalid mode specified'
  35.                     );
  36.             }
  37.  
  38.             $resized = $this->copy();
  39.            
  40.             foreach ($resized->imagick as $frame) {
  41.                 $page = $frame->getImagePage();
  42.  
  43.                 $w = intval($frame->getImageWidth() * $ratio);
  44.                 $h = intval($frame->getImageHeight() * $ratio);
  45.                 $x = intval($page['x'] * $ratio);
  46.                 $y = intval($page['y'] * $ratio);
  47.  
  48.                 $frame->resizeImage($w, $h, Imagick::FILTER_LANCZOS, 0);
  49.                 $frame->setImagePage($w, $h, $x, $y);
  50.             }
  51.         } catch (ImagickException $e) {
  52.             throw new RuntimeException(
  53.                 'Resize operation failed', $e->getCode()
  54.             );
  55.         }
  56.  
  57.         return $resized;
  58.     }