CreateNew.php in ImageMagick 8
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()
->resetArguments()
->setSourceLocalPath('')
->setSourceFormatFromExtension($arguments['extension'])
->setWidth($arguments['width'])
->setHeight($arguments['height'])
->setExifOrientation(NULL)
->setFrames(NULL);
$arg = '-size ' . $arguments['width'] . 'x' . $arguments['height'];
if ($arguments['extension'] === 'gif') {
switch ($this
->getToolkit()
->getPackage()) {
case 'imagemagick':
$arg .= ' xc:transparent -transparent-color ' . $this
->getToolkit()
->escapeShellArg($arguments['transparent_color']);
break;
case 'graphicsmagick':
$arg .= ' xc:' . $this
->getToolkit()
->escapeShellArg($arguments['transparent_color']) . ' -transparent ' . $this
->getToolkit()
->escapeShellArg($arguments['transparent_color']);
break;
}
}
else {
$arg .= ' xc:transparent';
}
$this
->getToolkit()
->addArgument($arg);
return TRUE;
}
}
Classes
Name |
Description |
CreateNew |
Defines imagemagick CreateNew operation. |