You are here

public static function Blazy::aspectRatioAttributes in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/Blazy.php \Drupal\blazy\Blazy::aspectRatioAttributes()

Modifies container attributes with aspect ratio.

1 call to Blazy::aspectRatioAttributes()
BlazyManager::buildImage in src/BlazyManager.php
Build out image, or anything related, including cache, CSS background, etc.

File

src/Blazy.php, line 179

Class

Blazy
Implements BlazyInterface.

Namespace

Drupal\blazy

Code

public static function aspectRatioAttributes(array &$attributes, array &$settings) {
  $attributes['class'][] = 'media--ratio media--ratio--' . $settings['ratio'];
  if ($settings['width'] && in_array($settings['ratio'], [
    'enforced',
    'fluid',
  ])) {

    // If "lucky", Blazy/ Slick Views galleries may already set this once.
    // Lucky when you don't flatten out the Views output earlier.
    $padding = $settings['padding_bottom'] ?: round($settings['height'] / $settings['width'] * 100, 2);
    self::inlineStyle($attributes, 'padding-bottom: ' . $padding . '%;');

    // Provides hint to breakpoints to work with multi-breakpoint ratio.
    $settings['_breakpoint_ratio'] = $settings['ratio'];

    // Views rewrite results or Twig inline_template may strip out `style`
    // attributes, provide hint to JS.
    $attributes['data-ratio'] = $padding;
  }
}