7015d4a5ec2724c18a0afc6c84799c408934084e
[yaffs-website] / web / modules / contrib / imagemagick / src / Plugin / ImageToolkit / Operation / imagemagick / Convert.php
1 <?php
2
3 namespace Drupal\imagemagick\Plugin\ImageToolkit\Operation\imagemagick;
4
5 /**
6  * Defines imagemagick Convert operation.
7  *
8  * @ImageToolkitOperation(
9  *   id = "imagemagick_convert",
10  *   toolkit = "imagemagick",
11  *   operation = "convert",
12  *   label = @Translation("Convert"),
13  *   description = @Translation("Instructs the toolkit to save the image with a specified format.")
14  * )
15  */
16 class Convert extends ImagemagickImageToolkitOperationBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function arguments() {
22     return [
23       'extension' => [
24         'description' => 'The new extension of the converted image',
25       ],
26     ];
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function validateArguments(array $arguments) {
33     if (!in_array($arguments['extension'], $this->getToolkit()->getSupportedExtensions())) {
34       throw new \InvalidArgumentException("Invalid extension ({$arguments['extension']}) specified for the image 'convert' operation");
35     }
36     return $arguments;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function execute(array $arguments) {
43     // When source image is multi-frame, convert only the first frame.
44     if ($this->getToolkit()->getFrames()) {
45       $path = $this->getToolkit()->getSourceLocalPath();
46       if (strripos($path, '[0]', -3) === FALSE) {
47         $this->getToolkit()->setSourceLocalPath($path . '[0]');
48       }
49     }
50     $this->getToolkit()
51       ->setFrames(NULL)
52       ->setDestinationFormatFromExtension($arguments['extension']);
53     return TRUE;
54   }
55
56 }