You are here

public function AutoAspectEffect::applyEffect in Thunder 8.5

Same name and namespace in other branches
  1. 8.2 modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php \Drupal\thunder_media\Plugin\ImageEffect\AutoAspectEffect::applyEffect()
  2. 8.3 modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php \Drupal\thunder_media\Plugin\ImageEffect\AutoAspectEffect::applyEffect()
  3. 8.4 modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php \Drupal\thunder_media\Plugin\ImageEffect\AutoAspectEffect::applyEffect()
  4. 6.2.x modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php \Drupal\thunder_media\Plugin\ImageEffect\AutoAspectEffect::applyEffect()
  5. 6.0.x modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php \Drupal\thunder_media\Plugin\ImageEffect\AutoAspectEffect::applyEffect()
  6. 6.1.x modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php \Drupal\thunder_media\Plugin\ImageEffect\AutoAspectEffect::applyEffect()

Applies an image effect to the image object.

Parameters

\Drupal\Core\Image\ImageInterface $image: An image file object.

Return value

bool TRUE on success. FALSE if unable to perform the image effect on the image.

Overrides ImageEffectInterface::applyEffect

File

modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php, line 51

Class

AutoAspectEffect
Resizes an image resource.

Namespace

Drupal\thunder_media\Plugin\ImageEffect

Code

public function applyEffect(ImageInterface $image) {
  $ratio_adjustment = isset($this->configuration['ratio_adjustment']) ? floatval($this->configuration['ratio_adjustment']) : 1;
  $aspect = $image
    ->getWidth() / $image
    ->getHeight();

  // Calculate orientation: width / height * adjustment. If > 1, it's wide.
  $style_name = $aspect * $ratio_adjustment > 1 ? $this->configuration['landscape'] : $this->configuration['portrait'];
  if (empty($style_name)) {

    // Do nothing. just return what we've got.
    return TRUE;
  }

  /** @var \Drupal\image\ImageStyleInterface $style */
  $style = $this->entityTypeManager
    ->getStorage('image_style')
    ->load($style_name);
  if (empty($style)) {

    // Required preset has gone missing?
    return FALSE;
  }

  // Run the preset actions ourself.
  foreach ($style
    ->getEffects() as $sub_effect) {

    /** @var \Drupal\image\ImageEffectInterface $sub_effect */
    $sub_effect
      ->applyEffect($image);
  }
  return TRUE;
}