Hi,

this module works great, but just for one link!

Is it possible to get the Previews for all links in an article?!

Regards,

Tobi

CommentFileSizeAuthor
#4 extlink_preview.zip3.22 KBMauMau

Comments

DerTobi75 created an issue. See original summary.

sanket1007’s picture

Yes, it can be done.

I have decided to do this in the next version which will be released very soon. Thanks for the idea though.

sanket1007’s picture

Status: Active » Closed (fixed)
MauMau’s picture

StatusFileSize
new3.22 KB

I like External Link Preview much better than Opengraph Filter.

External Link Preview
* renders better out of the box
* is lightweight
* doesn't require a special text format or much configuring

Multiple links is a good idea though. So I took the liberty to change a little bit in a couple of functions. The code below seems to work well in my use case - but I may well have overlooked something.

Anyway, to anyone interested in multiple links just switch to these altered functions inside extlink_preview.module:

/**
 * Implements hook_field_formatter_view().
 */
function extlink_preview_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  drupal_add_css(drupal_get_path('module', 'extlink_preview') . '/css/extlink_preview.css');
  switch ($display['type']) {
    case 'extlink_preview_formatter':
      foreach ($items as $delta => $item) {
        $output = extlink_preview_get_preview($item['value'], $settings);
        $element[$delta] = array('#markup' => $item['value'] . $output);
      }
      break;
  }
  return $element;
}

and

/**
 * Extracting the metatags of the url.
 */
