/** * Crop the image to the given dimension and position. * * @param Image $image * @param int $cropWidth Crop width in pixels. * @param int $cropHeight Crop Height in pixels. * @param string $position The crop position. Possible values top-left, top-center, top-right, center-left, center, center-right, bottom-left, bottom-center, bottom-right and smart. Defaults to center. * @param int $offsetX Number of pixels to add to the X position of the crop. * @param int $offsetY Number of pixels to add to the Y position of the crop. * * @return Editor * @throws \Exception */ public function crop(&$image, $cropWidth, $cropHeight, $position = 'center', $offsetX = 0, $offsetY = 0) { if ($image->isAnimated()) { // Ignore animated GIF for now return $this; } if ('smart' === $position) { // Smart crop list($x, $y) = $this->_smartCrop($image, $cropWidth, $cropHeight); } else { // Turn into an instance of Position $position = new Position($position, $offsetX, $offsetY); // Crop position as x,y coordinates list($x, $y) = $position->getXY($image->getWidth(), $image->getHeight(), $cropWidth, $cropHeight); } $x += $offsetX; $y += $offsetY; $image->getCore()->cropImage($cropWidth, $cropHeight, $x, $y); return $this; }
/** * Crop the image to the given dimension and position. * * @param Image $image * @param int $cropWidth Crop width in pixels. * @param int $cropHeight Crop Height in pixels. * @param string $position The crop position. Possible values top-left, top-center, top-right, center-left, center, center-right, bottom-left, bottom-center, bottom-right and smart. Defaults to center. * @param int $offsetX Number of pixels to add to the X position of the crop. * @param int $offsetY Number of pixels to add to the Y position of the crop. * * @return Editor * @throws \Exception */ public function crop(&$image, $cropWidth, $cropHeight, $position = 'center', $offsetX = 0, $offsetY = 0) { if ($image->isAnimated()) { // Ignore animated GIF for now return $this; } if ('smart' === $position) { // Smart crop list($x, $y) = $this->_smartCrop($image, $cropWidth, $cropHeight); } else { // Turn into an instance of Position $position = new Position($position, $offsetX, $offsetY); // Crop position as x,y coordinates list($x, $y) = $position->getXY($image->getWidth(), $image->getHeight(), $cropWidth, $cropHeight); } $x += $offsetX; $y += $offsetY; // Create blank image $newImageResource = imagecreatetruecolor($cropWidth, $cropHeight); // Now crop imagecopyresampled($newImageResource, $image->getCore(), 0, 0, $x, $y, $cropWidth, $cropHeight, $cropWidth, $cropHeight); // Free memory of old resource imagedestroy($image->getCore()); // Cropped image instance $image = new Image($newImageResource, $image->getImageFile(), $cropWidth, $cropHeight, $image->getType()); return $this; }