Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\image\Plugin\ImageEffect;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
6 use Drupal\Core\Image\ImageInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Crops an image resource.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @ImageEffect(
|
Chris@0
|
12 * id = "image_crop",
|
Chris@0
|
13 * label = @Translation("Crop"),
|
Chris@0
|
14 * description = @Translation("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.")
|
Chris@0
|
15 * )
|
Chris@0
|
16 */
|
Chris@0
|
17 class CropImageEffect extends ResizeImageEffect {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * {@inheritdoc}
|
Chris@0
|
21 */
|
Chris@0
|
22 public function applyEffect(ImageInterface $image) {
|
Chris@0
|
23 list($x, $y) = explode('-', $this->configuration['anchor']);
|
Chris@0
|
24 $x = image_filter_keyword($x, $image->getWidth(), $this->configuration['width']);
|
Chris@0
|
25 $y = image_filter_keyword($y, $image->getHeight(), $this->configuration['height']);
|
Chris@0
|
26 if (!$image->crop($x, $y, $this->configuration['width'], $this->configuration['height'])) {
|
Chris@0
|
27 $this->logger->error('Image 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
|
28 return FALSE;
|
Chris@0
|
29 }
|
Chris@0
|
30 return TRUE;
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * {@inheritdoc}
|
Chris@0
|
35 */
|
Chris@0
|
36 public function getSummary() {
|
Chris@0
|
37 $summary = [
|
Chris@0
|
38 '#theme' => 'image_crop_summary',
|
Chris@0
|
39 '#data' => $this->configuration,
|
Chris@0
|
40 ];
|
Chris@0
|
41 $summary += parent::getSummary();
|
Chris@0
|
42
|
Chris@0
|
43 return $summary;
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * {@inheritdoc}
|
Chris@0
|
48 */
|
Chris@0
|
49 public function defaultConfiguration() {
|
Chris@0
|
50 return parent::defaultConfiguration() + [
|
Chris@0
|
51 'anchor' => 'center-center',
|
Chris@0
|
52 ];
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * {@inheritdoc}
|
Chris@0
|
57 */
|
Chris@0
|
58 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
59 $form = parent::buildConfigurationForm($form, $form_state);
|
Chris@0
|
60 $form['anchor'] = [
|
Chris@0
|
61 '#type' => 'radios',
|
Chris@0
|
62 '#title' => t('Anchor'),
|
Chris@0
|
63 '#options' => [
|
Chris@0
|
64 'left-top' => t('Top left'),
|
Chris@0
|
65 'center-top' => t('Top center'),
|
Chris@0
|
66 'right-top' => t('Top right'),
|
Chris@0
|
67 'left-center' => t('Center left'),
|
Chris@0
|
68 'center-center' => t('Center'),
|
Chris@0
|
69 'right-center' => t('Center right'),
|
Chris@0
|
70 'left-bottom' => t('Bottom left'),
|
Chris@0
|
71 'center-bottom' => t('Bottom center'),
|
Chris@0
|
72 'right-bottom' => t('Bottom right'),
|
Chris@0
|
73 ],
|
Chris@0
|
74 '#theme' => 'image_anchor',
|
Chris@0
|
75 '#default_value' => $this->configuration['anchor'],
|
Chris@0
|
76 '#description' => t('The part of the image that will be retained during the crop.'),
|
Chris@0
|
77 ];
|
Chris@0
|
78 return $form;
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * {@inheritdoc}
|
Chris@0
|
83 */
|
Chris@0
|
84 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
85 parent::submitConfigurationForm($form, $form_state);
|
Chris@0
|
86
|
Chris@0
|
87 $this->configuration['anchor'] = $form_state->getValue('anchor');
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 }
|