You are here

function varbase_media_preprocess_image in Varbase Media 8.7

Same name and namespace in other branches
  1. 9.0.x varbase_media.module \varbase_media_preprocess_image()

Implements hook_preprocess_image().

File

./varbase_media.module, line 818
Contains varbase_media.module.

Code

function varbase_media_preprocess_image(&$variables) {

  // Add the Varbase Media Blazy Blurry behaviour.
  $use_blazy_blurry = \Drupal::config('varbase_media.settings')
    ->get('use_blazy_blurry');
  if (isset($use_blazy_blurry) && $use_blazy_blurry === TRUE) {
    if (!empty($variables['attributes']['class']) && !empty($variables['attributes']['uri'])) {
      $uri = $variables['attributes']['uri'];
      $current_uri_with_style = $uri;
      if (!empty($variables['attributes']['responsive_image_style'])) {
        $responsive_image_style = ResponsiveImageStyle::load($variables['attributes']['responsive_image_style']);

        // Create Uri from the fallback image style.
        $fallback_image_style_name = $responsive_image_style
          ->getFallbackImageStyle();
        $fallback_image_style = ImageStyle::load($fallback_image_style_name);

        // Set up derivative file information.
        $current_uri_with_style = $fallback_image_style
          ->buildUri($uri);

        // Create responsive image style derivative if necessary.
        if (!file_exists($current_uri_with_style)) {
          $fallback_image_style
            ->createDerivative($uri, $current_uri_with_style);
        }
      }
      elseif (!empty($variables['attributes']['image_style'])) {
        $image_style = ImageStyle::load($variables['attributes']['image_style']);

        // Set up derivative file information.
        $current_uri_with_style = $image_style
          ->buildUri($uri);

        // Create image style derivative if necessary.
        if (!file_exists($current_uri_with_style)) {
          $image_style
            ->createDerivative($uri, $current_uri_with_style);
        }
      }
      $image_style_blazy_blurry = ImageStyle::load('blazy_blurry');

      // Set up derivative file information.
      $blazy_blurry_image = $image_style_blazy_blurry
        ->buildUri($current_uri_with_style);

      // Create Blazy Blurry derivative if necessary.
      if (!file_exists($blazy_blurry_image)) {
        $image_style_blazy_blurry
          ->createDerivative($current_uri_with_style, $blazy_blurry_image);
      }
      if (file_exists($blazy_blurry_image)) {

        // Encode the image in base64 to speed up loading times.
        $file_type = mime_content_type($blazy_blurry_image);
        $blazy_blurry_image_base64 = 'data:' . $file_type . ';base64,' . base64_encode(file_get_contents($blazy_blurry_image));

        // Update the placeholder URI.
        $variables['attributes']['src'] = $blazy_blurry_image_base64;
        unset($variables['attributes']['responsive_image_style']);
        unset($variables['attributes']['image_style']);
        unset($variables['attributes']['uri']);
      }
    }
  }
}