You are here

public function AutoAspectEffect::applyEffect in Auto image style 8

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

src/Plugin/ImageEffect/AutoAspectEffect.php, line 25

Class

AutoAspectEffect
Resizes an image resource.

Namespace

Drupal\auto_image_style\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 ImageStyle $style */
  $style = ImageStyle::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 ImageEffectInterface $sub_effect */
    $sub_effect
      ->applyEffect($image);
  }
  return TRUE;
}