Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\image\Plugin\ImageEffect;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Image\ImageInterface;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Scales and crops an image resource.
|
Chris@0
|
9 *
|
Chris@0
|
10 * @ImageEffect(
|
Chris@0
|
11 * id = "image_scale_and_crop",
|
Chris@0
|
12 * label = @Translation("Scale and crop"),
|
Chris@0
|
13 * description = @Translation("Scale and crop will maintain the aspect-ratio of the original image, then crop the larger dimension. This is most useful for creating perfectly square thumbnails without stretching the image.")
|
Chris@0
|
14 * )
|
Chris@0
|
15 */
|
Chris@17
|
16 class ScaleAndCropImageEffect extends CropImageEffect {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * {@inheritdoc}
|
Chris@0
|
20 */
|
Chris@0
|
21 public function applyEffect(ImageInterface $image) {
|
Chris@17
|
22 $width = $this->configuration['width'];
|
Chris@17
|
23 $height = $this->configuration['height'];
|
Chris@17
|
24 $scale = max($width / $image->getWidth(), $height / $image->getHeight());
|
Chris@17
|
25
|
Chris@17
|
26 list($x, $y) = explode('-', $this->configuration['anchor']);
|
Chris@17
|
27 $x = image_filter_keyword($x, $image->getWidth() * $scale, $width);
|
Chris@17
|
28 $y = image_filter_keyword($y, $image->getHeight() * $scale, $height);
|
Chris@17
|
29
|
Chris@17
|
30 if (!$image->apply('scale_and_crop', ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height])) {
|
Chris@0
|
31 $this->logger->error('Image scale and crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
|
Chris@0
|
32 return FALSE;
|
Chris@0
|
33 }
|
Chris@0
|
34 return TRUE;
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@17
|
37 /**
|
Chris@17
|
38 * {@inheritdoc}
|
Chris@17
|
39 */
|
Chris@17
|
40 public function getSummary() {
|
Chris@17
|
41 $summary = [
|
Chris@17
|
42 '#theme' => 'image_scale_and_crop_summary',
|
Chris@17
|
43 '#data' => $this->configuration,
|
Chris@17
|
44 ];
|
Chris@17
|
45 $summary += parent::getSummary();
|
Chris@17
|
46
|
Chris@17
|
47 return $summary;
|
Chris@17
|
48 }
|
Chris@17
|
49
|
Chris@0
|
50 }
|