/** * Creates crop point * * @param AbstractImage $abstractImage * @param int $width * @param int $height * * @return Point */ protected function createCropPoint(AbstractImage $abstractImage, $width, $height) { if (!is_numeric($width) || !is_numeric($height)) { throw new IcrLogicException('Provided values for width and height are not numeric!'); } $width = (int) $width; $height = (int) $height; $imageWidth = $abstractImage->getSize()->getWidth(); $imageHeight = $abstractImage->getSize()->getHeight(); if ($imageWidth < $width || $imageHeight < $height) { throw new IcrRuntimeException('Provided image is too small to be resize! Provide larger image.'); } $cropWidth = $imageWidth / 2 - $width / 2; $cropHeight = $imageHeight / 2 - $height / 2; return new Point($cropWidth, $cropHeight); }
/** * Wrapper for Imagines thumbnail. * * This method had a bunch of issues and the code inside the method is a * workaround! Please see: * * @link https://github.com/burzum/cakephp-imagine-plugin/issues/42 * @link https://github.com/avalanche123/Imagine/issues/478 * * @throws \InvalidArgumentException * @param array Array of options for processing the image. * @throws InvalidArgumentException if no height or width was passed * @return $this */ public function thumbnail(array $options = []) { if (empty($options['height']) || empty($options['width'])) { throw new \InvalidArgumentException(__d('imagine', 'You have to pass height and width in the options!')); } $mode = ImageInterface::THUMBNAIL_INSET; if (isset($options['mode']) && $options['mode'] === 'outbound') { $mode = ImageInterface::THUMBNAIL_OUTBOUND; } $filter = ImageInterface::FILTER_UNDEFINED; if (isset($options['filter'])) { $filter = $options['filter']; } $size = new Box($options['width'], $options['height']); $imageSize = $this->_image->getSize(); $ratios = array($size->getWidth() / $imageSize->getWidth(), $size->getHeight() / $imageSize->getHeight()); // if target width is larger than image width // AND target height is longer than image height if ($size->contains($imageSize)) { return $this->_image; } if ($mode === ImageInterface::THUMBNAIL_INSET) { $ratio = min($ratios); } else { $ratio = max($ratios); } if ($mode === ImageInterface::THUMBNAIL_OUTBOUND) { if (!$imageSize->contains($size)) { $size = new Box(min($imageSize->getWidth(), $size->getWidth()), min($imageSize->getHeight(), $size->getHeight())); } else { $imageSize = $this->_image->getSize()->scale($ratio); $this->_image->resize($imageSize, $filter); } $this->_image->crop(new Point(max(0, round(($imageSize->getWidth() - $size->getWidth()) / 2)), max(0, round(($imageSize->getHeight() - $size->getHeight()) / 2))), $size); } else { if (!$imageSize->contains($size)) { $imageSize = $imageSize->scale($ratio); $this->_image->resize($imageSize, $filter); } else { $imageSize = $this->_image->getSize()->scale($ratio); $this->_image->resize($imageSize, $filter); } } return $this; }
/** * Creates crop point * * @param AbstractImage $abstractImage * @param int $width * @param int $height * * @return Point */ protected function createCropPoint(AbstractImage $abstractImage, $width, $height) { if (!is_numeric($width) || !is_numeric($height)) { throw new \LogicException('Provided values for width and height are not numeric!'); } $width = (int) $width; $height = (int) $height; $imageWidth = $abstractImage->getSize()->getWidth(); $imageHeight = $abstractImage->getSize()->getHeight(); if ($imageWidth < $width || $imageHeight < $height) { throw new ImageTooSmallException('Provided image is too small to be resize! Provide larger image.'); } $cropWidth = $imageWidth / 2 - $width / 2; $cropHeight = $imageHeight / 2 - $height / 2; $box = clone $this->box; $box->setWidth(round($cropWidth))->setHeight(round($cropHeight)); $this->point->setBox($box); return $this->point; }