/** * Use Imagine to resize an image and return it's new path * * @param string $originalImage * @param array $arguments * @param array $imageOptions * * @return void * * @throws ErrorException */ public function doFilter($originalImage, $arguments, $imageOptions) { // Need at least a height or a width if (empty($arguments['height']) && empty($arguments['width'])) { throw new ErrorException('You must set at least the height (h) or the width (w)'); } switch (strtolower($imageOptions['imageProcessor'])) { case 'gd': $Imagine = new \Imagine\Gd\Imagine(); break; case 'imagick': $Imagine = new \Imagine\Imagick\Imagine(); break; case 'gmagick': $Imagine = new \Imagine\Gmagick\Imagine(); break; default: throw new ErrorException('Unsupported imageProcessor config value: ' . $imageOptions['imageProcessor']); } $image = $Imagine->open($originalImage); $size = $image->getSize(); $originalWidth = $size->getWidth(); $originalHeight = $size->getHeight(); $width = $originalWidth; $height = $originalHeight; if (!empty($arguments['height'])) { if ($originalHeight > $arguments['height'] || $arguments['stretch']) { $height = $arguments['height']; } } if (!empty($arguments['width'])) { if ($originalWidth > $arguments['width'] || $arguments['stretch']) { $width = $arguments['width']; } } /** * Prevent from someone from creating huge images */ if ($width > $imageOptions['maxAllowedResizeWidth']) { $width = $imageOptions['maxAllowedResizeWidth']; } if ($height > $imageOptions['maxAllowedResizeHeight']) { $height = $imageOptions['maxAllowedResizeHeight']; } $mode = $arguments['exact'] ? ImageInterface::THUMBNAIL_OUTBOUND : ImageInterface::THUMBNAIL_INSET; $newSize = new Box($width, $height); $newImage = $image->thumbnail($newSize, $mode); if ($arguments['fill']) { $adjustedSize = $newImage->getSize(); $canvasWidth = isset($arguments['width']) ? $arguments['width'] : $adjustedSize->getWidth(); $canvasHeight = isset($arguments['height']) ? $arguments['height'] : $adjustedSize->getHeight(); /** * Prevent from someone from creating huge images */ if ($canvasWidth > $imageOptions['maxAllowedResizeWidth']) { $canvasWidth = $imageOptions['maxAllowedResizeWidth']; } if ($canvasHeight > $imageOptions['maxAllowedResizeHeight']) { $canvasHeight = $imageOptions['maxAllowedResizeHeight']; } $canvas = $Imagine->create(new Box($canvasWidth, $canvasHeight), new Color($arguments['fillColour'])); // Put image in the middle of the canvas $newImage = $canvas->paste($newImage, new Point((int) (($canvasWidth - $adjustedSize->getWidth()) / 2), (int) (($canvasHeight - $adjustedSize->getHeight()) / 2))); } $newImage->save($originalImage, array('quality' => $arguments['quality'])); }