ContrastImageEffect.php in Image Effects 8.3
File
src/Plugin/ImageEffect/ContrastImageEffect.php
View source
<?php
namespace Drupal\image_effects\Plugin\ImageEffect;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\image\ConfigurableImageEffectBase;
class ContrastImageEffect extends ConfigurableImageEffectBase implements ContainerFactoryPluginInterface {
public function defaultConfiguration() {
return [
'level' => 0,
] + parent::defaultConfiguration();
}
public function getSummary() {
return [
'#theme' => 'image_effects_contrast_summary',
'#data' => $this->configuration,
] + parent::getSummary();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['level'] = [
'#type' => 'number',
'#title' => $this
->t('Contrast'),
'#description' => $this
->t('The contrast effect enhances the intensity differences between the lighter and darker elements of the image.'),
'#default_value' => $this->configuration['level'],
'#field_prefix' => $this
->t('±'),
'#field_suffix' => $this
->t('%'),
'#required' => TRUE,
'#size' => 5,
'#min' => -100,
'#max' => 100,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['level'] = $form_state
->getValue('level');
}
public function applyEffect(ImageInterface $image) {
return $image
->apply('contrast', [
'level' => $this->configuration['level'],
]);
}
}