class CropImageEffect

Same name in other branches
  1. 9 core/modules/image/src/Plugin/ImageEffect/CropImageEffect.php \Drupal\image\Plugin\ImageEffect\CropImageEffect
  2. 8.9.x core/modules/image/src/Plugin/ImageEffect/CropImageEffect.php \Drupal\image\Plugin\ImageEffect\CropImageEffect
  3. 11.x core/modules/image/src/Plugin/ImageEffect/CropImageEffect.php \Drupal\image\Plugin\ImageEffect\CropImageEffect

Crops an image resource.

Hierarchy

Expanded class hierarchy of CropImageEffect

File

core/modules/image/src/Plugin/ImageEffect/CropImageEffect.php, line 13

Namespace

Drupal\image\Plugin\ImageEffect
View source
class CropImageEffect extends ResizeImageEffect {
    
    /**
     * {@inheritdoc}
     */
    public function applyEffect(ImageInterface $image) {
        [
            $x,
            $y,
        ] = explode('-', $this->configuration['anchor']);
        $x = image_filter_keyword($x, $image->getWidth(), $this->configuration['width']);
        $y = image_filter_keyword($y, $image->getHeight(), $this->configuration['height']);
        if (!$image->crop($x, $y, $this->configuration['width'], $this->configuration['height'])) {
            $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(),
            ]);
            return FALSE;
        }
        return TRUE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getSummary() {
        $summary = [
            '#theme' => 'image_crop_summary',
            '#data' => $this->configuration,
        ];
        $summary += parent::getSummary();
        return $summary;
    }
    
    /**
     * {@inheritdoc}
     */
    public function defaultConfiguration() {
        return parent::defaultConfiguration() + [
            'anchor' => 'center-center',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
        $form = parent::buildConfigurationForm($form, $form_state);
        $form['anchor'] = [
            '#type' => 'radios',
            '#title' => $this->t('Anchor'),
            '#options' => [
                'left-top' => $this->t('Top left'),
                'center-top' => $this->t('Top center'),
                'right-top' => $this->t('Top right'),
                'left-center' => $this->t('Center left'),
                'center-center' => $this->t('Center'),
                'right-center' => $this->t('Center right'),
                'left-bottom' => $this->t('Bottom left'),
                'center-bottom' => $this->t('Bottom center'),
                'right-bottom' => $this->t('Bottom right'),
            ],
            '#theme' => 'image_anchor',
            '#default_value' => $this->configuration['anchor'],
            '#description' => $this->t('The part of the image that will be retained during the crop.'),
        ];
        return $form;
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
        parent::submitConfigurationForm($form, $form_state);
        $this->configuration['anchor'] = $form_state->getValue('anchor');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ConfigurableImageEffectBase::validateConfigurationForm public function 2
CropImageEffect::applyEffect public function Applies an image effect to the image object. Overrides ResizeImageEffect::applyEffect 1
CropImageEffect::buildConfigurationForm public function Overrides ResizeImageEffect::buildConfigurationForm
CropImageEffect::defaultConfiguration public function Gets default configuration for this plugin. Overrides ResizeImageEffect::defaultConfiguration
CropImageEffect::getSummary public function Returns a render array summarizing the configuration of the image effect. Overrides ResizeImageEffect::getSummary 1
CropImageEffect::submitConfigurationForm public function Overrides ResizeImageEffect::submitConfigurationForm
ImageEffectBase::$logger protected property A logger instance.
ImageEffectBase::$uuid protected property The image effect ID.
ImageEffectBase::$weight protected property The weight of the image effect.
ImageEffectBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ImageEffectBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ImageEffectBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ImageEffectBase::getDerivativeExtension public function Returns the extension of the derivative after applying this image effect. Overrides ImageEffectInterface::getDerivativeExtension 1
ImageEffectBase::getUuid public function Returns the unique ID representing the image effect. Overrides ImageEffectInterface::getUuid
ImageEffectBase::getWeight public function Returns the weight of the image effect. Overrides ImageEffectInterface::getWeight
ImageEffectBase::label public function Returns the image effect label. Overrides ImageEffectInterface::label
ImageEffectBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ImageEffectBase::setWeight public function Sets the weight for this image effect. Overrides ImageEffectInterface::setWeight
ImageEffectBase::__construct public function
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin_id of the plugin instance. 2
ResizeImageEffect::transformDimensions public function Determines the dimensions of the styled image. Overrides ImageEffectBase::transformDimensions 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.