Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / src / Plugin / ImageToolkit / Operation / gd / ScaleAndCrop.php
1 <?php
2
3 namespace Drupal\system\Plugin\ImageToolkit\Operation\gd;
4
5 /**
6  * Defines GD2 Scale and crop operation.
7  *
8  * @ImageToolkitOperation(
9  *   id = "gd_scale_and_crop",
10  *   toolkit = "gd",
11  *   operation = "scale_and_crop",
12  *   label = @Translation("Scale and crop"),
13  *   description = @Translation("Scales an image to the exact width and height given. This plugin achieves the target aspect ratio by cropping the original image equally on both sides, or equally on the top and bottom. This function is useful to create uniform sized avatars from larger images.")
14  * )
15  */
16 class ScaleAndCrop extends GDImageToolkitOperationBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function arguments() {
22     return [
23       'x' => [
24         'description' => 'The horizontal offset for the start of the crop, in pixels',
25         'required' => FALSE,
26         'default' => NULL,
27       ],
28       'y' => [
29         'description' => 'The vertical offset for the start the crop, in pixels',
30         'required' => FALSE,
31         'default' => NULL,
32       ],
33       'width' => [
34         'description' => 'The target width, in pixels',
35       ],
36       'height' => [
37         'description' => 'The target height, in pixels',
38       ],
39     ];
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function validateArguments(array $arguments) {
46     $actualWidth = $this->getToolkit()->getWidth();
47     $actualHeight = $this->getToolkit()->getHeight();
48
49     $scaleFactor = max($arguments['width'] / $actualWidth, $arguments['height'] / $actualHeight);
50
51     $arguments['x'] = isset($arguments['x']) ?
52       (int) round($arguments['x']) :
53       (int) round(($actualWidth * $scaleFactor - $arguments['width']) / 2);
54     $arguments['y'] = isset($arguments['y']) ?
55       (int) round($arguments['y']) :
56       (int) round(($actualHeight * $scaleFactor - $arguments['height']) / 2);
57     $arguments['resize'] = [
58       'width' => (int) round($actualWidth * $scaleFactor),
59       'height' => (int) round($actualHeight * $scaleFactor),
60     ];
61
62     // Fail when width or height are 0 or negative.
63     if ($arguments['width'] <= 0) {
64       throw new \InvalidArgumentException("Invalid width ('{$arguments['width']}') specified for the image 'scale_and_crop' operation");
65     }
66     if ($arguments['height'] <= 0) {
67       throw new \InvalidArgumentException("Invalid height ('{$arguments['height']}') specified for the image 'scale_and_crop' operation");
68     }
69
70     return $arguments;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   protected function execute(array $arguments = []) {
77     return $this->getToolkit()->apply('resize', $arguments['resize'])
78         && $this->getToolkit()->apply('crop', $arguments);
79   }
80
81 }