You are here

taxonomy_image.module in Taxonomy Image 5

Same filename and directory in other branches
  1. 8 taxonomy_image.module
  2. 6 taxonomy_image.module
  3. 7 taxonomy_image.module

taxonomy_image.module Simple module for providing an association between taxonomy terms and images. Written by Jeremy Andrews <jeremy@kerneltrap.org>, May 2004.

File

taxonomy_image.module
View source
<?php

/**
 * @file
 *  taxonomy_image.module
 *  Simple module for providing an association between taxonomy terms and images.
 *  Written by Jeremy Andrews <jeremy@kerneltrap.org>, May 2004.
 */

/**
 * Function to display the image associated with the given term id.
 * An html <img> tag will be returned if an image is found.  The link
 * format can be modified with the tags parameter.
 * @see taxonomy_image_get_object()
 * @see taxonomy_image_get_url()
 *
 * @param
 *   $tid - the term id.
 * @param
 *   $tags - optional HTML attributes to include in the link.
 * 
 * @return
 *   An html img link.
 */
function taxonomy_image_display($tid, $tags = NULL, $profile = NULL, $overrides = array()) {
  global $user;
  if (user_access('access taxonomy images') && empty($user->taxonomy_image_disable_images)) {

    // Get our controlling variables.
    $wrapper = variable_get('taxonomy_image_wrapper', FALSE);
    $imagecache_preset = variable_get('taxonomy_image_imagecache_preset', 'ORIGINAL');
    $recursive = variable_get('taxonomy_image_recursive', 0);
    $resize = variable_get('taxonomy_image_resize', 0);
    $width = variable_get('taxonomy_image_width', '');
    $height = variable_get('taxonomy_image_height', '');

    // Any overrides specified?
    if ($overrides) {
      extract($overrides, EXTR_IF_EXISTS);
    }

    // do lookup, return full display path
    $image = taxonomy_image_get_object($tid, $recursive);

    // Do we have an image?
    if (isset($image->path)) {
      if (module_exists('imagecache') && $image->type_extension != 'swf') {
        $preset = $profile ? $profile : $imagecache_preset;
      }
      else {
        $preset = 'ORIGINAL';
      }

      // Handle our own image resizing?
      if ($preset == 'ORIGINAL') {
        taxonomy_image_resize($image, $resize, $width, $height);
      }
      $current = $image;

      // Build the link title based on admin choice.
      if ($current->description && !variable_get('taxonomy_image_link_title', 0)) {
        $current->title = strip_tags($current->description);
      }
      else {
        $current->title = strip_tags($current->name);
      }

      // Have to dump double quotes for attribute.
      $current->title = htmlspecialchars(strip_tags($current->title), ENT_COMPAT);
      $my_attrs = array(
        'width' => $current->width,
        'height' => $current->height,
      );

      // $tag was originally an HTML attribute string. It should now be a standard attributes array.
      // If the caller provided the same key, this will force me to use those.
      if (is_array($tags)) {
        $attributes = array_merge($my_attrs, $tags);
      }
      else {

        // Handle the old string format.
        $attributes = array_merge($my_attrs, taxonomy_image_parse_tags($tags));
      }

      // Handle special image types.
      switch ($current->type_extension) {
        case 'swf':
          $attributes['type'] = $current->mime;
          $attributes['data'] = $current->url;
          $attributes['title'] = $current->title;
          $return_url = '<object ' . drupal_attributes($attributes) . '>Your browser does not support Flash objects.</object>';
          break;
        default:
          if ($preset == 'ORIGINAL') {
            $return_url = theme('image', $current->url, $current->name, $current->title, $attributes, FALSE);
          }
          else {

            // Make sure the attributes don't try to override the preset.
            unset($attributes['width'], $attributes['height']);
            $return_url = theme('imagecache', $preset, $current->path, $current->name, $current->title, $attributes);
          }
      }
      if ($wrapper) {
        $return_url = '<div class="taxonomy_image_wrapper">' . $return_url . '</div>';
      }
      return $return_url;
    }
  }
  return '';
}

/**
 * Function to resize the image.
 */
function taxonomy_image_resize($image, $resize, $width = NULL, $height = NULL) {
  switch ($resize) {
    case 3:

      // Exact.
      if ($width) {
        $image->width = $width;
      }
      if ($height) {
        $image->height = $height;
      }
      break;
    case 2:

      // Not less than.
      if ($width && $width > $image->width) {
        $width_scale = $image->width / $width;
      }
      if ($height && $height > $image->height) {
        $height_scale = $image->height / $height;
      }
      if ($height_scale || $width_scale) {
        if ($width_scale && $height_scale) {
          $scale = min($width_scale, $height_scale);
        }
        else {
          $scale = $width_scale ? $width_scale : $height_scale;
        }
        $image->height = round($image->height / $scale);
        $image->width = round($image->width / $scale);
      }
      break;
    case 1:

      // Not greater than.
      if ($width && $width < $image->width) {
        $width_scale = $image->width / $width;
      }
      if ($height && $height < $image->height) {
        $height_scale = $image->height / $height;
      }
      if ($height_scale || $width_scale) {
        $scale = max($width_scale, $height_scale);
        $image->height = round($image->height / $scale);
        $image->width = round($image->width / $scale);
      }
      break;
    case 0:

      // Disabled.
      $image->height = '';
      $image->width = '';
      break;
  }
}

/**
 * Function to parse tags (HTML) string.
 */
