You are here

function taxonomy_image_get_object in Taxonomy Image 6

Same name and namespace in other branches
  1. 5 taxonomy_image.module \taxonomy_image_get_object()

Function to get an image object with more useful data for custom formatting.

Parameters

$tid - the term tid.:

Return value

image object.

4 calls to taxonomy_image_get_object()
taxonomy_image_attach_form_alter in contributed/taxonomy_image_attach/taxonomy_image_attach.module
Add an image selector to the taxonomy_image fieldset alongside the upload field.
taxonomy_image_display in ./taxonomy_image.module
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_get_url in ./taxonomy_image.module
Function to get a url for an image, for use in css, html or other client-side code.
taxonomy_image_link_alter in contributed/taxonomy_image_link_alter/taxonomy_image_link_alter.module
Implement hook_link_alter to picturize the taxonomy term links

File

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

Code

function taxonomy_image_get_object($tid, $recursive = NULL) {
  global $user;
  static $image = array();
  static $mypath, $fullpath;

  // Get base path for my images.
  if (!isset($mypath)) {
    $mypath = variable_get('taxonomy_image_path', 'category_pictures') . '/';
    $fullpath = file_directory_path() . '/' . $mypath;
  }
  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] = $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($fullpath . $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);
      }
    }
  }

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

    //    $image[$tid]->url = file_create_url($mypath . $image[$tid]->path);
    $image[$tid]->url = url($fullpath . $image[$tid]->path, array(
      'absolute' => TRUE,
    ));
  }

  // Get more properties if we had an image.
  if (!empty($image[$tid]->path)) {
    $image[$tid]->tid = $tid;
    $image[$tid]->vid = $term->vid;
    $p = $fullpath . $image[$tid]->path;
    if (is_readable($p)) {
      $img = getimagesize($p);
    }

    // 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}", $image[$tid], 'cache_tax_image');
  return $image[$tid];
}