Convert.php in Imagick 8
File
src/Plugin/ImageToolkit/Operation/imagick/Convert.php
View source
<?php
namespace Drupal\imagick\Plugin\ImageToolkit\Operation\imagick;
use Drupal\imagick\ImagickConst;
use Imagick;
class Convert extends ImagickOperationBase {
protected function arguments() {
return [
'format' => [
'description' => 'Image format.',
],
'quality' => [
'description' => 'Image quality.',
],
];
}
protected function validateArguments(array $arguments) {
if (!in_array($arguments['format'], $this
->getToolkit()
->getSupportedExtensions())) {
throw new \InvalidArgumentException("Invalid extension ({$arguments['format']}) specified for the image 'convert' operation");
}
return $arguments;
}
protected function execute(array $arguments = []) {
$resource = $this
->getToolkit()
->getResource();
$format = strtoupper($arguments['format']);
$quality = $arguments['quality'];
if (in_array($format, [
'JPEG',
'JPG',
'JPE',
])) {
$background = new Imagick();
$background
->newImage($resource
->getImageWidth(), $resource
->getImageHeight(), 'white');
$resource
->compositeImage($background, Imagick::COMPOSITE_DSTOVER, 0, 0);
}
$formatSuccess = $resource
->setImageFormat($format);
$qualitySuccess = $resource
->setImageProperty('quality', (int) $quality);
return $formatSuccess && $qualitySuccess;
}
}
Classes
Name |
Description |
Convert |
Defines imagick convert operation. |