function extlink_preview_get_preview($preview_item, $settings) {
  if (isset($preview_item)) {
   
   /* if (preg_match('/www./', $preview_item)) {
      $newstr = explode(' ', strstr($preview_item, 'www.'))[0];
      $preview_item = substr_replace($newstr, 'http://', 0, 0);
    }
    elseif (!(preg_match('/http:/', $preview_item)) && preg_match('/bit.ly/', $preview_item)) {
      $newstr = explode(' ', strstr($preview_item, 'bit.ly'))[0];
      $preview_item = substr_replace($newstr, 'http://', 0, 0);
    }
    
    if (preg_match("/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/", $preview_item, $match)) {
      $link = $match[0];
    } */
   
    $pattern = '/(href)=("[^"]*")/'; 
    if (preg_match_all($pattern, $preview_item, $matches)) {
      $links = $matches[2];
    }
    if(!is_array($links)) {
      // no links, just return nothing
      return '';
    }
    $all_output = '';
    foreach ($links as $link) {
      $link = str_replace('"', '', $link);
      $html = extlink_preview_get_contents_curl($link);
      if (isset($html) && !empty($html)) {
        $result = extlink_preview_get_all_metatags($html);
        $data = extlink_preview_get_og_metatags($html, $link);
      }
      if ($settings['show_title'] == 'Y') {
        if (!empty($data['og:title'])) {
          $output['title'] = $data['og:title'];
          $output['url'] = $link;
        }
        elseif (isset($result['title'])) {
          $output['title'] = $result['title'];
          $output['url'] = $link;
        }
      }
      if ($settings['trim_link_description'] != '0') {
        $description_length = $settings['trim_link_description'];
        if (!empty($data['og:description'])) {
          $description = strip_tags($data['og:description']);
          if (strlen($description) > $description_length) {
            $stringcut = substr($description, 0, $description_length);
            $description = substr($stringcut, 0, strrpos($stringcut, ' ')) . '...';
          }
          $output['description'] = $description;
        }
        else {
          if (isset($result['metaTags']['description']['value'])) {
            $description = strip_tags($result['metaTags']['description']['value']);
            if (strlen($description) > $description_length) {
              $stringcut = substr($description, 0, $description_length);
              $description = substr($stringcut, 0, strrpos($stringcut, ' ')) . '...';
            }
            $output['description'] = $description;
          }
        }
      }
      if ($settings['show_image'] == 'Y') {
        if (!empty($data['og:image'])) {
          if (preg_match('/www./', $data['og:image'])) {
            $newstr = explode(' ', strstr($data['og:image'], 'www.'))[0];
            $img_src = substr_replace($newstr, 'http://', 0, 0);
          }
          else {
            $img_src = $data['og:image'];
          }
          $raw = extlink_preview_ranger($img_src);
          if (preg_match("/.png/i", $img_src)) {
            $im = @imagecreatefrompng($img_src);
          }
          else {
            if (!empty($raw)) {
              $im = @imagecreatefromstring($raw);
            }
          }
          if (!empty($im)) {
            $width = imagesx($im);
            $height = imagesy($im);
          }
          else {
            if (!empty($img_src)) {
              $image_size = getimagesize($img_src);
              if (!empty($image_size)) {
                $width = $image_size['0'];
                $height = $image_size['1'];
              }
            }
          }
          if (isset($width) && isset($height)) {
            $image_aspect_ratio = $width / $height;
            if (isset($image_aspect_ratio)) {
              if ($width >= 380  && $image_aspect_ratio >= 1.2) {
                $img_class = "og-long-image";
              }
              elseif ($width > 140) {
                $img_class = "og-short-image";
              }
            }
          }
        }
        elseif (isset($result['metaTags']['twitter:image']['value'])) {
          $img_src = $result['metaTags']['twitter:image']['value'];
          if (preg_match('/www./', $result['metaTags']['twitter:image']['value'])) {
            $newstr = explode(' ', strstr($result['metaTags']['twitter:image']['value'], 'www.'))[0];
            $img_src = substr_replace($newstr, 'http://', 0, 0);
          }
          else {
            $img_src = $result['metaTags']['twitter:image']['value'];
          }
          $raw = extlink_preview_ranger($img_src);
          if (preg_match("/.png/i", $img_src)) {
            $im = @imagecreatefrompng($img_src);
          }
          else {
            if (!empty($raw)) {
              $im = @imagecreatefromstring($raw);
            }
          }
          if (!empty($im)) {
            $width = imagesx($im);
            $height = imagesy($im);
          }
          else {
            if (!empty($img_src)) {
              $image_size = getimagesize($img_src);
              if (!empty($image_size)) {
                $width = $image_size['0'];
                $height = $image_size['1'];
              }
            }
          }
          if (isset($width) && isset($height)) {
            $image_aspect_ratio = $width / $height;
            if (isset($image_aspect_ratio)) {
              if ($width >= 380  && $image_aspect_ratio >= 1.2) {
                $img_class = "og-long-image";
              }
              elseif ($width > 140) {
                $img_class = "og-short-image";
              }
            }
          }
        }
        else {
          $images = extlink_preview_get_images($html);
          if (isset($images)) {
            $images_data = array();
            foreach ($images as $key => $value) {
              if ((substr($images[$key], 0, 7) == 'http://') || (substr($images[$key], 0, 8) == 'https://')) {
                $raw = extlink_preview_ranger($images[$key]);
                if (!empty($raw)) {
                  $im = @imagecreatefromstring($raw);
                  if (!empty($im)) {
                    $width = imagesx($im);
                    $height = imagesy($im);
                    if (isset($width) && isset($height)) {
                      $images_data[$key]['width'] = $width;
                      $images_data[$key]['height'] = $height;
                      $images_data[$key]['url'] = $images[$key];
                    }
                  }
                }
              }
            }
            if (!empty($images_data)) {
              foreach ($images_data as $key => $value) {
                $twidth[$key] = $images_data[$key]['width'];
              }
              $max_width = max($twidth);
              foreach ($images_data as $key => $value) {
                if ($images_data[$key]['width'] == $max_width && $images_data[$key]['height'] >= 140) {
                  $image_width = $max_width;
                  $image_height = $images_data[$key]['height'];
                  if (isset($image_width) && isset($image_height)) {
                    $image_aspect_ratio = $image_width / $image_height;
                  }
                  $img_src = $images_data[$key]['url'];;
                  break;
                }
              }
              if (isset($img_src) && isset($image_width)) {
                if ($max_width >= 380 && $image_aspect_ratio >= 1.2) {
                  $img_class = "og-long-image";
                }
                else {
                  $img_class = "og-short-image";
                }
              }
            }
          }
        }
        if (isset($img_class) && isset($img_src)) {
          $image = theme('image', array('path' => $img_src, 'attributes' => array('class' => $img_class)));
          $output['image'] = $image;
        }
      }
      if(is_array($output)) {
        $all_output .= theme('extlink_preview', $output);
      }
      }
      return $all_output;
    }
}

or just use the attached file (unzip it and overwrite the old extlink_preview.module).

Great work sanket1007!