3 namespace Drupal\image_test\Plugin\ImageToolkit;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\ImageToolkit\ImageToolkitBase;
9 use Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface;
10 use Drupal\Core\State\StateInterface;
11 use Psr\Log\LoggerInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Defines a Test toolkit for image manipulation within Drupal.
19 * title = @Translation("A dummy toolkit that works")
22 class TestToolkit extends ImageToolkitBase {
27 * @var \Drupal\Core\State\StateInterface
32 * Image type represented by a PHP IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG).
39 * The width of the image.
46 * The height of the image.
53 * Constructs a TestToolkit object.
55 * @param array $configuration
56 * A configuration array containing information about the plugin instance.
57 * @param string $plugin_id
58 * The plugin_id for the plugin instance.
59 * @param array $plugin_definition
60 * The plugin implementation definition.
61 * @param \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface $operation_manager
62 * The toolkit operation manager.
63 * @param \Psr\Log\LoggerInterface $logger
65 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
67 * @param \Drupal\Core\State\StateInterface $state
68 * The state key value store.
70 public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory, StateInterface $state) {
71 parent::__construct($configuration, $plugin_id, $plugin_definition, $operation_manager, $logger, $config_factory);
72 $this->state = $state;
78 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
83 $container->get('image.toolkit.operation.manager'),
84 $container->get('logger.channel.image'),
85 $container->get('config.factory'),
86 $container->get('state')
93 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
94 $this->logCall('settings', func_get_args());
95 $form['test_parameter'] = [
97 '#title' => $this->t('Test toolkit parameter'),
98 '#description' => $this->t('A toolkit parameter for testing purposes.'),
101 '#default_value' => $this->configFactory->getEditable('system.image.test_toolkit')->get('test_parameter', FALSE),
109 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
110 if ($form_state->getValue(['test', 'test_parameter']) == 0) {
111 $form_state->setErrorByName('test][test_parameter', $this->t('Test parameter should be different from 0.'));
118 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
119 $this->configFactory->getEditable('system.image.test_toolkit')
120 ->set('test_parameter', $form_state->getValue(['test', 'test_parameter']))
127 public function isValid() {
128 return isset($this->type);
134 public function parseFile() {
135 $this->logCall('parseFile', func_get_args());
136 $data = @getimagesize($this->getSource());
137 if ($data && in_array($data[2], static::supportedTypes())) {
138 $this->setType($data[2]);
139 $this->width = $data[0];
140 $this->height = $data[1];
149 public function save($destination) {
150 $this->logCall('save', func_get_args());
151 // Return false so that image_save() doesn't try to chmod the destination
152 // file that we didn't bother to create.
157 * Stores the values passed to a toolkit call.
160 * One of the toolkit methods 'parseFile', 'save', 'settings', or 'apply'.
162 * Values passed to hook.
164 * @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestReset()
165 * @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestGetAllCalls()
167 protected function logCall($op, $args) {
168 $results = $this->state->get('image_test.results') ?: [];
169 $results[$op][] = $args;
170 // A call to apply is also logged under its operation name whereby the
171 // array of arguments are logged as separate arguments, this because at the
172 // ImageInterface level we still have methods named after the operations.
173 if ($op === 'apply') {
174 $operation = array_shift($args);
175 $results[$operation][] = array_values(reset($args));
177 $this->state->set('image_test.results', $results);
183 public function getWidth() {
190 public function getHeight() {
191 return $this->height;
195 * Returns the type of the image.
198 * The image type represented by a PHP IMAGETYPE_* constant (e.g.
201 public function getType() {
206 * Sets the PHP type of the image.
209 * The image type represented by a PHP IMAGETYPE_* constant (e.g.
214 public function setType($type) {
215 if (in_array($type, static::supportedTypes())) {
224 public function getMimeType() {
225 return $this->getType() ? image_type_to_mime_type($this->getType()) : '';
231 public static function isAvailable() {
238 public static function getSupportedExtensions() {
240 foreach (static::supportedTypes() as $image_type) {
241 $extensions[] = Unicode::strtolower(image_type_to_extension($image_type, FALSE));
247 * Returns a list of image types supported by the toolkit.
250 * An array of available image types. An image type is represented by a PHP
251 * IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.).
253 protected static function supportedTypes() {
254 return [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF];
260 public function apply($operation, array $arguments = []) {
261 $this->logCall('apply', func_get_args());