function taxonomy_image_parse_tags($tags) {
  $attrs = array();
  $arr = explode(' ', $tags);
  foreach ($arr as $tag) {
    if (!empty($tag)) {

      // Ignore extra blanks.
      $key_len = strpos($tag, '=');
      $attrs[drupal_substr($tag, 0, $key_len)] = trim(drupal_substr($tag, $key_len + 1), '\'"');
    }
  }
  return $attrs;
}

/**
 * Function to get a url for an image, for use in css, html or other client-side code.
 * @param
 *   $tid - the term id
 * @return
 *   The url string.
 */
function taxonomy_image_get_url($tid) {
  $image = taxonomy_image_get_object($tid);
  return $image->url;
}

/**
 * Function to get an image object with more useful data for custom formatting.
 * @param
 *   $tid - the term tid.
 * @return
 *   image object.
 */
function taxonomy_image_get_object($tid, $recursive = NULL) {
  global $user;
  static $image = array();
  if (!$recursive) {
    $recursive = variable_get('taxonomy_image_recursive', 0);
  }

  // Themes may call with a string of terms, skip it. Also skip if invalid tid.
  if (!is_numeric($tid) || $tid < 1) {
    return NULL;
  }

  // Should we be here?
  if (!user_access('access taxonomy images') || !empty($user->taxonomy_image_disable_images)) {
    return NULL;
  }

  // Is the data statically cached?
  if (isset($image[$tid])) {
    return $image[$tid];
  }
  else {
    $cache_obj = cache_get("taxonomy_image:{$tid}", 'cache_tax_image');
    if ($cache_obj) {
      $image[$tid] = unserialize($cache_obj->data);
      return $image[$tid];
    }

    // Not cached, so go build it.
    if ($image[$tid] = db_fetch_object(db_query('SELECT i.path, d.name, d.description FROM {term_image} i INNER JOIN {term_data} d USING(tid) WHERE i.tid=%d', $tid))) {
      $image[$tid]->url = file_create_url($image[$tid]->path);
    }
    elseif ($recursive) {

      // Walk up the taxonomy hierarchy and look for an image.
      $orig = $tid;
      while ($parent = db_fetch_object(db_query('SELECT t.tid FROM {term_hierarchy} h, {term_data} t WHERE h.parent=t.tid AND h.tid=%d ORDER BY weight, name', $tid))) {
        return $image[$orig] = taxonomy_image_get_object($parent->tid, $recursive);
      }
    }
  }

  // Get more properties if we had an image.
  $image[$tid]->tid = $tid;

  // If there was no image, but there is a default, fake it.
  if (!isset($image[$tid]->path) && ($default = variable_get('taxonomy_image_default', NULL))) {
    $image[$tid]->path = $default;
    $term = taxonomy_get_term($tid);
    $image[$tid]->name = $term->name;
    $image[$tid]->description = $term->description;
    $image[$tid]->url = file_create_url($image[$tid]->path);
  }
  if (!empty($image[$tid]->path)) {
    $img = getimagesize($image[$tid]->path);

    // Make sure it worked.
    if (!$img) {
      return NULL;
    }
    $exts = array(
      1 => 'gif',
      'jpeg',
      'png',
      'swf',
      'psd',
      'bmp',
      'tiff',
      'tiff',
      'jpc',
      'jp2',
      'jpf',
      'jb2',
      'swc',
      'aiff',
      'wbmp',
      'xbm',
    );
    $image[$tid]->width = $img[0];
    $image[$tid]->height = $img[1];
    $image[$tid]->type = $img[2];
    $image[$tid]->type_extension = $exts[$img[2]];
    $image[$tid]->tags = $img[3];
    $image[$tid]->bits = isset($img['bits']) ? $img['bits'] : NULL;
    $image[$tid]->channels = isset($img['channels']) ? $img['channels'] : NULL;
    $image[$tid]->mime = isset($img['mime']) ? $img['mime'] : NULL;
    $image[$tid]->term = drupal_get_path_alias(taxonomy_term_path(taxonomy_get_term($tid)));
    $image[$tid]->img = '<img src="' . $image[$tid]->url . '" ' . $image[$tid]->tags . 'alt="' . check_plain($image[$tid]->name) . '" ' . 'title="' . check_plain($image[$tid]->description ? $image[$tid]->description : $image[$tid]->name) . '" ' . '>';
  }
  cache_set("taxonomy_image:{$tid}", 'cache_tax_image', serialize($image[$tid]));
  return $image[$tid];
}

// Standard Drupal functions.

/**
 * Implementation of hook_perm.
 */
function taxonomy_image_perm() {
  return array(
    'access taxonomy images',
    'administer taxonomy images',
    'can disable taxonomy images',
  );
}

/**
 * Implementation of hook_help.
 */
