View source
<?php
namespace Drupal\image_effects\Plugin\ImageEffect;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Image\ImageInterface;
use Drupal\image\ConfigurableImageEffectBase;
use Drupal\image\Entity\ImageStyle;
class AspectSwitcherImageEffect extends ConfigurableImageEffectBase {
public function defaultConfiguration() {
return [
'landscape_image_style' => '',
'portrait_image_style' => '',
'ratio_adjustment' => 1,
];
}
public function getSummary() {
$data = $this->configuration;
if ($portrait_image_style = $this
->failSafeGetImageStyle($this->configuration['portrait_image_style'])) {
$data['portrait'] = $portrait_image_style
->label();
}
else {
$data['portrait'] = $this
->t("(none)");
}
if ($landscape_image_style = $this
->failSafeGetImageStyle($this->configuration['landscape_image_style'])) {
$data['landscape'] = $landscape_image_style
->label();
}
else {
$data['landscape'] = $this
->t("(none)");
}
return [
'#theme' => 'image_effects_aspect_switcher',
'#data' => $data,
] + parent::getSummary();
}
public function calculateDependencies() {
$image_styles = [];
if ($portrait_image_style = $this
->failSafeGetImageStyle($this->configuration['portrait_image_style'])) {
$image_styles[] = $portrait_image_style
->getConfigDependencyName();
}
if ($landscape_image_style = $this
->failSafeGetImageStyle($this->configuration['landscape_image_style'])) {
$image_styles[] = $landscape_image_style
->getConfigDependencyName();
}
return [
'config' => $image_styles,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['info'] = [
'#type' => 'details',
'#title' => $this
->t('Information'),
];
$form['info']['help'] = [
'#markup' => $this
->t("'Convert' effects included in the image style specified will not be effective. It is not possible to change the image format based on the aspect. If you need to change the image format, you will have to add a 'Convert' effect in this image style."),
];
$form['landscape_image_style'] = [
'#type' => 'entity_autocomplete',
'#title' => $this
->t('Landscape image style'),
'#target_type' => 'image_style',
'#default_value' => $this
->failSafeGetImageStyle($this->configuration['landscape_image_style']),
'#description' => $this
->t("Leave empty to avoid switching."),
];
$form['portrait_image_style'] = [
'#type' => 'entity_autocomplete',
'#title' => $this
->t('Portrait image style'),
'#target_type' => 'image_style',
'#default_value' => $this
->failSafeGetImageStyle($this->configuration['portrait_image_style']),
'#description' => $this
->t("Leave empty to avoid switching."),
];
$form['ratio_adjustment'] = [
'#type' => 'number',
'#title' => t('Ratio adjustment (advanced)'),
'#required' => TRUE,
'#size' => 7,
'#min' => 0,
'#max' => 5,
'#step' => 0.01,
'#default_value' => $this->configuration['ratio_adjustment'],
'#description' => $this
->t("This allows you to bend the rules for how different the proportions need to be to trigger the switch.") . "<br/>" . $this
->t("If n = (width/height)*ratio is greater than 1, use 'landscape', otherwise use 'portrait'.") . "<br/>" . $this
->t("When ratio = 1 (the default) it will just switch between portrait and landscape modes."),
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state);
if ($form_state
->getValue('portrait_image_style') === NULL && $form_state
->getValue('landscape_image_style') === NULL) {
$form_state
->setErrorByName('portrait_image_style', $this
->t("At least one of 'Landscape image style' or 'Portrait image style' must be selected."));
$form_state
->setErrorByName('landscape_image_style', $this
->t("At least one of 'Landscape image style' or 'Portrait image style' must be selected."));
}
if ($this
->failSafeGetImageStyle($form_state
->getValue('portrait_image_style')) === FALSE) {
$form_state
->setErrorByName('portrait_image_style', $this
->t("The image style does not exist."));
}
if ($this
->failSafeGetImageStyle($form_state
->getValue('landscape_image_style')) === FALSE) {
$form_state
->setErrorByName('landscape_image_style', $this
->t("The image style does not exist."));
}
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['portrait_image_style'] = $form_state
->getValue('portrait_image_style');
$this->configuration['landscape_image_style'] = $form_state
->getValue('landscape_image_style');
$this->configuration['ratio_adjustment'] = $form_state
->getValue('ratio_adjustment');
}
public function applyEffect(ImageInterface $image) {
$style_name = $this
->getChildImageStyleToExecute($image
->getWidth(), $image
->getHeight());
$style = $this
->failSafeGetImageStyle($style_name);
if ($style === NULL) {
return TRUE;
}
if ($style === FALSE) {
return FALSE;
}
foreach ($style
->getEffects() as $effect) {
$effect
->applyEffect($image);
}
return TRUE;
}
public function transformDimensions(array &$dimensions, $uri) {
$style_name = $this
->getChildImageStyleToExecute($dimensions['width'], $dimensions['height']);
$style = $this
->failSafeGetImageStyle($style_name);
if (!$style) {
return;
}
foreach ($style
->getEffects() as $effect) {
$effect
->transformDimensions($dimensions, $uri);
}
}
protected function getChildImageStyleToExecute($width, $height) {
$ratio_adjustment = isset($this->configuration['ratio_adjustment']) ? floatval($this->configuration['ratio_adjustment']) : 1;
return $width / $height * $ratio_adjustment > 1 ? $this->configuration['landscape_image_style'] : $this->configuration['portrait_image_style'];
}
protected function failSafeGetImageStyle($image_style_name) {
if ($image_style_name === NULL) {
return NULL;
}
$image_style = ImageStyle::load($image_style_name);
if ($image_style === NULL) {
$this->logger
->error("Cannot find image style '%style_name' to execute an 'aspect switcher' effect.", [
'%style_name' => $image_style_name,
]);
return FALSE;
}
return $image_style;
}
}