CreateNew.php in ImageMagick 8.2
File
src/Plugin/ImageToolkit/Operation/imagemagick/CreateNew.php
View source
<?php
namespace Drupal\imagemagick\Plugin\ImageToolkit\Operation\imagemagick;
use Drupal\Component\Utility\Color;
class CreateNew extends ImagemagickImageToolkitOperationBase {
protected function arguments() {
return [
'width' => [
'description' => 'The width of the image, in pixels',
],
'height' => [
'description' => 'The height of the image, in pixels',
],
'extension' => [
'description' => 'The extension of the image file (e.g. png, gif, etc.)',
'required' => FALSE,
'default' => 'png',
],
'transparent_color' => [
'description' => 'The RGB hex color for GIF transparency',
'required' => FALSE,
'default' => '#ffffff',
],
];
}
protected function validateArguments(array $arguments) {
if (!in_array($arguments['extension'], $this
->getToolkit()
->getSupportedExtensions())) {
throw new \InvalidArgumentException("Invalid extension ('{$arguments['extension']}') specified for the image 'create_new' operation");
}
$arguments['width'] = (int) round($arguments['width']);
$arguments['height'] = (int) round($arguments['height']);
if ($arguments['width'] <= 0) {
throw new \InvalidArgumentException("Invalid width ('{$arguments['width']}') specified for the image 'create_new' operation");
}
if ($arguments['height'] <= 0) {
throw new \InvalidArgumentException("Invalid height ({$arguments['height']}) specified for the image 'create_new' operation");
}
if ($arguments['transparent_color'] && !Color::validateHex($arguments['transparent_color'])) {
throw new \InvalidArgumentException("Invalid transparent color ({$arguments['transparent_color']}) specified for the image 'create_new' operation");
}
return $arguments;
}
protected function execute(array $arguments) {
$this
->getToolkit()
->setWidth($arguments['width'])
->setHeight($arguments['height'])
->setExifOrientation(NULL)
->setColorspace($this
->getToolkit()
->getExecManager()
->getPackage() === 'imagemagick' ? 'sRGB' : NULL)
->setProfiles([])
->setFrames(1);
$this
->getToolkit()
->arguments()
->setSourceFormatFromExtension($arguments['extension'])
->setSourceLocalPath('')
->reset();
$arg = '-size ' . $arguments['width'] . 'x' . $arguments['height'];
if ($arguments['extension'] === 'gif') {
switch ($this
->getToolkit()
->getExecManager()
->getPackage()) {
case 'imagemagick':
$arg .= ' xc:transparent -transparent-color ' . $this
->escapeArgument($arguments['transparent_color']);
break;
case 'graphicsmagick':
$arg .= ' xc:' . $this
->escapeArgument($arguments['transparent_color']) . ' -transparent ' . $this
->escapeArgument($arguments['transparent_color']);
break;
}
}
else {
$arg .= ' xc:transparent';
}
$this
->addArgument($arg);
return TRUE;
}
}
Classes
Name |
Description |
CreateNew |
Defines imagemagick CreateNew operation. |