--- teaserthumbnail/teaserthumbnail.module	2009-02-13 22:50:45.000000000 -0600
+++ teaserthumbnail.module.new	2009-11-19 11:21:49.000000000 -0600
@@ -403,12 +403,85 @@ function _teaserthumbnail_save_thumb($no
  */
 function _teaserthumbnail_strip_media($string) {
   // We remove all the pictures from the text
-  $img_pattern  = "/<img[^>]+src=\"[^\"]+\"[^>]*>/i";
-  $string = preg_replace($img_pattern, '', $string);
+  $img_pattern  = "/<img[^>]+src=\"([^\"]+)\"[^>]*>/i";
+  $string = preg_replace_callback($img_pattern, "_teaserthumbnail_strip_images_callback", $string);
   
   // We now remove the object blocks (video, flash)
   $object_pattern = "/<object[0-9 a-z_?*=\":\-\/\.#\,<>\\n\\r\\t]+<\/object>/smi";
   $text = preg_replace($object_pattern, '', $string);
   
   return $string;
+}
+
+/**
+ * Replace callback that differentiates between relative and absolute images.  Useful for displaying images from posts.
+ */
+function _teaserthumbnail_strip_images_callback($image) {
+  return strstr($image[1], "://")?_teaserthumbnail_replace_remote_callback($image):"";
+}
+
+/**
+ * Replace callback to fix the sizes of remote images.
+ */
+function _teaserthumbnail_replace_remote_callback($image) {
+  // We retrieve the mininum size for a picture to be considered suitable
+  $min_height = variable_get('teaserthumbnail_min_height', '150');
+  $min_width = variable_get('teaserthumbnail_min_width', '150');
+  
+  $image_tag = $image[0];
+
+  if (strstr($image_tag,"style=")!==false) {//find it in the style
+    //find the existing size
+//    drupal_set_message($image_tag);
+    if (preg_match("/width: ?(?<width>\d+)px/i",$image_tag,$match_width)) {
+      $img_width = $match_width['width'];
+      $image_tag = preg_replace("/width=\"[^\"]+\"/i", "", $image_tag);
+    }
+    if (preg_match("/height: ?(?<height>\d+)px/i",$image_tag,$match_height)) {
+      $img_height = $match_height['height'];
+      $image_tag = preg_replace("/height=\"[^\"]+\"/i", "", $image_tag);
+    }
+    //replace both height and width or only one
+    if ($img_width && $img_height && ($img_width>$min_width || $img_height>$min_height)) {
+      if($img_width>$min_width) {
+        $scale_ratio = $min_width/$img_width;
+        $image_tag = preg_replace("/width: ?\d+px/i","width:".$min_width."px", $image_tag);
+        $image_tag = preg_replace("/height: ?\d+px/i","height:".round($img_height*$scale_ratio, 0)."px", $image_tag);
+      } else {
+        $scale_ratio = $min_height/$img_height;
+        $image_tag = preg_replace("/height: ?\d+px/i","height:".$min_height."px", $image_tag);
+        $image_tag = preg_replace("/width: ?\d+px/i","width:".round($img_width*$scale_ratio, 0)."px", $image_tag);
+      }
+    } elseif ($img_width && $img_width>$min_width) {
+        $image_tag = preg_replace("/width: ?\d+px/i","width:".$min_width."px", $image_tag);
+    } elseif ($img_height && $img_height>$min_height) {
+        $image_tag = preg_replace("/height: ?\d+px/i","height:".$min_height."px", $image_tag);
+    }
+  } else if (strstr($image_tag, "width=") || strstr($image_tag, "height=")) {
+    //find the existing size
+    if (preg_match("/width=\"(?<width>\d+)/i",$image_tag,$match_width)) {
+      $img_width = $match_width['width'];
+    }
+    if (preg_match("/height=\"(?<height>\d+)/i",$image_tag,$match_height)) {
+      $img_height = $match_height['height'];
+    }
+//    drupal_set_message("$img_width X $image_height");
+    //replace both height and width or only one
+    if ($img_width && $img_height && ($img_width>$min_width || $img_height>$min_height)) {
+      if($img_width>$min_width) {
+        $scale_ratio = $min_width/$img_width;
+        $image_tag = preg_replace("/width=\"\d+/i","width=\"".$min_width, $image_tag);
+        $image_tag = preg_replace("/height=\"\d+/i","height=\"".round($img_height*$scale_ratio, 0), $image_tag);
+      } else {
+        $scale_ratio = $min_height/$img_height;
+        $image_tag = preg_replace("/height=\"\d+/i","height=\"".$min_height, $image_tag);
+        $image_tag = preg_replace("/width=\"\d+/i","width=\"".round($img_width*$scale_ratio, 0), $image_tag);
+      }
+    } elseif ($img_width && $img_width>$min_width) {
+        $image_tag = preg_replace("/width=\"\d+/i","width=\"".$min_width, $image_tag);
+    } elseif ($img_height && $img_height>$min_height) {
+        $image_tag = preg_replace("/height=\"\d+/i","height=\"".$min_height, $image_tag);
+    }
+  }
+  return $image_tag;
 }
\ No newline at end of file
