public function crop($startX, $startY, $cropWidth, $cropHeight)
{
// validate input
if (!is_numeric($startX)) {
throw new InvalidArgumentException('$startX must be numeric');
}
if (!is_numeric($startY)) {
throw new InvalidArgumentException('$startY must be numeric');
}
if (!is_numeric($cropWidth)) {
throw new InvalidArgumentException('$cropWidth must be numeric');
}
if (!is_numeric($cropHeight)) {
throw new InvalidArgumentException('$cropHeight must be numeric');
}
// do some calculations
$cropWidth = $this->currentDimensions['width'] < $cropWidth ? $this->currentDimensions['width'] : $cropWidth;
$cropHeight = $this->currentDimensions['height'] < $cropHeight ? $this->currentDimensions['height'] : $cropHeight;
// ensure everything's in bounds
if ($startX + $cropWidth > $this->currentDimensions['width']) {
$startX = $this->currentDimensions['width'] - $cropWidth;
}
if ($startY + $cropHeight > $this->currentDimensions['height']) {
$startY = $this->currentDimensions['height'] - $cropHeight;
}
if ($startX < 0) {
$startX = 0;
}
if ($startY < 0) {
$startY = 0;
}
// create the working image
if (function_exists('imagecreatetruecolor')) {
$this->workingImage = imagecreatetruecolor($cropWidth, $cropHeight);
} else {
$this->workingImage = imagecreate($cropWidth, $cropHeight);
}
$this->preserveAlpha();
imagecopyresampled($this->workingImage, $this->oldImage, 0, 0, $startX, $startY, $cropWidth, $cropHeight, $cropWidth, $cropHeight);
$this->oldImage = $this->workingImage;
$this->currentDimensions['width'] = $cropWidth;
$this->currentDimensions['height'] = $cropHeight;
return $this;
}