public function adaptiveResize($width, $height)
{
// make sure our arguments are valid
if (!is_numeric($width) || $width == 0) {
throw new InvalidArgumentException('$width must be numeric and greater than zero');
}
if (!is_numeric($height) || $height == 0) {
throw new InvalidArgumentException('$height must be numeric and greater than zero');
}
// make sure we're not exceeding our image size if we're not supposed to
if ($this->options['resizeUp'] === false) {
$this->maxHeight = intval($height) > $this->currentDimensions['height'] ? $this->currentDimensions['height'] : $height;
$this->maxWidth = intval($width) > $this->currentDimensions['width'] ? $this->currentDimensions['width'] : $width;
} else {
$this->maxHeight = intval($height);
$this->maxWidth = intval($width);
}
$this->calcImageSizeStrict($this->currentDimensions['width'], $this->currentDimensions['height']);
// resize the image to be close to our desired dimensions
$this->resize($this->newDimensions['newWidth'], $this->newDimensions['newHeight']);
// reset the max dimensions...
if ($this->options['resizeUp'] === false) {
$this->maxHeight = intval($height) > $this->currentDimensions['height'] ? $this->currentDimensions['height'] : $height;
$this->maxWidth = intval($width) > $this->currentDimensions['width'] ? $this->currentDimensions['width'] : $width;
} else {
$this->maxHeight = intval($height);
$this->maxWidth = intval($width);
}
// create the working image
if (function_exists('imagecreatetruecolor')) {
$this->workingImage = imagecreatetruecolor($this->maxWidth, $this->maxHeight);
} else {
$this->workingImage = imagecreate($this->maxWidth, $this->maxHeight);
}
$this->preserveAlpha();
$cropWidth = $this->maxWidth;
$cropHeight = $this->maxHeight;
$cropX = 0;
$cropY = 0;
// now, figure out how to crop the rest of the image...
if ($this->currentDimensions['width'] > $this->maxWidth) {
$cropX = intval(($this->currentDimensions['width'] - $this->maxWidth) / 2);
} elseif ($this->currentDimensions['height'] > $this->maxHeight) {
$cropY = intval(($this->currentDimensions['height'] - $this->maxHeight) / 2);
}
imagecopyresampled($this->workingImage, $this->oldImage, 0, 0, $cropX, $cropY, $cropWidth, $cropHeight, $cropWidth, $cropHeight);
// update all the variables and resources to be correct
$this->oldImage = $this->workingImage;
$this->currentDimensions['width'] = $this->maxWidth;
$this->currentDimensions['height'] = $this->maxHeight;
return $this;
}