You are here

function theme_picture_sizes_formatter in Picture 7.2

Theme pictue sizes formatter.

1 theme call to theme_picture_sizes_formatter()
picture_field_formatter_picture_sizes_formatter_view in ./picture.module
Helper function.

File

./picture.module, line 1214
Picture formatter.

Code

function theme_picture_sizes_formatter($variables) {
  $output_method = variable_get('picture_img_sizes_output_method', 'picture_element');
  $variables['image_style'] = $variables['fallback_image_style'];
  if ($output_method == 'picture_element') {
    $variables['breakpoints'][BREAKPOINTS_SOURCE_TYPE_MODULE . '.picture.empty_srcset']['1x'] = array(
      'mapping_type' => 'sizes',
      'sizes' => $variables['sizes'],
      'sizes_image_styles' => $variables['image_styles'],
      'lazyload' => !empty($variables['lazyload']),
      'lazyload_aspect_ratio' => !empty($variables['lazyload_aspect_ratio']),
    );
    return theme('picture_formatter', $variables);
  }
  if ($output_method == 'img_srcset') {
    $image = $variables['item'];

    // Map image attributes.
    $variables['uri'] = _picture_image_style_url($variables['fallback_image_style'], $image['uri']);
    if (!empty($image['alt'])) {
      $variables['alt'] = $image['alt'];
    }
    if (!empty($image['title'])) {
      $variables['title'] = $image['title'];
    }
    if (!empty($image['attributes'])) {
      $variables['attributes'] = $image['attributes'];
    }

    // Check for lazyload_data_attributes.
    if (isset($variables['lazyload_data_attributes']) && $variables['lazyload_data_attributes']) {
      if (!isset($variables['attributes'])) {
        $variables['attributes'] = array();
      }
      if (!isset($variables['attributes']['class'])) {
        $variables['attributes']['class'] = array();
      }
      $variables['attributes']['class'] = array_merge($variables['attributes']['class'], explode(',', $variables['lazyload_class']));
    }

    // Set up image source options.
    $variables['srcset'] = array();

    // Get the dimensions of the original image.
    $image_info = image_get_info($image['uri']);
    foreach ($variables['image_styles'] as $image_style) {
      $image_style_uri = _picture_image_style_url($image_style, $image['uri']);
      $dimensions = array(
        'width' => $image_info['width'],
        'height' => $image_info['height'],
      );

      // Let the the image style transform the dimensions. This way we avoid the
      // performance cost of actually having to generate the derivative. The
      // image style can transform the dimensions without the derivative having
      // to exist.
      $dimensions = picture_get_image_dimensions($image_style, $dimensions);
      $variables['srcset'][] = array(
        'uri' => $image_style_uri,
        'width' => $dimensions['width'] . 'w',
      );
    }
    $output = theme('image_srcset', $variables);
    if (isset($variables['lazyload_data_attributes']) && $variables['lazyload_data_attributes']) {
      $output = str_replace(array(
        'src="',
        'srcset="',
      ), array(
        'data-src="',
        'data-srcset="',
      ), $output);
    }
    return $output;
  }
}