You are here

function _resp_img_replace_picture in Responsive images and styles 7.2

1 call to _resp_img_replace_picture()
resp_img_post_render in ./resp_img.module
#post_render callback.

File

./resp_img.module, line 667

Code

function _resp_img_replace_picture($content) {
  $result = array();
  preg_match_all('/<img[^>]*>/i', $content, $result);
  $orig_imgs = $imgs = $result[0];
  $vars['image_style'] = 'image_style';
  $vars['#formatter'] = 'picture';
  $vars['#theme'] = 'picture_formatter';
  foreach ($imgs as &$img) {
    if (strpos($img, RESP_IMG_STYLE_PREFIX) !== FALSE) {
      $xml = simplexml_load_string('<image>' . html_entity_decode($img, 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;
        }
      }
      if (isset($attributes['src']) && !empty($attributes['src'])) {
        $group_name = drupal_substr($attributes['src'], strpos($attributes['src'], RESP_IMG_STYLE_PREFIX));
        $group_name = str_replace(RESP_IMG_STYLE_PREFIX, '', drupal_substr($group_name, 0, strpos($group_name, '/')));
        $breakpoint_styles = _resp_img_get_breakpoint_styles($group_name);

        // @todo: Ugly! Find a better way?
        $src = drupal_substr($attributes['src'], strpos($attributes['src'], RESP_IMG_STYLE_PREFIX . $group_name) + drupal_strlen(RESP_IMG_STYLE_PREFIX . $group_name) + 1);
        $src = preg_replace('/\\//', '://', $src, 1);
        unset($attributes['src']);
        $variables = array(
          'path' => $src,
          'breakpoints' => $breakpoint_styles,
        );
        if (isset($attributes['width'])) {
          $variables['width'] = $attributes['width'];
          unset($attributes['width']);
        }
        if (isset($attributes['height'])) {
          $variables['height'] = $attributes['height'];
          unset($attributes['height']);
        }
        $variables['attributes'] = $attributes;
        if (isset($variables['attributes']['class'])) {
          $variables['attributes']['class'] = explode(' ', $variables['attributes']['class']);
        }
        $variables['style_name'] = RESP_IMG_STYLE_PREFIX . $group_name;
        $img = theme('picture', $variables);
      }
    }
  }
  $content = str_replace($orig_imgs, $imgs, $content);
  return $content;
}