Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / system / src / Plugin / ImageToolkit / Operation / gd / Resize.php
1 <?php
2
3 namespace Drupal\system\Plugin\ImageToolkit\Operation\gd;
4
5 /**
6  * Defines GD2 resize operation.
7  *
8  * @ImageToolkitOperation(
9  *   id = "gd_resize",
10  *   toolkit = "gd",
11  *   operation = "resize",
12  *   label = @Translation("Resize"),
13  *   description = @Translation("Resizes an image to the given dimensions (ignoring aspect ratio).")
14  * )
15  */
16 class Resize extends GDImageToolkitOperationBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function arguments() {
22     return [
23       'width' => [
24         'description' => 'The new width of the resized image, in pixels',
25       ],
26       'height' => [
27         'description' => 'The new height of the resized image, in pixels',
28       ],
29     ];
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function validateArguments(array $arguments) {
36     // Assure integers for all arguments.
37     $arguments['width'] = (int) round($arguments['width']);
38     $arguments['height'] = (int) round($arguments['height']);
39
40     // Fail when width or height are 0 or negative.
41     if ($arguments['width'] <= 0) {
42       throw new \InvalidArgumentException("Invalid width ('{$arguments['width']}') specified for the image 'resize' operation");
43     }
44     if ($arguments['height'] <= 0) {
45       throw new \InvalidArgumentException("Invalid height ('{$arguments['height']}') specified for the image 'resize' operation");
46     }
47
48     return $arguments;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   protected function execute(array $arguments = []) {
55     // Create a new resource of the required dimensions, and copy and resize
56     // the original resource on it with resampling. Destroy the original
57     // resource upon success.
58     $original_resource = $this->getToolkit()->getResource();
59     $data = [
60       'width' => $arguments['width'],
61       'height' => $arguments['height'],
62       'extension' => image_type_to_extension($this->getToolkit()->getType(), FALSE),
63       'transparent_color' => $this->getToolkit()->getTransparentColor(),
64       'is_temp' => TRUE,
65     ];
66     if ($this->getToolkit()->apply('create_new', $data)) {
67       if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, 0, 0, $arguments['width'], $arguments['height'], imagesx($original_resource), imagesy($original_resource))) {
68         imagedestroy($original_resource);
69         return TRUE;
70       }
71       else {
72         // In case of failure, destroy the temporary resource and restore
73         // the original one.
74         imagedestroy($this->getToolkit()->getResource());
75         $this->getToolkit()->setResource($original_resource);
76       }
77     }
78     return FALSE;
79   }
80
81 }