Version 1
[yaffs-website] / web / core / modules / system / src / Plugin / ImageToolkit / Operation / gd / Crop.php
1 <?php
2
3 namespace Drupal\system\Plugin\ImageToolkit\Operation\gd;
4
5 /**
6  * Defines GD2 Crop operation.
7  *
8  * @ImageToolkitOperation(
9  *   id = "gd_crop",
10  *   toolkit = "gd",
11  *   operation = "crop",
12  *   label = @Translation("Crop"),
13  *   description = @Translation("Crops an image to a rectangle specified by the given dimensions.")
14  * )
15  */
16 class Crop extends GDImageToolkitOperationBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function arguments() {
22     return [
23       'x' => [
24         'description' => 'The starting x offset at which to start the crop, in pixels',
25       ],
26       'y' => [
27         'description' => 'The starting y offset at which to start the crop, in pixels',
28       ],
29       'width' => [
30         'description' => 'The width of the cropped area, in pixels',
31         'required' => FALSE,
32         'default' => NULL,
33       ],
34       'height' => [
35         'description' => 'The height of the cropped area, in pixels',
36         'required' => FALSE,
37         'default' => NULL,
38       ],
39     ];
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function validateArguments(array $arguments) {
46     // Assure at least one dimension.
47     if (empty($arguments['width']) && empty($arguments['height'])) {
48       throw new \InvalidArgumentException("At least one dimension ('width' or 'height') must be provided to the image 'crop' operation");
49     }
50
51     // Preserve aspect.
52     $aspect = $this->getToolkit()->getHeight() / $this->getToolkit()->getWidth();
53     $arguments['height'] = empty($arguments['height']) ? $arguments['width'] * $aspect : $arguments['height'];
54     $arguments['width'] = empty($arguments['width']) ? $arguments['height'] / $aspect : $arguments['width'];
55
56     // Assure integers for all arguments.
57     foreach (['x', 'y', 'width', 'height'] as $key) {
58       $arguments[$key] = (int) round($arguments[$key]);
59     }
60
61     // Fail when width or height are 0 or negative.
62     if ($arguments['width'] <= 0) {
63       throw new \InvalidArgumentException("Invalid width ('{$arguments['width']}') specified for the image 'crop' operation");
64     }
65     if ($arguments['height'] <= 0) {
66       throw new \InvalidArgumentException("Invalid height ('{$arguments['height']}') specified for the image 'crop' operation");
67     }
68
69     return $arguments;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   protected function execute(array $arguments) {
76     // Create a new resource of the required dimensions, and copy and resize
77     // the original resource on it with resampling. Destroy the original
78     // resource upon success.
79     $original_resource = $this->getToolkit()->getResource();
80     $data = [
81       'width' => $arguments['width'],
82       'height' => $arguments['height'],
83       'extension' => image_type_to_extension($this->getToolkit()->getType(), FALSE),
84       'transparent_color' => $this->getToolkit()->getTransparentColor(),
85       'is_temp' => TRUE,
86     ];
87     if ($this->getToolkit()->apply('create_new', $data)) {
88       if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, $arguments['x'], $arguments['y'], $arguments['width'], $arguments['height'], $arguments['width'], $arguments['height'])) {
89         imagedestroy($original_resource);
90         return TRUE;
91       }
92       else {
93         // In case of failure, destroy the temporary resource and restore
94         // the original one.
95         imagedestroy($this->getToolkit()->getResource());
96         $this->getToolkit()->setResource($original_resource);
97       }
98     }
99     return FALSE;
100   }
101
102 }