You are here

function _picture_filter_prepare_image in Picture 7.2

Same name and namespace in other branches
  1. 7 picture.module \_picture_filter_prepare_image()

Prepares a Render Array for theme_picture_formatter().

It is similar to picture_field_formatter_view() with modifications for inline images.

Parameters

string $image: An img tag.

See also

picture_field_formatter_view()

1 call to _picture_filter_prepare_image()
_picture_filter_process in ./picture.module
Process callback for inline image filter.

File

./picture.module, line 1754
Picture formatter.

Code

function _picture_filter_prepare_image($image) {

  // Make sure the closing tag is right.
  $image = str_replace('/>', '>', $image);
  $image = str_replace('>', ' />', $image);
  $image = str_replace(" ", '', $image);
  $image = htmlspecialchars($image);

  // Parse the tag as xml.
  $xml = simplexml_load_string('<image>' . html_entity_decode($image, ENT_QUOTES, "utf-8") . '</image>');
  if (isset($xml->img[0]) && is_object($xml->img[0])) {
    $attributes = array();
    foreach ($xml->img[0]
      ->attributes() as $a => $b) {
      $attributes[$a] = (string) $b;
    }
  }
  $fallback_image_style = '';
  if (isset($attributes['data-picture-mapping'])) {
    $mapping_id = $attributes['data-picture-mapping'];
  }
  elseif (isset($attributes['data-picture-group'])) {
    $mapping_id = $attributes['data-picture-group'];
  }
  else {
    $mapping_id = null;
  }
  $mappings = picture_mapping_load($mapping_id);

  // Make sure we have valid mappings.
  if (empty($mappings)) {
    return FALSE;
  }
  $breakpoint_styles = picture_get_mapping_breakpoints($mappings, $fallback_image_style);

  // Make sure we have a src attribute.
  if (!isset($attributes['src'])) {
    return FALSE;
  }
  $src = $attributes['src'];
  unset($attributes['src']);
  $alt = isset($attributes['alt']) ? $attributes['alt'] : '';
  unset($attributes['alt']);
  $title = isset($attributes['title']) ? $attributes['title'] : '';
  unset($attributes['title']);

  // Ensure that class attributes are properly split into array.
  if (!empty($attributes['class']) && !is_array($attributes['class'])) {
    $attributes['class'] = array_filter(explode(' ', $attributes['class']));
  }

  // Make sure we have map src to uri.
  $uri = picture_image_uri($src);
  if (!$uri) {
    return FALSE;
  }
  $image_info = image_get_info($uri);

  // Check if stage_file_proxy is active.
  if (!$image_info && module_exists('stage_file_proxy')) {
    $local = stage_file_proxy_process_file_uri($uri);
    $image_info = image_get_info($uri);
  }
  if (!$image_info) {

    // It's not an image.
    return FALSE;
  }
  $picture_mappings = variable_get('picture_ckeditor_mappings', array());

  // Use srcset if possible and allowed.
  if (variable_get('picture_use_sizes_in_wysiwyg', FALSE)) {
    $bps = picture_get_mapping_breakpoints($mappings);
    $bp = reset($bps);
    $info = reset($bp);
    if (count($bps) == 1 && count($bp) == 1 && $info['mapping_type'] === 'image_style') {
      $image_render_array = array(
        '#theme' => 'image_style',
        '#path' => $uri,
        '#style_name' => $info['image_style'],
        '#alt' => $alt,
        '#title' => $title,
        '#attributes' => array(
          'data-picture-mapping' => $mapping_id,
        ) + $attributes,
      );
      return $image_render_array;
    }
    if (count($bps) == 1 && count($bp) == 1 && $info['mapping_type'] === 'sizes') {
      $image_styles = $info['sizes_image_styles'];
      $image_render_array = array(
        '#theme' => 'image_srcset',
        '#sizes' => $info['sizes'],
      );

      // Map image attributes.
      $image_render_array['#uri'] = _picture_image_style_url($picture_mappings[$mapping_id]['fallback'], $uri);

      // Set up image source options.
      $image_render_array['#srcset'] = array();
      foreach ($image_styles as $image_style) {
        $image_style_uri = _picture_image_style_url($image_style, $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);
        $image_render_array['#srcset'][] = array(
          'uri' => $image_style_uri,
          'width' => $dimensions['width'] . 'w',
        );
      }
      $image_render_array['#attributes'] = array(
        'data-picture-mapping' => $mapping_id,
      ) + $attributes;
      return $image_render_array;
    }
  }
  $image_render_array = array(
    '#theme' => 'picture',
    '#style_name' => $picture_mappings[$mapping_id]['fallback'],
    '#uri' => $uri,
    '#width' => $image_info['width'],
    '#height' => $image_info['height'],
    '#alt' => $alt,
    '#title' => $title,
    '#attributes' => array(
      'data-picture-mapping' => $mapping_id,
    ) + $attributes,
    '#breakpoints' => $breakpoint_styles,
    '#lazyload' => isset($picture_mappings[$mapping_id]['lazyload']) ? $picture_mappings[$mapping_id]['lazyload'] : FALSE,
    '#lazyload_aspect_ratio' => isset($picture_mappings[$mapping_id]['lazyload_aspect_ratio']) ? $picture_mappings[$mapping_id]['lazyload_aspect_ratio'] : FALSE,
  );
  return $image_render_array;
}