/** * {@inheritdoc} */ public function thumbnail(BoxInterface $size, $mode = ImageInterface::THUMBNAIL_INSET, $filter = ImageInterface::FILTER_UNDEFINED) { if ($mode !== ImageInterface::THUMBNAIL_INSET && $mode !== ImageInterface::THUMBNAIL_OUTBOUND) { throw new InvalidArgumentException('Invalid mode specified'); } $imageSize = $this->getSize(); $ratios = array($size->getWidth() / $imageSize->getWidth(), $size->getHeight() / $imageSize->getHeight()); $thumbnail = $this->copy(); $thumbnail->usePalette($this->palette()); $thumbnail->strip(); // if target width is larger than image width // AND target height is longer than image height if ($size->contains($imageSize)) { return $thumbnail; } 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 = $thumbnail->getSize()->scale($ratio); $thumbnail->resize($imageSize, $filter); } $thumbnail->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); $thumbnail->resize($imageSize, $filter); } else { $imageSize = $thumbnail->getSize()->scale($ratio); $thumbnail->resize($imageSize, $filter); } } return $thumbnail; }
/** * {@inheritdoc} */ public function thumbnail(BoxInterface $size, $mode = ImageInterface::THUMBNAIL_INSET) { if ($mode !== ImageInterface::THUMBNAIL_INSET && $mode !== ImageInterface::THUMBNAIL_OUTBOUND) { throw new InvalidArgumentException('Invalid mode specified'); } $imageSize = $this->getSize(); $thumbnail = $this->copy(); // if target width is larger than image width // AND target height is longer than image height if ($size->contains($imageSize)) { return $thumbnail; } // if target width is larger than image width // OR target height is longer than image height if (!$imageSize->contains($size)) { $size = new Box(min($imageSize->getWidth(), $size->getWidth()), min($imageSize->getHeight(), $size->getHeight())); } try { if ($mode === ImageInterface::THUMBNAIL_INSET) { $thumbnail->gmagick->thumbnailimage($size->getWidth(), $size->getHeight(), true); } elseif ($mode === ImageInterface::THUMBNAIL_OUTBOUND) { $thumbnail->gmagick->cropthumbnailimage($size->getWidth(), $size->getHeight()); } } catch (\GmagickException $e) { throw new RuntimeException('Thumbnail operation failed', $e->getCode(), $e); } return $thumbnail; }
/** * @covers Imagine\Image\Box::contains * * @dataProvider getSizeBoxStartAndExpected * * @param BoxInterface $size * @param BoxInterface $box * @param PointInterface $start * @param Boolean $expected */ public function testShouldDetermineIfASizeContainsABoxAtAStartPosition(BoxInterface $size, BoxInterface $box, PointInterface $start, $expected) { $this->assertEquals($expected, $size->contains($box, $start)); }
/** * Calculate the final dimensions for an outbound box. usually exactly the requested width and height unless that * would require upscaling and it is not allowed. * * @param BoxInterface $originalDimensions * @param integer $requestedWidth * @param integer $requestedHeight * @return BoxInterface */ protected function calculateOutboundBox(BoxInterface $originalDimensions, $requestedWidth, $requestedHeight) { $newDimensions = new Box($requestedWidth, $requestedHeight); if ($this->getAllowUpScaling() === TRUE || $originalDimensions->contains($newDimensions) === TRUE) { return $newDimensions; } // We need to make sure that the new dimensions are such that no upscaling is needed. $ratios = array($originalDimensions->getWidth() / $requestedWidth, $originalDimensions->getHeight() / $requestedHeight); $ratio = min($ratios); $newDimensions = $newDimensions->scale($ratio); return $newDimensions; }