Guest User

Untitled

a guest
Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.79 KB | None | 0 0
  1. public function adaptiveResize ($width, $height)
  2.     {
  3.         // make sure our arguments are valid
  4.         if (!is_numeric($width) || $width  == 0)
  5.         {
  6.             throw new InvalidArgumentException('$width must be numeric and greater than zero');
  7.         }
  8.        
  9.         if (!is_numeric($height) || $height == 0)
  10.         {
  11.             throw new InvalidArgumentException('$height must be numeric and greater than zero');
  12.         }
  13.        
  14.         // make sure we're not exceeding our image size if we're not supposed to
  15.         if ($this->options['resizeUp'] === false)
  16.         {
  17.             $this->maxHeight    = (intval($height) > $this->currentDimensions['height']) ? $this->currentDimensions['height'] : $height;
  18.             $this->maxWidth     = (intval($width) > $this->currentDimensions['width']) ? $this->currentDimensions['width'] : $width;
  19.         }
  20.         else
  21.         {
  22.             $this->maxHeight    = intval($height);
  23.             $this->maxWidth     = intval($width);
  24.         }
  25.        
  26.         $this->calcImageSizeStrict($this->currentDimensions['width'], $this->currentDimensions['height']);
  27.        
  28.         // resize the image to be close to our desired dimensions
  29.         $this->resize($this->newDimensions['newWidth'], $this->newDimensions['newHeight']);
  30.        
  31.         // reset the max dimensions...
  32.         if ($this->options['resizeUp'] === false)
  33.         {
  34.             $this->maxHeight    = (intval($height) > $this->currentDimensions['height']) ? $this->currentDimensions['height'] : $height;
  35.             $this->maxWidth     = (intval($width) > $this->currentDimensions['width']) ? $this->currentDimensions['width'] : $width;
  36.         }
  37.         else
  38.         {
  39.             $this->maxHeight    = intval($height);
  40.             $this->maxWidth     = intval($width);
  41.         }
  42.        
  43.         // create the working image
  44.         if (function_exists('imagecreatetruecolor'))
  45.         {
  46.             $this->workingImage = imagecreatetruecolor($this->maxWidth, $this->maxHeight);
  47.         }
  48.         else
  49.         {
  50.             $this->workingImage = imagecreate($this->maxWidth, $this->maxHeight);
  51.         }
  52.        
  53.         $this->preserveAlpha();
  54.        
  55.         $cropWidth  = $this->maxWidth;
  56.         $cropHeight = $this->maxHeight;
  57.         $cropX      = 0;
  58.         $cropY      = 0;
  59.        
  60.         // now, figure out how to crop the rest of the image...
  61.         if ($this->currentDimensions['width'] > $this->maxWidth)
  62.         {
  63.             $cropX = intval(($this->currentDimensions['width'] - $this->maxWidth) / 2);
  64.         }
  65.         elseif ($this->currentDimensions['height'] > $this->maxHeight)
  66.         {
  67.             $cropY = intval(($this->currentDimensions['height'] - $this->maxHeight) / 2);
  68.         }
  69.        
  70.         imagecopyresampled
  71.         (
  72.             $this->workingImage,
  73.             $this->oldImage,
  74.             0,
  75.             0,
  76.             $cropX,
  77.             $cropY,
  78.             $cropWidth,
  79.             $cropHeight,
  80.             $cropWidth,
  81.             $cropHeight
  82.         );
  83.        
  84.         // update all the variables and resources to be correct
  85.         $this->oldImage                     = $this->workingImage;
  86.         $this->currentDimensions['width']   = $this->maxWidth;
  87.         $this->currentDimensions['height']  = $this->maxHeight;
  88.        
  89.         return $this;
  90.     }
Add Comment
Please, Sign In to add comment