You are here

protected function JuiceboxFormatter::styleImage in Juicebox HTML5 Responsive Image Galleries 8.2

Same name and namespace in other branches
  1. 8.3 src/JuiceboxFormatter.php \Drupal\juicebox\JuiceboxFormatter::styleImage()

Utility to style an individual file entity for use in a Juicebox gallery.

This method can detect if the passed file is incompatible with Juicebox. If so it styles the output as a mimetype image icon representing the file type. Otherwise the item is styled normally with the passed image style.

Parameters

\Drupal\file\FileInterface $file: A file entity containing the image data to append Juicebox styled image data to.

string $style: The Drupal image style to apply to the item.

bool $check_compatible: Whether-or-not to detect if the item is compatible with Juicebox, and if so, substitute a mimetype icon for its output.

Return value

array The styled image data.

1 call to JuiceboxFormatter::styleImage()
JuiceboxFormatter::styleImageSrcData in src/JuiceboxFormatter.php
Utility to extract image source data.

File

src/JuiceboxFormatter.php, line 295

Class

JuiceboxFormatter
Class to define a Drupal service with common formatter methods.

Namespace

Drupal\juicebox

Code

protected function styleImage(FileInterface $file, $style, $check_compatible = TRUE) {
  $global_settings = $this
    ->getGlobalSettings();
  $library = $this
    ->getLibrary();
  $mimetype = $file
    ->getMimeType();
  $image_data = [];
  $image_data['juicebox_compatible'] = TRUE;

  // Set the normal, unstyled, url for reference.
  $image_data['unstyled_src'] = file_create_url($file
    ->getFileUri());

  // Check compatibility if configured and if the library info contains
  // mimetype compatibitly information.
  if ($check_compatible && !empty($library['compatible_mimetypes']) && !in_array($mimetype, $library['compatible_mimetypes'])) {

    // If the item is not compatible, find the substitute mimetype icon.
    $image_data['juicebox_compatible'] = FALSE;
    $icon_dir = drupal_get_path('module', 'juicebox') . '/images/mimetypes';

    // We only have icons for each major type, so simplify accordingly.
    // file_icon_class() could also be useful here but would require
    // supporting icons for more package types.
    $type_parts = explode('/', $mimetype);
    $icon_path = $icon_dir . '/' . reset($type_parts) . '.png';
    if (file_exists($icon_path)) {
      $image_data['imageURL'] = file_create_url($icon_path);
    }
    else {
      $image_data['imageURL'] = file_create_url($icon_dir . '/general.png');
    }
  }
  else {
    $sizes = [
      'imageURL' => $style,
    ];

    // The "juicebox_multisize" style is special, and actually consists of 3
    // individual styles configured globally.
    if ($style == 'juicebox_multisize') {
      $sizes = [
        'smallImageURL' => $global_settings['juicebox_multisize_small'],
        'imageURL' => $global_settings['juicebox_multisize_medium'],
        'largeImageURL' => $global_settings['juicebox_multisize_large'],
      ];
    }
    foreach ($sizes as $size => $style_each) {
      if (!empty($style_each)) {
        $style_obj = ImageStyle::load($style_each);
        if ($style_obj) {
          $image_data[$size] = $style_obj
            ->buildUrl($file
            ->getFileUri());
        }
      }
      else {
        $image_data[$size] = $image_data['unstyled_src'];
      }
    }
  }
  return $image_data;
}