function taxonomy_image_help($section) {
  switch ($section) {
    case 'admin/content/taxonomy/taxonomy_image':
      if (user_access('administer taxonomy images')) {
        $settings = '<p>' . l(t('Taxonomy Image settings'), 'admin/settings/taxonomy_image') . '</p>';
      }
      else {
        $settings = NULL;
      }
      return t('The taxonomy_image module allows site administrators to associate images with category (taxonomy) terms.  Once defined, this association allows Drupal themes to display images with site content.  For example, this module might be used to display a penguin with content about Linux, and a cheeseburger with content about junk food. To modify or delete an existing image or to upload a new image, click "edit image" on the overview page or use the term edit page.  To learn more about how to create vocabularies and terms, review the <a href="!taxonomy-help">taxonomy help page</a>.', array(
        '!taxonomy-help' => url('admin/help/taxonomy'),
      )) . $settings;
    case 'admin/help#taxonomy_image':
      $output = t('<h3>Introduction</h3>
      <p>The taxonomy_image module allows site administrators to associate images with category terms.  Once defined, this association allows Drupal sites to display images with site content.  For example, the taxonomy_image module might be used to display a penguin with content about Linux, and a cheeseburger with content about junk food.</p>
      <p>The module allows both a one-to-one term-to-image relationship, and a many-to-one terms-to-image relationship.</p>
      <p>The taxonomy_image module requires that the taxonomy module also be enabled. The Categories module is not currently supported.</p>
      <h3>Configuration</h3>
      <h4>Uploading images</h4>
      <p>With the taxonomy_image module enabled, images can be uploaded and associated with category terms at "Administer >> Content management >> Categories".  On that page you will find links to tables containing all your vocabularies and terms.  Next to each term is a link titled "edit term" which you can click to upload an image for that term.  After clicking that link, you will be you will find the image management section at the bottom of that page. Using the "browse" button you can select your image then click "Save".
      <p>Continue this process to upload appropriate images for your category terms.  Note that by default images will be displayed at the size they were uploaded.  Alternatively, you can go to "Administer >> Site configuration >> Taxonomy_image" to set the display height and/or width of all taxonomy images.</p>
      <h4>Permissions</h4>
      <p>For your users to be able to view the images you have uploaded, you will need to give them the necessary permissions.  Only users with the "access taxonomy images" permission will see images.  If you wish to give your users the ability to disable the images, also give them the "can disable taxonomy images" permission. A third permission, "administer taxonomy images", controls which users are allowed to configure taxonomy images.</p>
      <h4>Recursive image display</h4>
      <p>Taxonomy is a very powerful tool. One of its features is providing the ability to create hierarchical vocabularies, with which you can build a tree of terms.  It is possible that an entire tree of terms, or even just a branch of terms, are all about a similar subject and should all be associated with the same image.  By going to "Administer >> Site configuration >> Taxonomy_image", you can enable "Recursive image display".  With this option enabled, you only need to configure an image for the parent term, and all children will automatically inheret the same image (unless they are manually configured to display an alternate image).</p>
      <h3>Displaying images</h3>
      <p>To actually display images, you will have to make a call to taxonomy_image_display(). [There are some included add-on modules that will do this for you.]  When calling this function, you will need to pass in the taxonomy term for which an image should be displayed.  For example, from your theme\'s "_node" function you might do the following:
<pre>
  foreach (taxonomy_node_get_terms($node->nid) as $term) {
    if ($image = taxonomy_image_display($term->tid)) {
      $output .= \\"$image\\";
    }
</pre>
    <p>Taxonomy_image_display uses "theme(\'image\',..." so you may override the display in your theme layer.</p>
    ');
      if (module_exists('views')) {
        $output .= t('<h3>Views</h3><p>Views support has been added, so you can use the field "Taxonomy Image" in your view definition.</p>');
      }
      if (module_exists('imagecache')) {
        $output .= t('<h3>Imagecache</h3>
          <p>With imagecache enabled, you may use Imagecache profiles instead of image height/width. You can set a default profile on the settings page, or use it manually with taxonomy_image_display(), like this:</p>
<pre>
taxonomy_image_display($term->tid, NULL, \'PROFILE_NAME\');
</pre>
         <p>set <em>PROFILE_NAME</em> to whatever profile you want to use.</p>');
      }
      if (module_exists('taxonomy_image_node_display') || module_exists('taxonomy_image_link_alter')) {
        $output .= t('<h3>Add-on Feature Modules</h3>');
      }
      if (module_exists('taxonomy_image_node_display')) {
        $output .= t('<h4>Taxonomy Image Node Display</h4><p>With this add-on feature, Taxonomy Image will automatically include all available term-associated images into the display of selected node types. The weight option determines where in the output the image will be positioned. Another option specifies whether or not to add a "taxonomy/term/nn" link to each image. Configuration is part of the usual Taxonomy Image settings page.</p>');
      }
      if (module_exists('taxonomy_image_link_alter')) {
        $output .= t('<h4>Taxonomy Image Link Alter</h4><p>With this add-on feature, Taxonomy Image will automatically modify the standard taxonomy term display to use the term\'s image, if it has one. Configuration is part of the usual Taxonomy Image settings page.</p>');
      }
      if (module_exists('taxonomy_image_blocks')) {
        $output .= t('<h4>Taxonomy Image Blocks</h4><p>This add-on feature provides a block in which the terms and images are shown on full node display. Configuration is part of the usual block settings page.</p>');
      }
      return $output;
  }
}

/**
 * Implementation of hook_menu.
 */
function taxonomy_image_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/content/taxonomy/taxonomy_image',
      'title' => t('Images'),
      'callback' => 'taxonomy_image_overview',
      'access' => user_access('administer taxonomy images'),
      'description' => t('An overview of taxonomy images'),
      'type' => MENU_LOCAL_TASK,
    );

    // Admin Settings
    $items[] = array(
      'path' => 'admin/settings/taxonomy_image',
      'title' => t('Taxonomy Image'),
      'callback' => 'taxonomy_image_admin_settings',
      'access' => user_access('administer site configuration'),
      'description' => t('Global configuration of taxonomy image functionality.'),
      'type' => MENU_NORMAL_ITEM,
    );
    $items[] = array(
      'path' => 'admin/settings/taxonomy_image/general',
      'title' => t('General'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'taxonomy_image_admin_form',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -1,
    );
  }
  return $items;
}

/**
 * Implementation of hook_user().
 */
function taxonomy_image_user($type, $edit, $user, $category = 'account') {
  switch ($type) {
    case 'form':
      if (user_access('can disable taxonomy images') && $category == 'account') {
        $form['content_images'] = array(
          '#type' => 'fieldset',
          '#title' => t('Content images'),
        );
        $form['content_images']['taxonomy_image_disable_images'] = array(
          '#type' => 'checkbox',
          '#title' => t('Disable images'),
          '#return_value' => 1,
          '#default_value' => !empty($user->taxonomy_image_disable_images),
          '#description' => variable_get('taxonomy_image_disable', t('Check this box to disable the display of content images.')),
        );
        return $form;
      }
      break;
  }
}

/**
 * Administration Page
 */
function taxonomy_image_admin_settings() {
  return drupal_get_form('taxonomy_image_admin_form');
}
function taxonomy_image_admin_form() {
  $error = NULL;
  if (!file_check_directory(file_create_path(variable_get('taxonomy_image_path', 'category_pictures')), FILE_CREATE_DIRECTORY)) {
    $error = theme('error', t('The picture directory does not exist, or is not writable.'));
  }
  drupal_add_css(drupal_get_path('module', 'taxonomy_image') . '/taxonomy_image.css');
  $files_path = variable_get('file_directory_path', 'files') . '/';
  $form['general'] = array(
    '#type' => 'fieldset',
    '#title' => t('General Options'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -6,
  );
  $form['general']['taxonomy_image_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Picture image path'),
    '#default_value' => variable_get('taxonomy_image_path', 'category_pictures'),
    '#size' => 60,
    '#maxlength' => 255,
    '#field_prefix' => '<strong>' . $files_path . '</strong>',
    '#description' => t('Subdirectory in the directory "%dir" where category pictures will be stored. Lower case is recommended. ', array(
      '%dir' => $files_path,
    )) . $error,
  );
  $form['general']['taxonomy_image_disable'] = array(
    '#type' => 'textarea',
    '#rows' => 1,
    '#title' => t('User "Disable" box description'),
    '#default_value' => variable_get('taxonomy_image_disable', t('Check this box to disable the display of content images.')),
    '#description' => t('This allows you to describe the ability to disable Taxonomy Image display in a manner that your users will understand.'),
  );
  $form['general']['taxonomy_image_link_title'] = array(
    '#type' => 'radios',
    '#options' => array(
      t('Term description'),
      t('Term name'),
    ),
    '#title' => t('Link title'),
    '#default_value' => variable_get('taxonomy_image_link_title', 0),
    '#description' => t('This indicates which information to use for the link title (tool tip). If you choose "description" and one is not available, the title will be used.'),
    '#prefix' => '<div class="taxonomy_image_radios">',
    '#suffix' => '</div>',
  );
  $form['general']['taxonomy_image_default'] = array(
    '#type' => 'textfield',
    '#title' => t('Default image'),
    '#default_value' => variable_get('taxonomy_image_default', NULL),
    '#description' => t('If you specify a default image and the requested term has no image, the default will be used. You must enter the entire image path.'),
    '#size' => 80,
    '#maxlength' => 255,
  );
  $form['resizing'] = array(
    '#type' => 'fieldset',
    '#title' => t('Resizing Options'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -4,
  );
  $ic_exists = module_exists('imagecache');
  if ($ic_exists) {
    $form['resizing']['ic'] = array(
      '#type' => 'fieldset',
      '#title' => t('Resizing Options - Imagecache'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#weight' => -3,
    );
    $form['resizing']['ic']['taxonomy_image_imagecache_preset'] = array(
      '#type' => 'radios',
      '#title' => t('Imagecache Preset'),
      '#description' => t('The Imagecache module is available. If you select one of these presets, or provide one in your call, then Taxonomy Image will use Imagecache to process the image and ignore the following settings. "ORIGINAL" effectively disables Imagecache and uses the following settings.'),
      '#options' => drupal_map_assoc(_taxonomy_image_presets()),
      '#default_value' => variable_get('taxonomy_image_imagecache_preset', NULL),
      '#prefix' => '<div class="taxonomy_image_radios">',
      '#suffix' => '</div>',
    );
  }
  $form['resizing']['ti'] = array(
    '#type' => 'fieldset',
    '#title' => t('Resizing Options - Taxonomy Image'),
    '#collapsible' => TRUE,
    '#collapsed' => $ic_exists,
    '#weight' => -2,
  );
  $form['resizing']['ti']['taxonomy_image_resize'] = array(
    '#type' => 'radios',
    '#title' => t('Constraint'),
    '#default_value' => variable_get('taxonomy_image_resize', 0),
    '#options' => array(
      3 => 'Exact',
      2 => 'Not less than',
      1 => 'Not greater than',
      0 => 'Disabled',
    ),
    '#description' => t('This option allows you to control the size of pictures displayed by this module.  If set to "disabled," pictures will not be resized, i.e. displayed exactly as they are uploaded.  If set to "not greater than," pictures larger than the specified size will be scaled down.  If set to "not less than," pictures smaller than the specified size will be scaled up.  If set to "exact," pictures will be resized to exactly the specified dimension(s).'),
    '#prefix' => '<div class="taxonomy_image_radios">',
    '#suffix' => '</div>',
  );
  $form['resizing']['ti']['taxonomy_image_height'] = array(
    '#type' => 'textfield',
    '#title' => t('Picture height'),
    '#default_value' => variable_get('taxonomy_image_height', ''),
    '#size' => 5,
    '#maxlength' => 6,
    '#field_suffix' => '<em>' . t('pixels') . '</em>',
    '#description' => t('Specify a height in pixels.  If left blank or set to 0 this field is ignored.'),
  );
  $form['resizing']['ti']['taxonomy_image_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Picture width'),
    '#default_value' => variable_get('taxonomy_image_width', ''),
    '#size' => 5,
    '#maxlength' => 6,
    '#field_suffix' => '<em>' . t('pixels') . '</em>',
    '#description' => t('Specify a width in pixels.  If left blank or set to 0 this field is ignored.'),
  );
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => -3,
  );
  if ($ic_exists) {
    $form['advanced']['taxonomy_image_admin_preset'] = array(
      '#type' => 'radios',
      '#title' => t('Admin Preset'),
      '#description' => t('This setting is for the images shown in the Admin page. "ORIGINAL" causes the standard non-Imagcache resizing option to be used.'),
      '#options' => drupal_map_assoc(_taxonomy_image_presets()),
      '#default_value' => variable_get('taxonomy_image_admin_preset', 'ORIGINAL'),
      '#prefix' => '<div class="taxonomy_image_radios">',
      '#suffix' => '</div>',
    );
  }
  $form['advanced']['taxonomy_image_wrapper'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add wrapper div tag'),
    '#default_value' => variable_get('taxonomy_image_wrapper', FALSE),
    '#description' => t('This option indicates whether to add a "&lt;div&gt;" tag to the image. This may be useful in lists where the images are not of the same width.'),
  );
  $form['advanced']['taxonomy_image_recursive'] = array(
    '#type' => 'radios',
    '#title' => t('Recursive image display'),
    '#default_value' => variable_get('taxonomy_image_recursive', 0),
    '#options' => array(
      0 => 'Disabled',
      1 => 'Enabled',
    ),
    '#description' => t("When enabled, taxonomy_image_display() will recursively search for an image to display, starting with the passed in term, then trying the term's parents.  This functionality is only useful if you have defined hierarchical taxonomies, and multiple terms within a tree will share the same image.  If this doesn't mean anything to you, leave this option disabled."),
    '#prefix' => '<div class="taxonomy_image_radios">',
    '#suffix' => '</div>',
  );
  return system_settings_form($form);
}

/**
 * Helper function for Imagecache presets.
 */
function _taxonomy_image_presets() {
  if (module_exists('imagecache')) {
    if (function_exists('_imagecache_get_presets')) {
      $presets = _imagecache_get_presets();
    }
    elseif (function_exists('imagecache_presets')) {
      $ic_presets = imagecache_presets();
      $presets = array();
      foreach ($ic_presets as $preset_id => $preset_info) {
        $presets[$preset_info['presetid']] = $preset_info['presetname'];
      }
    }
    else {
      drupal_set_message(t('Unrecognized Imagecache API.'), 'error');
      return FALSE;
    }
    $presets[0] = 'ORIGINAL';
    sort($presets);
    return $presets;
  }
  else {
    return FALSE;
  }
}
function taxonomy_image_views_tables() {
  $tables['term_image'] = array(
    'name' => 'term_image',
    'join' => array(
      'left' => array(
        'table' => 'term_node',
        'field' => 'tid',
      ),
      'right' => array(
        'field' => 'tid',
      ),
    ),
    'fields' => array(
      'tid' => array(
        'name' => t('Taxonomy Image: Image'),
        'help' => t('The image associated with the node\'s taxonomy term.'),
        'handler' => array(
          'taxonomy_image_views_handler' => t('Image'),
          'taxonomy_image_views_handler_link' => t('Image as Link'),
        ),
      ),
    ),
  );
  if (module_exists('imagecache')) {
    $tables['term_image']['fields']['tid']['option']['#options'] = _taxonomy_image_presets();
    $tables['term_image']['fields']['tid']['option']['#type'] = 'select';
  }
  return $tables;
}
function taxonomy_image_views_arguments() {
  $arguments = array(
    'taximage' => array(
      'name' => t('Taxonomy: Term Image'),
      'handler' => 'taxonomy_image_handler_arg_taximg',
      'option' => 'string',
      'help' => t('The argument will filter by a taxonomy term image.'),
    ),
  );
  return $arguments;
}

/**
 * Handler for Taxonomy Image views field.
 */
function taxonomy_image_views_handler($fieldinfo, $fielddata, $value, $data) {
  if (empty($fielddata['options'])) {
    return taxonomy_image_display($value, NULL, NULL, array(
      'wrapper' => FALSE,
    ));
  }
  else {
    $profile = imagecache_preset($fielddata['options']);
    $profile_name = $profile['presetname'];
    $img = taxonomy_image_display($value, $data->name, $profile_name, array(
      'wrapper' => FALSE,
    ));
    return $img;
  }
}

/**
 * Handler for Taxonomy Image views field as link.
 */
function taxonomy_image_views_handler_link($fieldinfo, $fielddata, $value, $data) {
  if (empty($fielddata['options'])) {
    $img = taxonomy_image_display($value, $data->name, NULL, array(
      'wrapper' => FALSE,
    ));
    $img = l($img, drupal_get_path_alias(taxonomy_term_path(taxonomy_get_term($value))), array(), NULL, NULL, FALSE, TRUE);
    return $img;
  }
  else {
    $profile = imagecache_preset($fielddata['options']);
    $profile_name = $profile['presetname'];
    $img = taxonomy_image_display($value, $data->name, $profile_name, array(
      'wrapper' => FALSE,
    ));
    $img = l($img, drupal_get_path_alias(taxonomy_term_path(taxonomy_get_term($value))), array(), NULL, NULL, FALSE, TRUE);
    return $img;
  }
}
function taxonomy_image_handler_arg_taximg($op, &$query, $argtype, $arg = '') {
  switch ($op) {
    case 'summary':
      $query
        ->ensure_table('term_data');
      $query
        ->add_field('name', 'term_data');
      $query
        ->add_field('weight', 'term_data');
      $query
        ->add_field('tid', 'term_data');
      $fieldinfo['field'] = "term_data.name";
      return $fieldinfo;
    case 'sort':
      $query
        ->add_orderby('term_data', 'weight', $argtype);
      $query
        ->add_orderby('term_data', 'name', $argtype);
      break;
    case 'filter':
      $query
        ->ensure_table('term_data');
      $query
        ->add_where('term_data.tid = %d', $arg);
      $query
        ->set_distinct();
      break;
    case 'link':
      $tid = intval($query->tid);
      if ($tid) {
        $img = taxonomy_image_display($tid, NULL, NULL, array(
          'wrapper' => FALSE,
        ));
      }
      else {
        $img = t('Uncategorized');
      }
      return l($img, "{$arg}/" . $tid, array(), NULL, NULL, FALSE, TRUE);
    case 'title':
      $result = db_query("SELECT name FROM {term_data} WHERE tid = %d", $query);
      $term = db_fetch_object($result);
      return $term->name ? check_plain($term->name) : t('Uncategorized');
  }
}

/**
 * This module is a small helper module to fix an issue with a view.
 */
function taxonomy_image_views_tables_alter(&$data) {
  if (module_exists('taxonomy_image_link_alter')) {
    $vocabularies = taxonomy_get_vocabularies();
    foreach ($vocabularies as $voc) {
      $data["term_node_{$voc->vid}"] = array(
        'name' => 'term_node',
        'provider' => 'internal',
        'join' => array(
          'left' => array(
            'table' => 'node',
            'field' => 'nid',
          ),
          'right' => array(
            'field' => 'nid',
          ),
        ),
        'fields' => array(
          'name' => array(
            'name' => t('Taxonomy: Terms for @voc-name', array(
              '@voc-name' => $voc->name,
            )),
            'sortable' => FALSE,
            'help' => t('This will display all taxonomy terms associated with the node that are members of %voc-name. Note that this causes one extra query per row displayed, and might have a minor performance impact.', array(
              '%voc-name' => $voc->name,
            )),
            'handler' => 'taxonomy_image_handler_field_allterms',
            'vocabulary' => $voc->vid,
            'notafield' => TRUE,
            'option' => array(
              '#type' => 'select',
              '#options' => array(
                'link' => 'As links',
                'nolink' => 'Without links',
                'image' => 'As Image',
                'imagenolink' => 'As Image NO links',
              ),
            ),
          ),
        ),
        'filters' => array(
          'tid' => array(
            'name' => t('Taxonomy: Terms for @voc-name', array(
              '@voc-name' => $voc->name,
            )),
            'value' => views_taxonomy_form($voc),
            //views does handle this as array regardless what we state here due to the operators
            'value-type' => 'array',
            'tags' => $voc->tags,
            'operator' => 'views_handler_operator_andor',
            'handler' => 'views_handler_filter_tid_custom',
            'option' => 'string',
            'vocabulary' => $voc->vid,
            'help' => t("Only terms associated with %voc-name will appear in the select box for this filter. When filtering by taxonomy term you may specify the 'depth' as an option. Please see the taxonomy help for more information.", array(
              '%voc-name' => $voc->name,
            )),
          ),
        ),
      );
    }
  }
}

/**
 * Display all the terms for a given vocabulary
 */
function taxonomy_image_handler_field_allterms($fieldinfo, $fielddata, $value, $data) {
  if ($fieldinfo['vocabulary']) {
    $terms = taxonomy_node_get_terms_by_vocabulary($data->nid, $fieldinfo['vocabulary']);
  }
  else {
    $terms = taxonomy_node_get_terms($data->nid);
  }
  switch ($fielddata['options']) {
    case 'nolink':
      $links = array();
      foreach ($terms as $tid => $term) {
        $links[] = check_plain($term->name);
      }
      $links = !empty($links) ? implode(' | ', $links) : '';
      break;
    case 'image':

      // load node to get type which is needed  by taxonomy_image_link_alter to check that the type is activated to replace links by images.
      $node = node_load($data->nid);

      // copy terms, but should not be needed
      $node->taxonomy = $terms;

      // taxonomy_image_link_alter will hook on the taxonomy_link call
      $links = theme('links', taxonomy_link('taxonomy terms', $node));
      break;
    case 'imagenolink':
      $links = array();
      foreach ($terms as $tid => $term) {
        $links[] = taxonomy_image_display($tid, NULL, NULL, array(
          'wrapper' => FALSE,
        ));
      }
      $links = implode(' ', $links);
      break;
    default:
      $node = new stdClass();
      $node->taxonomy = $terms;
      $links = theme('links', taxonomy_link('taxonomy terms', $node));
  }
  return $links;
}
function taxonomy_image_file_download($file) {
  if (user_access('access taxonomy images')) {
    $path = file_create_path($file);
    if (function_exists('mime_content_type')) {
      if ($type = mime_content_type($path)) {
        return array(
          "Content-type: {$type}",
        );
      }
    }
    list($width, $height, $type, $attr) = getimagesize($path);
    $types = array(
      IMAGETYPE_GIF => 'image/gif',
      IMAGETYPE_JPEG => 'image/jpeg',
      IMAGETYPE_PNG => 'image/png',
      IMAGETYPE_SWF => 'application/x-shockwave-flash',
      IMAGETYPE_PSD => 'image/psd',
      IMAGETYPE_BMP => 'image/bmp',
      IMAGETYPE_TIFF_II => 'image/tiff',
      IMAGETYPE_TIFF_MM => 'image/tiff',
      IMAGETYPE_JPC => 'application/octet-stream',
      IMAGETYPE_JP2 => 'image/jp2',
      IMAGETYPE_JPX => 'application/octet-stream',
      IMAGETYPE_JB2 => 'application/octet-stream',
      IMAGETYPE_SWC => 'application/x-shockwave-flash',
      IMAGETYPE_IFF => 'image/iff',
      IMAGETYPE_WBMP => 'image/vnd.wap.wbmp',
      IMAGETYPE_XBM => 'image/xbm',
    );
    if (isset($types[$type])) {
      return array(
        'Content-type: ' . $types[$type],
      );
    }
  }
}

// Taxonomy_image specific functions.
function taxonomy_image_admin() {
  global $form_values;
  $op = $_POST['op'];
  $tid = $_POST['tid'];

  // TODO: Use menus, not arg()
  if (empty($op)) {
    $op = arg(3);
  }
  switch ($op) {
    case 'image':
      if (arg(4) == 'add' || arg(4) == 'edit') {
        $output = drupal_get_form('taxonomy_image_form', (array) taxonomy_image_get_term(arg(5)));
        break;
      }
      $output = taxonomy_image_overview();
      break;
    case t('Save'):
      $output = taxonomy_image_save($tid);
      $output = taxonomy_image_overview();
      break;
    case t('Delete'):
      $output = taxonomy_image_delete($tid);
      $output = taxonomy_image_overview();
      break;
    default:
      $output = taxonomy_image_overview();
  }
  print theme('page', $output);
}
function taxonomy_image_overview() {
  $dest = drupal_get_destination();
  $output = '';
  if (variable_get('taxonomy_image_recursive', 0)) {
    $output .= '<h4>' . t('Recursively displaying images.') . '</h4>';
  }
  $admin_preset = variable_get('taxonomy_image_admin_preset', NULL);
  $header = array(
    t('Name'),
    t('Node Types'),
    t('Image'),
    t('Op'),
  );
  $attrs = array(
    'class' => 'taxonomy_image_overview',
  );
  $vocabularies = taxonomy_get_vocabularies();
  foreach ($vocabularies as $vocabulary) {
    $types = array();
    $rows = array();
    $types = $vocabulary->nodes;
    $rows[] = array(
      array(
        'data' => '<strong>' . check_plain($vocabulary->name) . '</strong>',
        'valign' => 'top',
        'class' => 'title',
      ),
      array(
        'data' => implode(', ', $types),
        'align' => 'center',
        'valign' => 'middle',
      ),
      '',
      '',
    );
    $tree = taxonomy_get_tree($vocabulary->vid);
    if ($tree) {
      foreach ($tree as $term) {
        $data = str_repeat('--', $term->depth) . ($term->depth > 0 ? ' ' : NULL) . check_plain($term->name);
        $exists = taxonomy_image_display($term->tid, array(), $admin_preset);
        $op = l(t('edit term'), 'admin/content/taxonomy/edit/term/' . $term->tid, array(), $dest);

        // Use taxonomy_image_display() instead of _taxonomy_image_exists() in
        // case image display recursion is enabled...
        $image = $exists ? $exists : '<em>' . t('none') . '</em>';
        $rows[] = array(
          array(
            'data' => $data,
            'colspan' => 2,
          ),
          array(
            'data' => $image,
            'align' => 'center',
          ),
          $op,
        );
      }
    }
    $output .= '<br/>' . theme('table', $header, $rows, $attrs);
  }
  return $output;
}
function taxonomy_image_get_term($tid) {
  return db_fetch_object(db_query('SELECT d.name, d.description, d.tid, i.path FROM {term_data} d LEFT JOIN {term_image} i USING(tid) WHERE d.tid = %d', $tid));
}
function taxonomy_image_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'taxonomy_form_term':
      $preview = @taxonomy_image_display($form['tid']['#value']);
      $form['#attributes'] = array(
        'enctype' => 'multipart/form-data',
      );
      $form['taxonomy_image'] = array(
        '#type' => 'fieldset',
        '#title' => t('Term Image'),
        '#collapsible' => TRUE,
        '#collapsed' => empty($preview),
      );
      if ($preview) {
        $form['taxonomy_image']['current_image'] = array(
          '#type' => 'fieldset',
          '#title' => t('Current Image'),
        );
        $form['taxonomy_image']['current_image']['image'] = array(
          '#value' => $preview,
        );
        $form['taxonomy_image']['current_image']['current_image_delete'] = array(
          '#type' => 'checkbox',
          '#title' => t('Delete'),
          '#prefix' => '<div class="taxonomy_image_checkboxes">',
          '#suffix' => '</div>',
        );
      }
      $form['taxonomy_image']['new_image']['path'] = array(
        '#type' => 'file',
        '#title' => t('Upload image'),
        '#size' => 40,
        '#description' => t("The image file you wish to associate this term."),
      );
      $form['submit']['#weight'] = 10;
      $form['delete']['#weight'] = 10;
      break;
  }
  return $form;
}

/**
 * Implementation of hook_taxonomy().
 */
function taxonomy_image_taxonomy($op, $type, $form_values = NULL) {
  $directory = file_create_path(variable_get('taxonomy_image_path', 'category_pictures'));
  file_check_directory($directory, FILE_CREATE_DIRECTORY);

  // We're only interested in term changes.
  if ($type != 'term') {
    return;
  }
  $tid = $form_values['tid'];
  switch ($op) {
    case 'insert':
    case 'update':

      // Delete the cached version.
      cache_clear_all("taxonomy_image:{$tid}", 'cache_tax_image');

      // Did they mark it to delete?
      if (!empty($form_values['current_image_delete'])) {
        taxonomy_image_delete($tid);
      }
      $is_current_image = db_result(db_query('SELECT COUNT(tid) FROM {term_image} WHERE tid=%d', $tid));
      if ($file = file_save_upload('path', $directory)) {
        if ($is_current_image) {

          // Delete old image before saving the new one.
          taxonomy_image_delete($tid);
        }
        $insert = db_query("INSERT INTO {term_image} (tid, path) VALUES ('%d', '%s')", $tid, $file->filepath);
        if ($insert == FALSE) {
          $message = theme('error', t('Database insert failed. [tid = !tid, path = @path.', array(
            '!tid' => $tid,
            '@path' => $file->filepath,
          )));
        }
        else {
          $message = t('Image uploaded as @name.', array(
            '@name' => $file->filepath,
          ));
        }
      }
      else {
        if (!file_check_directory($directory)) {

          // we know what's wrong, so generate a more useful error message
          $message = theme('error', t('The category picture directory "%dir" does not exist, or is not writable.', array(
            '%dir' => variable_get('file_directory_path', 'files') . '/' . variable_get('taxonomy_image_path', 'category_pictures'),
          )));
        }
        else {
          $message = theme('error', t('Image upload failed.'));
        }
      }
      return drupal_set_message($message);
    case 'delete':
      taxonomy_image_delete($tid);
      return;
  }
}
function taxonomy_image_delete($tid) {
  $old_path = db_result(db_query('SELECT path FROM {term_image} WHERE tid=%d', $tid));
  $how_many = db_result(db_query("SELECT COUNT(path) FROM {term_image} WHERE path='%s'", $old_path));
  $taxonomy_image_path = file_directory_path() . '/' . variable_get('taxonomy_image_path', 'category_pictures');
  if (drupal_substr($old_path, 0, drupal_strlen($taxonomy_image_path)) != $taxonomy_image_path) {

    // This file is not one of our own, don't actually delete it.
    $file_del_ok = TRUE;
    $db_del_ok = TRUE;
  }
  else {
    if ($how_many == 1) {

      // This is the only term using this file, so it is safe to delete it.
      $file_del_ok = db_query("DELETE FROM {files} WHERE filepath='%s'", $old_path);
    }
    else {

      // Pretend we deleted it.
      $file_del_ok = TRUE;
      drupal_set_message(t('Not deleted from the files table because it is use on !count other terms.', array(
        '!count' => $how_many - 1,
      )));
    }
  }
  $db_del_ok = db_query('DELETE FROM {term_image} WHERE tid=%d', $tid);
  if ($file_del_ok && $db_del_ok) {
    drupal_set_message(t('@name image removed.', array(
      '@name' => $old_path,
    )));
  }
  else {
    drupal_set_message(t('Image delete failed. File: !file, Db: !db.', array(
      '!file' => $file_del_ok ? 'yes' : 'no',
      '!db' => $db_del_ok ? 'yes' : 'no',
    )));
  }
  return;
}
function _taxonomy_image_exists($tid) {
  if (db_fetch_object(db_query('SELECT path FROM {term_image} WHERE tid=%d', $tid))) {
    return TRUE;
  }
  return FALSE;
}

Functions

Namesort descending Description
taxonomy_image_admin
taxonomy_image_admin_form
taxonomy_image_admin_settings Administration Page
taxonomy_image_delete
taxonomy_image_display Function to display the image associated with the given term id. An html <img> tag will be returned if an image is found. The link format can be modified with the tags parameter.
taxonomy_image_file_download
taxonomy_image_form_alter
taxonomy_image_get_object Function to get an image object with more useful data for custom formatting.
taxonomy_image_get_term
taxonomy_image_get_url Function to get a url for an image, for use in css, html or other client-side code.
taxonomy_image_handler_arg_taximg
taxonomy_image_handler_field_allterms Display all the terms for a given vocabulary
taxonomy_image_help Implementation of hook_help.
taxonomy_image_menu Implementation of hook_menu.
taxonomy_image_overview
taxonomy_image_parse_tags Function to parse tags (HTML) string.
taxonomy_image_perm Implementation of hook_perm.
taxonomy_image_resize Function to resize the image.
taxonomy_image_taxonomy Implementation of hook_taxonomy().
taxonomy_image_user Implementation of hook_user().
taxonomy_image_views_arguments
taxonomy_image_views_handler Handler for Taxonomy Image views field.
taxonomy_image_views_handler_link Handler for Taxonomy Image views field as link.
taxonomy_image_views_tables
taxonomy_image_views_tables_alter This module is a small helper module to fix an issue with a view.
_taxonomy_image_exists
_taxonomy_image_presets Helper function for Imagecache presets.