using a special syntax', array('%explanation-url' => url('filter/tips', NULL, 'filter-image_filter-0'))); } function image_filter_long_tip_translated() { return t( '

You may quickly link to image nodes using a special syntax. Each image code will be replaced by thumbnail linked to full size image node. Syntax:

[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]

Every parameter except node_id is optional.

Typically, you will specify one of size, width, or height, or none of them. If you use size=label, where label 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 width=n or height=n, 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.

If you specify nolink=1, no link will be created to the image node. The default is to create a link to the image.

Alternatively, if you specify node=id, a link will be created to the node with the given id.

The align, hspace, vspace, border, class, and style parameters set the corresponding attributes in the generated img tag.

'); } function image_filter_help($section = 'admin/help#image_filter') { if (module_exists('image_filter') && !module_exists('image')) { drupal_set_message(t('The image_filter module relies on image.module. Please go to the modules section of administration to turn on image.module.', array('%modules' => url('admin/modules')))); } $output = ''; switch ($section) { case 'admin/help#image_filter': $output = t('

Sometimes, you want to add an image to another node like a blog entry or a story. You may do this by creating an image node in Drupal for the target image, and then referencing that image in your story, blog, etc. To enable this feature and learn the proper syntax, visit the filters configuration screen.

', array('%filters' => url('admin/filters'))); break; case 'admin/modules#description': $output = t("Allow users to reference images from other nodes."); break; case 'filter#short-tip': return image_filter_short_tip_translated(); case 'filter#long-tip': return image_filter_long_tip_translated(); } return $output; } /** * Hook which handles filtering. */ function image_filter_filter($op, $delta = 0, $format = -1, $text = '') { switch ($op) { case 'list': return array(0 => t('Image filter')); case 'description': return t('Allow users to reference images from other nodes.'); case 'process': return image_filter_process($text); default: return $text; } } /** * */ function image_filter_filter_tips($delta = 0, $format = -1, $long = false) { if ($long) { return image_filter_long_tip_translated(); } else { return image_filter_short_tip_translated(); } } define("IMAGE_FILTER_WORD", 1); define("IMAGE_FILTER_INTEGER", 2); define("IMAGE_FILTER_STRING", 3); function image_filter_attr_value($text, $value_type = IMAGE_FILTER_WORD) { // Strip off initial and final quotes. $first = substr($text, 0, 1); if ($first == "\"" || $first == "\'") { if (substr($text, -1, 1) == $first) { $text = substr($text, 1, -1); } } switch ($value_type) { case IMAGE_FILTER_WORD: return preg_replace("/\W/", '', $text); case IMAGE_FILTER_INTEGER: return preg_replace("/\D/", '', $text); default: return check_plain($text); } } /** * Actually execute filter on given text. */ function image_filter_process($text) { // 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(); if (strlen($match[2]) > 0) { // Original syntax: [image:NID,align,hspace,vspace,border] $a = preg_replace('/\W/', '', preg_split("/\s*,\s*/", $match[3])); $args['align'] = image_filter_attr_value($a[0], IMAGE_FILTER_WORD); $args['hspace'] = image_filter_attr_value($a[1], IMAGE_FILTER_INTEGER); $args['vspace'] = image_filter_attr_value($a[2], IMAGE_FILTER_INTEGER); $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", "align=\"$1\"", $match[3]); preg_match_all("/(\w+)\=(\"[^\"]*\"|\S*)/", $match[3], $a, PREG_SET_ORDER); foreach ($a as $arg) { $args[strtolower($arg[1])] = $arg[2]; } } $width = image_filter_attr_value($args['width'], IMAGE_FILTER_INTEGER); $height = image_filter_attr_value($args['height'], IMAGE_FILTER_INTEGER); $size = image_filter_attr_value($args['size'], IMAGE_FILTER_WORD); 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 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; } $img->align = image_filter_attr_value($args['align'], IMAGE_FILTER_WORD); $img->hspace = image_filter_attr_value($args['hspace'], IMAGE_FILTER_INTEGER); $img->vspace = image_filter_attr_value($args['vspace'], IMAGE_FILTER_INTEGER); $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); $linkid = image_filter_attr_value($args['node'], IMAGE_FILTER_INTEGER); $img->link = $args['nolink'] ? NULL : $linkid ? "node/$linkid" : "node/$img->nid"; $img_tag = theme("image_inline_img", $img); $text = str_replace($match[0], $img_tag, $text); } } return $text; } /** * Theme function to render image in a node. * Displays a thumbnail with a link to the image. */ function theme_image_inline_img($img) { $imgtag = "fileurl\"" . ($img->width ? " width=\"$img->width\"" : '') . ($img->height ? " height=\"$img->height\"" : '') . ($img->align ? " align=\"$img->align\"" : '') . ($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: // mode: php // indent-tabs-mode: nil // tab-width: 2 // End: ?>