Replace.php in Image Replace 8
File
src/Plugin/ImageToolkit/Operation/gd/Replace.php
View source
<?php
namespace Drupal\image_replace\Plugin\ImageToolkit\Operation\gd;
use Drupal\Core\Image\ImageInterface;
use Drupal\system\Plugin\ImageToolkit\GDToolkit;
use Drupal\system\Plugin\ImageToolkit\Operation\gd\GDImageToolkitOperationBase;
class Replace extends GDImageToolkitOperationBase {
protected function arguments() {
return [
'replacement_image' => [
'description' => 'The replacement image',
],
];
}
protected function validateArguments(array $arguments) {
if (!($arguments['replacement_image'] instanceof ImageInterface || !$arguments['replacement_image']
->getToolkit() instanceof GDToolkit)) {
throw new \InvalidArgumentException("Invalid replacement image specified for the 'image_replace' operation.");
}
return $arguments;
}
protected function execute(array $arguments = []) {
$replacement_toolkit = $this
->getReplacementImageToolkit($arguments);
$original_resource = $this
->getToolkit()
->getResource();
$data = [
'width' => $replacement_toolkit
->getWidth(),
'height' => $replacement_toolkit
->getHeight(),
'extension' => image_type_to_extension($this
->getToolkit()
->getType(), FALSE),
'transparent_color' => $replacement_toolkit
->getTransparentColor(),
'is_temp' => TRUE,
];
if ($this
->getToolkit()
->apply('create_new', $data)) {
if (imagecopy($this
->getToolkit()
->getResource(), $replacement_toolkit
->getResource(), 0, 0, 0, 0, $data['width'], $data['height'])) {
imagedestroy($original_resource);
return TRUE;
}
else {
imagedestroy($this
->getToolkit()
->getResource());
$this
->getToolkit()
->setResource($original_resource);
}
}
return FALSE;
}
protected function getReplacementImageToolkit(array $arguments = []) {
return $arguments['replacement_image']
->getToolkit();
}
}
Classes
Name |
Description |
Replace |
Defines GD2 image_replace operation. |