*** image_filter.module.orig    2007-10-23 15:34:47.000000000 +1000
--- image_filter.module 2007-10-24 11:11:30.000000000 +1000
***************
*** 1,8 ****
  <?php
  // $Id: image_filter.module,v 1.23 2007/02/06 05:52:48 davidhull Exp $

  function image_filter_short_tip_translated() {
!   return t('You may link to images on this site using a special syntax, like <code>[image:node_id align=alignment hspace=n vspace=n border=n size=label width=n height=n nolink=(0|1) class=name style=style-data node=id]</code>');
  }

  function image_filter_long_tip_translated() {
--- 1,9 ----
  <?php
  // $Id: image_filter.module,v 1.23 2007/02/06 05:52:48 davidhull Exp $
+ // vim:set ft=php:

  function image_filter_short_tip_translated() {
!   return t('You may link to images on this site using a special syntax, like <code>[image:node_id align=alignment hspace=n vspace=n border=n size=label width=n height=n nolink=(0|1) caption=(teaser|body|"My Caption") class=name style=style-data node=id]</code>');
  }

  function image_filter_long_tip_translated() {
***************
*** 13,19 ****
  <p>Typically, you will specify one of <code>size</code>, <code>width</code>, or <code>height</code>, or none of them. If you use <code>size=<em>label</em></code>, where <em>label</em> is one of the image size labels specified on the image settings page, the size associated with that label will be used. The sizes "thumbnail", "preview", and "original" are always available. If you use <code>width=<em>n</em></code> or <code>height=<em>n</em></code>, the image will be scaled to fit the specified width or height. If you use none of them, the thumbnail image size will be used.</p>
  <p>If you specify <code>nolink=1</code>, no link will be created to the image node. The default is to create a link to the image.</p>
  <p>Alternatively, if you specify <code>node=id</code>, a link will be created to the node with the given id.</p>
! <p>The <code>align</code>, <code>hspace</code>, <code>vspace</code>, <code>border</code>, <code>class</code>, and <code>style</code> parameters set the corresponding attributes in the generated <code>img</code> tag.</p>');
  }

  function image_filter_help($section = 'admin/help#image_filter') {
--- 14,21 ----
  <p>Typically, you will specify one of <code>size</code>, <code>width</code>, or <code>height</code>, or none of them. If you use <code>size=<em>label</em></code>, where <em>label</em> is one of the image size labels specified on the image settings page, the size associated with that label will be used. The sizes "thumbnail", "preview", and "original" are always available. If you use <code>width=<em>n</em></code> or <code>height=<em>n</em></code>, the image will be scaled to fit the specified width or height. If you use none of them, the thumbnail image size will be used.</p>
  <p>If you specify <code>nolink=1</code>, no link will be created to the image node. The default is to create a link to the image.</p>
  <p>Alternatively, if you specify <code>node=id</code>, a link will be created to the node with the given id.</p>
! <p>The <code>align</code>, <code>hspace</code>, <code>vspace</code>, <code>border</code>, <code>class</code>, and <code>style</code> parameters set the corresponding attributes in the generated <code>img</code> tag.</p>
! <p>If you specify the <code>caption</code> option <code>style</code> and <code>class</code> arguments will be placed in a separate <code>&lt;div&gt;</code> containing the image (without these tags) and a caption. The caption is either the teaser or body or the text you specify.</p>');
  }

  function image_filter_help($section = 'admin/help#image_filter') {
***************
*** 92,97 ****
--- 94,100 ----

    // Find all the image codes and loop over them, replacing each with an img tag.
    preg_match_all("/\[image:(\d+)(\s*,)?\s*(.*?)\]/i", $text, $matches, PREG_SET_ORDER);
+
    foreach ($matches as $match) {

      $args = array();
***************
*** 104,110 ****
        $args['border'] = image_filter_attr_value($a[3], IMAGE_FILTER_INTEGER);
      }
      else {
!       // New syntax: [image:node_id (left|right|top|middle|bottom|absmiddle|texttop|baseline|align=alignment) hspace=n vspace=n border=n size=label width=n height=n nolink=(0|1) class=name style=style-data node=id]

        // Convert bare alignment 'X' to 'align="X"'.
        $match[3] = preg_replace("/^(left|right|top|middle|bottom|absmiddle|texttop|baseline)\b/i",
--- 107,113 ----
        $args['border'] = image_filter_attr_value($a[3], IMAGE_FILTER_INTEGER);
      }
      else {
!       // New syntax: [image:node_id (left|right|top|middle|bottom|absmiddle|texttop|baseline|align=alignment) hspace=n vspace=n border=n size=label width=n height=n nolink=(0|1) caption=(teaser|body|"My Caption") class=name style=style-data node=id]

        // Convert bare alignment 'X' to 'align="X"'.
        $match[3] = preg_replace("/^(left|right|top|middle|bottom|absmiddle|texttop|baseline)\b/i",
***************
*** 122,172 ****
      if ($size == 'original') { $size = '_original'; }
      if (!$width && !$height && strlen($size) == 0) { $size = 'thumbnail'; }

!     $img = NULL;
!
!     $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, f.filename, f.filepath FROM {files} f, {node} n WHERE n.status = 1 AND f.filemime LIKE 'image/%%' AND f.nid = n.nid AND n.nid = %d" . (strlen($size) != 0 ? " AND f.filename IN ('" . db_escape_string($size) . "','_original')" : '')), $match[1]);

!     // Loop over the files found for this image and select the best match.
!     while ($i = db_fetch_object($result)) {
!       $i->size = getimagesize(file_create_path($i->filepath));
!       if (! $img) {
!         $img = $i;
!       }
!       // If we find a match for the desired image size label, take it.
!       if ((strlen($size) != 0) && ($i->filename == $size)) {
!         $img = $i;
!         break;
!       }
!       // If we find a better match for the desired image width or height, tentatively take it.
!       if ($width && $i->size[0] >= $width && $i->size[1] >= $height) {
!         if ($i->size[0] < $img->size[0] || $img->size[0] < $width) {
!           $img = $i;
!         }
!       }
!       if ($height && $i->size[1] >= $height && $i->size[0] >= $width) {
!         if ($i->size[1] < $img->size[1] || $img->size[1] < $height) {
!           $img = $i;
          }
        }
-     }

-     // If we found a matching image, replace the image code with an <img> tag.
-     if ($img) {
        $img->fileurl = file_create_url($img->filepath);

!       if (! $width && ! $height) {
!         $img->width = $img->size[0];
!         $img->height = $img->size[1];
!       }
!       else if ($width && ! $height) {
          $img->width = $width;
-         $img->height = round($img->size[1] * $width / $img->size[0]);
        }
!       else if ($height && ! $width) {
          $img->height = $height;
-         $img->width = round($img->size[0] * $height / $img->size[1]);
        }
!       else {
          $img->width = $width;
          $img->height = $height;
        }
--- 125,177 ----
      if ($size == 'original') { $size = '_original'; }
      if (!$width && !$height && strlen($size) == 0) { $size = 'thumbnail'; }

!     $img = node_load($match[1]);
!     if ($img && is_array($img->images)) {
!     // If we found a matching image, replace the image code with an <img> tag.

!       if ($size && array_key_exists($size, $img->images)) {
!         // Use the file for this exact given size
!         $img->filepath = $img->images[$size];
!         $info = image_get_info(file_create_path($img->filepath));
!         $img->width = $info['width'];
!         $img->height = $info['height'];
!       } else {
!         // Find the closest match to the given size
!         foreach($img->images as $label => $filepath) {
!           $better = FALSE;
!           $info = image_get_info(file_create_path($filepath));
!           if (!$img->width) {
!             $better = TRUE;
!           } else if ($width && $info['width'] >= $width && $info['height'] >= $height) {
!             if ($info['width'] < $img->width || $img->width < $width) {
!               $better = TRUE;
!             }
!           } else if ($height && $info['height'] >= $height && $info['width'] >= $width) {
!             if ($info['height'] < $img->height || $img->height < $height) {
!               $better = TRUE;
!             }
!           }
!
!           if ($better) {
!             $img->filepath = $filepath;
!             $img->size = $label;
!             $img->width = $info['width'];
!             $img->height = $info['height'];
!           }
          }
        }

        $img->fileurl = file_create_url($img->filepath);

!       if ($width && !$height) {
!         $img->height = round($img->height * $width / $img->width);
          $img->width = $width;
        }
!       else if ($height && !$width) {
!         $img->width = round($img->width * $height / $img->height);
          $img->height = $height;
        }
!       else if ($width && $height) {
          $img->width = $width;
          $img->height = $height;
        }
***************
*** 177,182 ****
--- 182,190 ----
        $img->border = image_filter_attr_value($args['border'], IMAGE_FILTER_INTEGER);
        $img->class = image_filter_attr_value($args['class'], IMAGE_FILTER_WORD);
        $img->style = image_filter_attr_value($args['style'], IMAGE_FILTER_STRING);
+       $img->caption = image_filter_attr_value($args['caption'], IMAGE_FILTER_STRING);
+       // Add default class if caption and none given
+       if ($img->caption && !$img->class) { $img->class = "image_caption"; }
        $linkid = image_filter_attr_value($args['node'], IMAGE_FILTER_INTEGER);
        $img->link = $args['nolink']
          ? NULL
***************
*** 195,200 ****
--- 203,211 ----
   * Displays a thumbnail with a link to the image.
   */
  function theme_image_inline_img($img) {
+   $styles = ($img->class ? " class=\"$img->class\"" : '') .
+             ($img->style ? " style=\"$img->style\"" : '');
+
    $imgtag = "<img src=\"$img->fileurl\"" .
      ($img->width ? " width=\"$img->width\"" : '') .
      ($img->height ? " height=\"$img->height\"" : '') .
***************
*** 202,213 ****
      ($img->border ? " border=\"$img->border\"" : '') .
      ($img->hspace ? " hspace=\"$img->hspace\"" : '') .
      ($img->vspace ? " vspace=\"$img->vspace\"" : '') .
!     " alt=\"$img->title\"" .
!     ($img->class ? " class=\"$img->class\"" : '') .
!     ($img->style ? " style=\"$img->style\"" : '') . " />";
!   return ($img->link
!           ? l($imgtag, $img->link, array(), NULL, NULL, TRUE, TRUE)
!           : $imgtag);
  }

  // Local Variables:
--- 213,234 ----
      ($img->border ? " border=\"$img->border\"" : '') .
      ($img->hspace ? " hspace=\"$img->hspace\"" : '') .
      ($img->vspace ? " vspace=\"$img->vspace\"" : '') .
!     ($img->caption ? '' : $styles) .
!     " alt=\"$img->title\"" . " />";
!   $out .= ($img->link
!            ? l($imgtag, $img->link, array(), NULL, NULL, TRUE, TRUE)
!            : $imgtag);
!   if ($img->caption) {
!     $out = "<div $styles>" .
!       $out .
!       "<div class=\"caption\">" .
!       ($img->caption == "body" ? $img->body :
!         ($img->caption == "teaser" ? $img->teaser :
!           $img->caption)) .
!       "</div></div>";
!   }
!
!   return $out;
  }

  // Local Variables:
