/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); $params = $event->getArgument('params'); $maxWidth = !empty($params['width']) ? (int) $params['width'] : 0; $maxHeight = !empty($params['height']) ? (int) $params['height'] : 0; try { $sourceWidth = $image->getWidth(); $sourceHeight = $image->getHeight(); $width = $maxWidth ?: $sourceWidth; $height = $maxHeight ?: $sourceHeight; // Figure out original ratio $ratio = $sourceWidth / $sourceHeight; // Is the original image larger than the max-parameters? if ($sourceWidth > $width || $sourceHeight > $height) { if ($width / $height > $ratio) { $width = round($height * $ratio); } else { $height = round($width / $ratio); } } else { // Original image is smaller than the max-parameters, don't transform return; } $this->imagick->setOption('jpeg:size', $width . 'x' . $height); $this->imagick->thumbnailImage($width, $height); $size = $this->imagick->getImageGeometry(); $image->setWidth($size['width'])->setHeight($size['height'])->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); $params = $event->getArgument('params'); $color = !empty($params['color']) ? $this->formatColor($params['color']) : $this->color; $width = isset($params['width']) ? (int) $params['width'] : $this->width; $height = isset($params['height']) ? (int) $params['height'] : $this->height; $mode = !empty($params['mode']) ? $params['mode'] : $this->mode; try { if ($mode === 'outbound') { // Paint the border outside of the image, increasing the width/height if ($this->imagick->getImageAlphaChannel() !== 0) { // If we have an alpha channel and call `borderImage()`, Imagick will remove // the alpha channel - if we have an alpha channel, use an alternative approach $this->expandImage($color, $width, $height, $image); } else { // If we don't have an alpha channel, use the more cost-efficient `borderImage()` $this->imagick->borderImage($color, $width, $height); } } else { // Paint the border inside of the image, keeping the orignal width/height $this->drawBorderInside($color, $width, $height, $image); } $size = $this->imagick->getImageGeometry(); $image->setWidth($size['width'])->setHeight($size['height'])->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } catch (ImagickPixelException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); $params = $event->getArgument('params'); if (empty($params['width']) && empty($params['height'])) { throw new TransformationException('Missing both width and height. You need to specify at least one of them', 400); } $width = !empty($params['width']) ? (int) $params['width'] : 0; $height = !empty($params['height']) ? (int) $params['height'] : 0; $originalWidth = $image->getWidth(); $originalHeight = $image->getHeight(); if ($width === $originalWidth && $height === $originalHeight) { // Resize params match the current image size, no need for any resizing return; } // Calculate width or height if not both have been specified if (!$height) { $height = $originalHeight / $originalWidth * $width; } else { if (!$width) { $width = $originalWidth / $originalHeight * $height; } } try { $this->imagick->setOption('jpeg:size', $width . 'x' . $height); $this->imagick->thumbnailImage($width, $height); $size = $this->imagick->getImageGeometry(); $image->setWidth($size['width'])->setHeight($size['height'])->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $params = $event->getArgument('params'); $threshold = !empty($params['threshold']) ? (double) $params['threshold'] : $this->threshold; try { $this->imagick->sepiaToneImage($threshold); $event->getArgument('image')->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $params = $event->getArgument('params'); $brightness = isset($params['b']) ? (int) $params['b'] : 100; $saturation = isset($params['s']) ? (int) $params['s'] : 100; $hue = isset($params['h']) ? (int) $params['h'] : 100; try { $this->imagick->modulateImage($brightness, $saturation, $hue); $event->getArgument('image')->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $params = $event->getArgument('params'); $preset = isset($params['preset']) ? $params['preset'] : null; switch ($preset) { case 'moderate': $radius = 2; $sigma = 1; $gain = 2; $threshold = 0.05; break; case 'strong': $radius = 2; $sigma = 1; $gain = 3; $threshold = 0.025; break; case 'extreme': $radius = 2; $sigma = 1; $gain = 4; $threshold = 0; break; case 'light': default: // Default values (with only adding ?t[]=sharpen) $radius = 2; $sigma = 1; $gain = 1; $threshold = 0.05; } if (isset($params['radius'])) { $radius = (double) $params['radius']; } if (isset($params['sigma'])) { $sigma = (double) $params['sigma']; } if (isset($params['gain'])) { $gain = (double) $params['gain']; } if (isset($params['threshold'])) { $threshold = (double) $params['threshold']; } try { $this->imagick->unsharpMaskImage($radius, $sigma, $gain, $threshold); $event->getArgument('image')->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); $pois = $this->getPoisFromMetadata($event, $image); if (empty($pois) || !is_array($pois)) { return; } $params = $event->getArgument('params'); $color = !empty($params['color']) ? $this->formatColor($params['color']) : $this->color; $borderSize = isset($params['borderSize']) ? (int) $params['borderSize'] : $this->borderSize; $pointSize = isset($params['pointSize']) ? (int) $params['pointSize'] : $this->pointSize; $imageWidth = $image->getWidth(); $imageHeight = $image->getHeight(); try { foreach ($pois as $poi) { if (isset($poi['width']) && isset($poi['height'])) { $this->drawPoiRectangle($poi, $color, $borderSize - 1, $imageWidth, $imageHeight); } else { if (isset($poi['cx']) && isset($poi['cy'])) { $this->drawPoiCircle($poi, $color, $borderSize, $pointSize); } else { throw new TransformationException('Point of interest had neither `width` and `height` nor `cx` and `cy`'); } } } $image->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * {@inheritdoc} */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); try { // Contrast $this->imagick->contrastImage(1); // Noise $this->imagick->addNoiseImage(Imagick::NOISE_GAUSSIAN, Imagick::CHANNEL_GREEN); // Desaturate + adjust brightness $this->imagick->modulateImage(135, 25, 100); // Adjust color balance $this->imagick->evaluateImage(Imagick::EVALUATE_MULTIPLY, 1.1, Imagick::CHANNEL_RED); $this->imagick->evaluateImage(Imagick::EVALUATE_MULTIPLY, 1.02, Imagick::CHANNEL_BLUE); $this->imagick->evaluateImage(Imagick::EVALUATE_MULTIPLY, 1.1, Imagick::CHANNEL_GREEN); // Gamma $this->imagick->gammaImage(0.87); // Vignette $width = $image->getWidth(); $height = $image->getHeight(); $size = $height > $width ? $width / 6 : $height / 6; $this->imagick->setImageBackgroundColor(new ImagickPixel('black')); $this->imagick->vignetteImage(0, 60, 0 - $size, 0 - $size); // Mark as transformed $image->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); $params = $event->getArgument('params'); foreach (['width', 'height'] as $param) { if (!isset($params[$param])) { throw new TransformationException('Missing required parameter: ' . $param, 400); } } // Fetch the x, y, width and height of the resulting image $x = !empty($params['x']) ? (int) $params['x'] : $this->x; $y = !empty($params['y']) ? (int) $params['y'] : $this->y; $mode = !empty($params['mode']) ? $params['mode'] : null; $width = (int) $params['width']; $height = (int) $params['height']; $imageWidth = $image->getWidth(); $imageHeight = $image->getHeight(); // Set correct x and/or y values based on the crop mode if ($mode === 'center' || $mode === 'center-x') { $x = (int) ($imageWidth - $width) / 2; } if ($mode === 'center' || $mode === 'center-y') { $y = (int) ($imageHeight - $height) / 2; } // Throw exception on X/Y values that are out of bounds if ($x + $width > $imageWidth) { throw new TransformationException('Crop area is out of bounds (`x` + `width` > image width)', 400); } else { if ($y + $height > $imageHeight) { throw new TransformationException('Crop area is out of bounds (`y` + `height` > image height)', 400); } } // Return if there is no need for cropping if ($x === 0 && $y === 0 && $imageWidth <= $width && $imageHeight <= $height) { return; } try { $this->imagick->cropImage($width, $height, $x, $y); $this->imagick->setImagePage(0, 0, 0, 0); $size = $this->imagick->getImageGeometry(); $image->setWidth($size['width'])->setHeight($size['height'])->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { try { $this->imagick->setInterlaceScheme(Imagick::INTERLACE_PLANE); $event->getArgument('image')->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $params = $event->getArgument('params'); $alpha = isset($params['sharpen']) ? (double) $params['sharpen'] : 1; $alpha = isset($params['alpha']) ? (double) $params['alpha'] : $alpha; $beta = isset($params['beta']) ? (double) $params['beta'] : 0.5; $sharpen = $alpha > 0; if ($alpha == 0) { return; } $beta *= $this->getQuantumRange(); try { $this->imagick->sigmoidalContrastImage($sharpen, abs($alpha), $beta); $event->getArgument('image')->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { try { $this->imagick->transverseImage(); $event->getArgument('image')->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); $params = $event->getArgument('params'); if (empty($params['angle'])) { throw new TransformationException('Missing required parameter: angle', 400); } $angle = (int) $params['angle']; $bg = !empty($params['bg']) ? $this->formatColor($params['bg']) : $this->bg; try { $this->imagick->rotateImage($bg, $angle); $size = $this->imagick->getImageGeometry(); $image->setWidth($size['width'])->setHeight($size['height'])->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } catch (ImagickPixelException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $params = $event->getArgument('params'); $sharpen = isset($params['sharpen']) ? (int) $params['sharpen'] : 0; try { if ($sharpen > 0) { for ($i = 0; $i < $sharpen; $i++) { $this->imagick->contrastImage(1); } } else { for ($i = 0; $i >= $sharpen; $i--) { $this->imagick->contrastImage(0); } } $event->getArgument('image')->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $params = $event->getArgument('params'); if (empty($params['level'])) { throw new TransformationException('Missing required parameter: level', 400); } $this->level = (int) $params['level']; if ($this->level < 0 || $this->level > 100) { throw new TransformationException('level must be between 0 and 100', 400); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); $params = $event->getArgument('params'); if (empty($params['type'])) { throw new TransformationException('Missing required parameter: type', 400); } $type = $params['type']; if ($image->getExtension() === $type) { // The requested extension is the same as the image, no conversion is needed return; } try { $this->imagick->setImageFormat($type); $mimeType = array_search($type, Image::$mimeTypes); $image->setMimeType($mimeType)->setExtension($type)->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); $params = $event->getArgument('params'); $width = !empty($params['width']) ? (int) $params['width'] : $this->width; $height = !empty($params['height']) ? (int) $params['height'] : $this->height; $fit = !empty($params['fit']) ? $params['fit'] : $this->fit; try { $this->imagick->setOption('jpeg:size', $width . 'x' . $height); if ($fit === 'inset') { $this->imagick->thumbnailimage($width, $height, true); } else { $this->imagick->cropThumbnailImage($width, $height); } $size = $this->imagick->getImageGeometry(); $image->setWidth($size['width'])->setHeight($size['height'])->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * {@inheritdoc} */ public function transform(EventInterface $event) { $params = $event->getArgument('params'); $inner = $this->formatColor(isset($params['inner']) ? $params['inner'] : 'none'); $outer = $this->formatColor(isset($params['outer']) ? $params['outer'] : '000'); $scale = (double) max(isset($params['scale']) ? $params['scale'] : 1.5, 1); $image = $event->getArgument('image'); $width = $image->getWidth(); $height = $image->getHeight(); $scaleX = floor($width * $scale); $scaleY = floor($height * $scale); $vignette = new Imagick(); $vignette->newPseudoImage($scaleX, $scaleY, 'radial-gradient:' . $inner . '-' . $outer); $vignette->cropImage($width, $height, floor(($scaleX - $width) / 2), floor(($scaleY - $height) / 2)); try { $this->imagick->compositeImage($vignette, Imagick::COMPOSITE_MULTIPLY, 0, 0); $image->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); $params = $event->getArgument('params'); $color = !empty($params['color']) ? $this->formatColor($params['color']) : $this->color; $width = !empty($params['width']) ? (int) $params['width'] : $this->width; $height = !empty($params['height']) ? (int) $params['height'] : $this->height; $mode = !empty($params['mode']) ? $params['mode'] : $this->mode; try { if ($mode === 'outbound') { // Paint the border outside of the image, increasing the width/height $this->imagick->borderImage($color, $width, $height); } else { // Paint the border inside of the image, keeping the orignal width/height $imageWidth = $image->getWidth(); $imageHeight = $image->getHeight(); $rect = new ImagickDraw(); $rect->setStrokeColor($color); $rect->setFillColor($color); $rect->setStrokeAntialias(false); // Left $rect->rectangle(0, 0, $width - 1, $imageHeight); // Right $rect->rectangle($imageWidth - $width, 0, $imageWidth, $imageHeight); // Top $rect->rectangle(0, 0, $imageWidth, $height - 1); // Bottom $rect->rectangle(0, $imageHeight - $height, $imageWidth, $imageHeight); // Draw the border $this->imagick->drawImage($rect); } $size = $this->imagick->getImageGeometry(); $image->setWidth($size['width'])->setHeight($size['height'])->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } catch (ImagickPixelException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $params = $event->getArgument('params'); $channel = isset($params['channel']) ? $params['channel'] : 'all'; $amount = isset($params['amount']) ? $params['amount'] : 1; if ($amount < -100) { $amount = -100; } else { if ($amount > 100) { $amount = 100; } } if ($amount < 0) { // amounts from -100 to 0 gets translated to 0 to 1 $gamma = 1 - abs($amount) / 100; } else { // amount from 0 to 100 gets translated to 1 to 10 $gamma = floor($amount / 10.1) + 1; } if ($channel === 'all') { $channel = Imagick::CHANNEL_ALL; } else { $c = null; $channels = array('r' => Imagick::CHANNEL_RED, 'g' => Imagick::CHANNEL_GREEN, 'b' => Imagick::CHANNEL_BLUE, 'c' => Imagick::CHANNEL_CYAN, 'm' => Imagick::CHANNEL_MAGENTA, 'y' => Imagick::CHANNEL_YELLOW, 'k' => Imagick::CHANNEL_BLACK); foreach ($channels as $id => $value) { if (strpos($channel, $id) !== false) { $c |= $value; } } $channel = $c; } try { $quantumRange = $this->imagick->getQuantumRange(); $this->imagick->levelImage(0, (double) $gamma, $quantumRange['quantumRangeLong'], $channel); $event->getArgument('image')->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * {@inheritdoc} */ public function transform(EventInterface $event) { try { $this->imagick->modulateImage(100, 68, 101); $this->imagick->gammaImage(1.19); $range = $this->imagick->getQuantumRange()['quantumRangeLong']; $blackPoint = 0 - round(27 / 255 * $range); $this->imagick->levelImage(0, 1, $range, Imagick::CHANNEL_RED); $this->imagick->levelImage($blackPoint, 1, $range, Imagick::CHANNEL_RED); $event->getArgument('image')->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * {@inheritdoc} */ public function transform(EventInterface $event) { try { $this->imagick->modulateImage(100, 0, 100); $overlay = new Imagick(); $overlay->newPseudoImage(1, 1000, 'gradient:'); $overlay->rotateImage('#fff', 90); $overlay->sigmoidalContrastImage(true, 1.6, 50); $overlay->sigmoidalContrastImage(false, 1 / 3, 0); $this->imagick->clutImage($overlay); $event->getArgument('image')->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { try { $this->imagick->stripImage(); // In newer versions of Imagick, it seems we need to clear and re-read // the data to properly clear the properties $version = $this->imagick->getVersion(); $version = preg_replace('#.*?(\\d+\\.\\d+\\.\\d+).*#', '$1', $version['versionString']); if (version_compare($version, '6.8.0') >= 0) { $data = $this->imagick->getImagesBlob(); $this->imagick->clear(); $this->imagick->readImageBlob($data); } $event->getArgument('image')->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Update metadata * * @param EventInterface $event An event instance */ public function updateMetadata(EventInterface $event) { $request = $event->getRequest(); $event->getDatabase()->updateMetadata($request->getPublicKey(), $request->getImageIdentifier(), $event->getArgument('metadata')); }
/** * Adjust image transformations * * This method will adjust transformation parameters based on the ration between the original * image and the image variation used. * * @param EventInterface $event The current event */ public function adjustImageTransformations(EventInterface $event) { $request = $event->getRequest(); $transformations = $request->getTransformations(); $transformationIndex = $event->getArgument('transformationIndex'); $ratio = $event->getArgument('ratio'); $transformationNames = ['crop', 'border', 'canvas', 'watermark']; // Adjust coordinates according to the ratio between the original and the variation for ($i = 0; $i <= $transformationIndex; $i++) { $name = $transformations[$i]['name']; $params = $transformations[$i]['params']; if (in_array($name, $transformationNames)) { foreach (['x', 'y', 'width', 'height'] as $param) { if (isset($params[$param])) { $params[$param] = round($params[$param] / $ratio); } } $transformations[$i]['params'] = $params; } } $request->setTransformations($transformations); }
/** * Perform a simple crop/resize operation on the image * * @param EventInterface $event * @param int $width * @param int $height */ private function simpleCrop(EventInterface $event, $width, $height) { $image = $event->getArgument('image'); $sourceRatio = $image->getWidth() / $image->getHeight(); $cropRatio = $width / $height; $params = []; if ($cropRatio > $sourceRatio) { $params['width'] = $width; } else { $params['height'] = $height; } $event->getManager()->trigger('image.transformation.maxsize', ['image' => $event->getArgument('image'), 'params' => $params]); $event->getManager()->trigger('image.transformation.crop', ['image' => $event->getArgument('image'), 'params' => ['width' => $width, 'height' => $height, 'mode' => 'center']]); }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); try { // Get orientation from exif data $orientation = $this->imagick->getImageOrientation(); /** * Transform image if orientation is set and greater than 1 * (Imagick::ORIENTATION_TOPLEFT) */ if ($orientation > 1) { $flipHorizontally = false; $flipVertically = false; $rotate = 0; switch ($orientation) { case Imagick::ORIENTATION_TOPRIGHT: //2 $flipHorizontally = true; break; case Imagick::ORIENTATION_BOTTOMRIGHT: //3 $rotate = 180; break; case Imagick::ORIENTATION_BOTTOMLEFT: //4 $flipVertically = true; break; case Imagick::ORIENTATION_LEFTTOP: //5 $rotate = 90; $flipHorizontally = true; break; case Imagick::ORIENTATION_RIGHTTOP: //6 $rotate = 90; break; case Imagick::ORIENTATION_RIGHTBOTTOM: //7 $rotate = 90; $flipVertically = true; break; case Imagick::ORIENTATION_LEFTBOTTOM: //8 $rotate = 270; break; } if ($rotate) { $this->imagick->rotateImage('#000', $rotate); /** * Recalculate width and height if number of degrees are not * dividable by 180, meaning height and width is changed. */ if ($rotate % 180) { $size = $this->imagick->getImageGeometry(); $image->setWidth($size['width'])->setHeight($size['height']); } } if ($flipHorizontally) { $this->imagick->flopImage(); } if ($flipVertically) { $this->imagick->flipImage(); } if ($rotate || $flipHorizontally || $flipVertically) { // Set the image orientation so it reflects the transformation that's been done $this->imagick->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); $image->hasBeenTransformed(true); } } } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } catch (ImagickPixelException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event The event instance */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); $params = $event->getArgument('params'); $width = !empty($params['width']) ? (int) $params['width'] : 0; $height = !empty($params['height']) ? (int) $params['height'] : 0; $imageIdentifier = !empty($params['img']) ? $params['img'] : $this->defaultImage; $position = !empty($params['position']) ? $params['position'] : $this->position; $x = !empty($params['x']) ? (int) $params['x'] : $this->x; $y = !empty($params['y']) ? (int) $params['y'] : $this->y; $opacity = (!empty($params['opacity']) ? (int) $params['opacity'] : 100) / 100; if (empty($imageIdentifier)) { throw new TransformationException('You must specify an image identifier to use for the watermark', 400); } // Try to load watermark image from storage try { $watermarkData = $event->getStorage()->getImage($event->getRequest()->getUser(), $imageIdentifier); $watermark = new Imagick(); $watermark->readImageBlob($watermarkData); $watermarkSize = $watermark->getImageGeometry(); $watermark->setImageOpacity($opacity); } catch (StorageException $e) { if ($e->getCode() == 404) { throw new TransformationException('Watermark image not found', 400); } throw $e; } // Should we resize the watermark? if ($height || $width) { // Calculate width or height if not both have been specified if (!$height) { $height = $watermarkSize['height'] / $watermarkSize['width'] * $width; } else { if (!$width) { $width = $watermarkSize['width'] / $watermarkSize['height'] * $height; } } $watermark->thumbnailImage($width, $height); } else { $width = $watermarkSize['width']; $height = $watermarkSize['height']; } // Determine placement of the watermark if ($position === 'top-right') { $x = $image->getWidth() - $width + $x; } else { if ($position === 'bottom-left') { $y = $image->getHeight() - $height + $y; } else { if ($position === 'bottom-right') { $x = $image->getWidth() - $width + $x; $y = $image->getHeight() - $height + $y; } else { if ($position === 'center') { $x = $image->getWidth() / 2 - $width / 2 + $x; $y = $image->getHeight() / 2 - $height / 2 + $y; } } } } // Now make a composite try { $this->imagick->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y); $image->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Add radial blur to the image * * @param EventInterface $event The event instance */ private function radialBlur(EventInterface $event) { $params = $event->getArgument('params'); $this->checkRequiredParams($params, ['angle']); $angle = (double) $params['angle']; try { $this->imagick->radialBlurImage($angle); $event->getArgument('image')->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }
/** * Transform the image * * @param EventInterface $event */ public function transform(EventInterface $event) { $image = $event->getArgument('image'); $params = $event->getArgument('params'); $width = !empty($params['width']) ? (int) $params['width'] : $image->getWidth(); $height = !empty($params['height']) ? (int) $params['height'] : $image->getHeight(); $mode = !empty($params['mode']) ? $params['mode'] : $this->mode; $x = !empty($params['x']) ? (int) $params['x'] : $this->x; $y = !empty($params['y']) ? (int) $params['y'] : $this->y; $bg = !empty($params['bg']) ? $this->formatColor($params['bg']) : $this->bg; try { // Clone the original that we will move back onto the canvas $original = clone $this->imagick; // Clear the original and make the canvas $this->imagick->clear(); $this->imagick->newImage($width, $height, $bg); $this->imagick->setImageFormat($image->getExtension()); $existingWidth = $image->getWidth(); $existingHeight = $image->getHeight(); if ($existingWidth > $width || $existingHeight > $height) { // The existing image is bigger than the canvas and needs to be cropped $cropX = 0; $cropY = 0; $cropWidth = $width; $cropHeight = $height; if ($existingWidth > $width) { if ($mode === 'center' || $mode === 'center-x') { $cropX = (int) ($existingWidth - $width) / 2; } } else { $cropWidth = $existingWidth; } if ($existingHeight > $height) { if ($mode === 'center' || $mode === 'center-y') { $cropY = (int) ($existingHeight - $height) / 2; } } else { $cropHeight = $existingHeight; } // Crop the original $original->cropImage($cropWidth, $cropHeight, $cropX, $cropY); } // Figure out the correct placement of the image based on the placement mode. Use the // size from the imagick image when calculating since the image may have been cropped // above. $existingSize = $original->getImageGeometry(); if ($mode === 'center') { $x = ($width - $existingSize['width']) / 2; $y = ($height - $existingSize['height']) / 2; } else { if ($mode === 'center-x') { $x = ($width - $existingSize['width']) / 2; } else { if ($mode === 'center-y') { $y = ($height - $existingSize['height']) / 2; } } } // Paste existing image into the new canvas at the given position $this->imagick->compositeImage($original, Imagick::COMPOSITE_DEFAULT, $x, $y); // Store the new image $size = $this->imagick->getImageGeometry(); $image->setWidth($size['width'])->setHeight($size['height'])->hasBeenTransformed(true); } catch (ImagickException $e) { throw new TransformationException($e->getMessage(), 400, $e); } catch (ImagickPixelException $e) { throw new TransformationException($e->getMessage(), 400, $e); } }