3 namespace Drupal\image\Plugin\ImageEffect;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Image\ImageInterface;
9 * Crops an image resource.
13 * label = @Translation("Crop"),
14 * description = @Translation("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.")
17 class CropImageEffect extends ResizeImageEffect {
22 public function applyEffect(ImageInterface $image) {
23 list($x, $y) = explode('-', $this->configuration['anchor']);
24 $x = image_filter_keyword($x, $image->getWidth(), $this->configuration['width']);
25 $y = image_filter_keyword($y, $image->getHeight(), $this->configuration['height']);
26 if (!$image->crop($x, $y, $this->configuration['width'], $this->configuration['height'])) {
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()]);
36 public function getSummary() {
38 '#theme' => 'image_crop_summary',
39 '#data' => $this->configuration,
41 $summary += parent::getSummary();
49 public function defaultConfiguration() {
50 return parent::defaultConfiguration() + [
51 'anchor' => 'center-center',
58 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
59 $form = parent::buildConfigurationForm($form, $form_state);
62 '#title' => t('Anchor'),
64 'left-top' => t('Top left'),
65 'center-top' => t('Top center'),
66 'right-top' => t('Top right'),
67 'left-center' => t('Center left'),
68 'center-center' => t('Center'),
69 'right-center' => t('Center right'),
70 'left-bottom' => t('Bottom left'),
71 'center-bottom' => t('Bottom center'),
72 'right-bottom' => t('Bottom right'),
74 '#theme' => 'image_anchor',
75 '#default_value' => $this->configuration['anchor'],
76 '#description' => t('The part of the image that will be retained during the crop.'),
84 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
85 parent::submitConfigurationForm($form, $form_state);
87 $this->configuration['anchor'] = $form_state->getValue('anchor');