8 class ImageMagick extends \Magento\Framework\Image\Adapter\AbstractAdapter
28 'resolution' => [
'x' => 72,
'y' => 72],
29 'small_image' => [
'width' => 300,
'height' => 300],
30 'sharpen' => [
'radius' => 4,
'deviation' => 1],
42 if (is_array($color)) {
43 $color =
"rgb(" . join(
',', $color) .
")";
46 $pixel = new \ImagickPixel();
47 if (is_numeric($color)) {
48 $pixel->setColorValue($color, 1);
50 $pixel->setColor($color);
52 if ($this->_imageHandler) {
53 $this->_imageHandler->setImageBackgroundColor($color);
56 $pixel = $this->_imageHandler->getImageBackgroundColor();
59 $this->imageBackgroundColor = $pixel->getColorAsString();
71 public function open($filename)
73 $this->_fileName = $filename;
78 $this->_imageHandler = new \Imagick($this->_fileName);
79 }
catch (\ImagickException $e) {
80 throw new \Exception(sprintf(
'Unsupported image format. File: %s', $this->_fileName), $e->getCode(), $e);
96 public function save($destination =
null, $newName =
null)
101 $this->_imageHandler->stripImage();
102 $this->_imageHandler->writeImage(
$fileName);
112 $this->_imageHandler->setImageCompressionQuality($this->
quality());
113 $this->_imageHandler->setImageCompression(\Imagick::COMPRESSION_JPEG);
114 $this->_imageHandler->setImageUnits(\Imagick::RESOLUTION_PIXELSPERINCH);
115 $this->_imageHandler->setImageResolution(
116 $this->_options[
'resolution'][
'x'],
117 $this->_options[
'resolution'][
'y']
119 if (method_exists($this->_imageHandler,
'optimizeImageLayers')) {
120 $this->_imageHandler->optimizeImageLayers();
133 return $this->_imageHandler->getImageBlob();
143 public function resize($frameWidth =
null, $frameHeight =
null)
148 $newImage = new \Imagick();
150 $dims[
'frame'][
'width'],
151 $dims[
'frame'][
'height'],
152 $this->_imageHandler->getImageBackgroundColor()
155 $this->_imageHandler->resizeImage(
156 $dims[
'dst'][
'width'],
157 $dims[
'dst'][
'height'],
158 \Imagick::FILTER_CUBIC,
162 if ($this->_imageHandler->getImageWidth() < $this->_options[
'small_image'][
'width'] ||
163 $this->_imageHandler->getImageHeight() < $this->_options[
'small_image'][
'height']
165 $this->_imageHandler->sharpenImage(
166 $this->_options[
'sharpen'][
'radius'],
167 $this->_options[
'sharpen'][
'deviation']
171 $newImage->compositeImage(
172 $this->_imageHandler,
173 \Imagick::COMPOSITE_COPYOPACITY,
178 $newImage->compositeImage(
179 $this->_imageHandler,
180 \Imagick::COMPOSITE_OVER,
185 $newImage->setImageFormat($this->_imageHandler->getImageFormat());
186 $this->_imageHandler->clear();
187 $this->_imageHandler->destroy();
188 $this->_imageHandler = $newImage;
203 $angle = 360 - $angle;
204 $pixel = new \ImagickPixel();
205 $pixel->setColor(
"rgb(" . $this->imageBackgroundColor .
")");
207 $this->_imageHandler->rotateImage($pixel, $angle);
220 public function crop($top = 0, $left = 0, $right = 0, $bottom = 0)
222 if ($left == 0 && $top == 0 && $right == 0 && $bottom == 0 || !$this->
_canProcess()) {
226 $newWidth = $this->_imageSrcWidth - $left - $right;
227 $newHeight = $this->_imageSrcHeight - $top - $bottom;
229 $this->_imageHandler->cropImage($newWidth, $newHeight, $left, $top);
248 public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity = 30, $tile =
false)
250 if (empty($imagePath) || !file_exists($imagePath)) {
251 throw new \LogicException(self::ERROR_WATERMARK_IMAGE_ABSENT);
257 $opacity = (double)number_format($opacity / 100, 1);
258 $watermark = new \Imagick($imagePath);
264 $watermark->resizeImage(
267 \Imagick::FILTER_CUBIC,
272 if (method_exists($watermark,
'getImageAlphaChannel')) {
274 if ($watermark->getImageAlphaChannel() == 0) {
275 $watermark->setImageAlphaChannel(\Imagick::ALPHACHANNEL_OPAQUE);
279 $compositeChannels = \Imagick::CHANNEL_ALL;
280 $watermark->evaluateImage(\Imagick::EVALUATE_MULTIPLY, $opacity, \Imagick::CHANNEL_OPACITY);
281 $compositeChannels &= ~(\Imagick::CHANNEL_OPACITY);
285 $watermark->sampleImage($this->_imageSrcWidth, $this->_imageSrcHeight);
288 $positionX = ($this->_imageSrcWidth - $watermark->getImageWidth()) / 2;
289 $positionY = ($this->_imageSrcHeight - $watermark->getImageHeight()) / 2;
292 $positionX = $this->_imageSrcWidth - $watermark->getImageWidth();
295 $positionX = $this->_imageSrcWidth - $watermark->getImageWidth();
296 $positionY = $this->_imageSrcHeight - $watermark->getImageHeight();
299 $positionY = $this->_imageSrcHeight - $watermark->getImageHeight();
310 $offsetX = $positionX;
311 $offsetY = $positionY;
312 while ($offsetY <= $this->_imageSrcHeight + $watermark->getImageHeight()) {
313 while ($offsetX <= $this->_imageSrcWidth + $watermark->getImageWidth()) {
314 $this->_imageHandler->compositeImage(
316 \Imagick::COMPOSITE_OVER,
321 $offsetX += $watermark->getImageWidth();
323 $offsetX = $positionX;
324 $offsetY += $watermark->getImageHeight();
327 $this->_imageHandler->compositeImage(
329 \Imagick::COMPOSITE_OVER,
335 }
catch (\ImagickException $e) {
336 throw new \Exception(
'Unable to create watermark.', $e->getCode(), $e);
340 $this->_imageHandler->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);
342 $watermark->destroy();
354 throw new \Exception(
"Required PHP extension 'Imagick' was not loaded.");
365 $this->_imageSrcWidth = $this->_imageHandler->getImageWidth();
366 $this->_imageSrcHeight = $this->_imageHandler->getImageHeight();
367 $this->_imageHandler->setImagePage($this->_imageSrcWidth, $this->_imageSrcHeight, 0, 0);
385 if (
null !== $this->_imageHandler && $this->_imageHandler instanceof \Imagick) {
386 $this->_imageHandler->clear();
387 $this->_imageHandler->destroy();
388 $this->_imageHandler =
null;
402 $pixel = $this->_imageHandler->getImagePixelColor($x, $y);
404 $color = $pixel->getColor();
406 'red' => $color[
'r'],
407 'green' => $color[
'g'],
408 'blue' => $color[
'b'],
409 'alpha' => (1 - $color[
'a']) * 127,
423 throw new \LogicException(self::ERROR_WRONG_IMAGE);
444 if (method_exists(
$image,
'setFont')) {
446 }
elseif (method_exists($draw,
'setFont')) {
447 $draw->setFont($font);
452 $draw->setFontSize($this->_fontSize * 4 / 3);
453 $draw->setFillColor($color);
454 $draw->setStrokeAntialias(
true);
455 $draw->setTextAntialias(
true);
459 $draw->annotation(0, $metrics[
'ascender'],
$text);
461 $height = abs($metrics[
'ascender']) + abs($metrics[
'descender']);
462 $image->newImage($metrics[
'textWidth'], $height, $background);
463 $this->_fileType = IMAGETYPE_PNG;
464 $image->setImageFormat(
'png');
466 $this->_imageHandler =
$image;
479 return new \Imagick(
$files);
489 return new \ImagickDraw();
500 return new \ImagickPixel($color);
crop($top=0, $left=0, $right=0, $bottom=0)
elseif(isset( $params[ 'redirect_parent']))
_adaptResizeValues($frameWidth, $frameHeight)
_getImagickPixelObject($color=null)
_getImagickObject($files=null)
save($destination=null, $newName=null)
const POSITION_BOTTOM_RIGHT
watermark($imagePath, $positionX=0, $positionY=0, $opacity=30, $tile=false)
getWatermarkImageOpacity()
backgroundColor($color=null)
createPngFromString($text, $font='')
const POSITION_BOTTOM_LEFT
const ERROR_WATERMARK_IMAGE_ABSENT
_prepareDestination($destination=null, $newName=null)
foreach($appDirs as $dir) $files
resize($frameWidth=null, $frameHeight=null)