, May 2004.
*/
/**
* Call this function from your theme or other php code to display the
* image associated with the given term id. An html tag will be returned
* if an image is found. The format of the link can be modified with the
* tags parameter.
*
* @param int $tid the term id.
* @param string $tags optional tags to add into the
link
*
* @return string An html
link.
*/
function taxonomy_image_display($tid, $tags = NULL) {
global $user;
static $image = array();
if (user_access('access taxonomy images') &&
!$user->taxonomy_image_disable_images) {
// do lookup, return full display path
if (!$image[$tid]->url) {
if ($image[$tid] = db_fetch_object(db_query('SELECT i.path, d.name FROM {term_image} i INNER JOIN {term_data} d WHERE i.tid = d.tid AND i.tid = %d', $tid))) {
$image[$tid]->url = file_create_url($image[$tid]->path);
}
else if (variable_get('taxonomy_image_recursive', 0)) {
// 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))) {
$tid = $parent->tid;
if ($image[$tid]->url) {
// we have already loaded this image from the database
$image[$orig] = $image[$tid];
break;
}
else if ($image[$tid] = db_fetch_object(db_query('SELECT i.path, d.name FROM {term_image} i INNER JOIN {term_data} d WHERE i.tid = d.tid AND i.tid = %d', $tid))) {
// we found a parent with a configured image, use it
$image[$tid]->url = file_create_url($image[$tid]->path);
$image[$orig] = $image[$tid];
break;
}
}
}
}
if ($image[$tid]->url) {
if (!$image[$tid]->width || !$image[$tid]->height) {
list($image[$tid]->width, $image[$tid]->height) = getimagesize($image[$tid]->path);
// handle image resizing
switch (variable_get('taxonomy_image_resize', 0)) {
case 3: // exact
if ($width = variable_get('taxonomy_image_width', 0))
$image[$tid]->width = $width;
if ($height = variable_get('taxonomy_image_height', 0))
$image[$tid]->height = $height;
break;
case 2: // not less than
if (($width = variable_get('taxonomy_image_width', 0)) &&
($width > $image[$tid]->width)) {
$width_scale = $image[$tid]->width / $width;
}
if (($height = variable_get('taxonomy_image_height', 0)) &&
($height > $image[$tid]->height)) {
$height_scale = $image[$tid]->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[$tid]->height = $image[$tid]->height / $scale;
$image[$tid]->width = $image[$tid]->width / $scale;
}
break;
case 1: // not greater than
if (($width = variable_get('taxonomy_image_width', 0)) &&
($width < $image[$tid]->width)) {
$width_scale = $image[$tid]->width / $width;
}
if (($height = variable_get('taxonomy_image_height', 0)) &&
($height < $image[$tid]->height)) {
$height_scale = $image[$tid]->height / $height;
}
if ($height_scale || $width_scale) {
$scale = max($width_scale, $height_scale);
$image[$tid]->height = $image[$tid]->height / $scale;
$image[$tid]->width = $image[$tid]->width / $scale;
}
break;
}
}
$current = $image[$tid];
return "
";
}
}
return '';
}
// standard Drupal functions
function taxonomy_image_perm() {
return array ('access taxonomy images', 'administer taxonomy images', 'can disable taxonomy images');
}
function taxonomy_image_help($section = '') {
switch ($section) {
case 'admin/content/taxonomy/image':
return t('The taxonomy_image module allows site administrators to associate images with category 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 upload a new image for a specific term, click "upload image" next to the term. To modify or delete and existing image, click "edit image". To learn more about how to create vocabularies and terms, review the taxonomy help page.', array('%taxonomy-help' => url('admin/help/taxonomy')));
case 'admin/help#taxonomy_image':
return t('
The taxonomy_image module allows site administrators to associate images with category terms. Once defined, this association allows Drupal themes 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.
The module allows both a one-to-one term-to-image relationship, and a many-to-one terms-to-image relationship.
The taxonomy_image module requires that the taxonomy module also be enabled.
With the taxonomy_image module enabled, images can be uploaded and associated with category terms at \'administer >> categories >> images\'. On that page you will find tables containing all your vocabularies and terms. Next to each term is a link titled \'upload image\' which you can click to upload an image for that term. After clicking that link, you will be brought to another page with a small \'Add images\' form. Using the \'browse\' button you can select your image then click \'Save\'.
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 >> settings >> taxonomy_image\' to force the display height and/or width of all taxonomy images.
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.
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 >> settings >> taxonomy_image\', you can enable \'Recursive image disaply\'. 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 alternative image).
To actually display images from your theme, you will have to modify the theme to make a call to taxonomy_image_display(). 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:
foreach (taxonomy_node_get_terms($node->nid) as $term) {
if ($image = taxonomy_image_display($term->tid)) {
$output .= "$image";
}
');
}
}
function taxonomy_image_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'admin/content/taxonomy/image', 'title' => t('Category Images'),
'callback' => 'taxonomy_image_admin',
'access' => user_access('administer taxonomy images'),
'type' => MENU_LOCAL_TASK);
// Admin Settings
$items[]= array (
'path' => 'admin/settings/taxonomy_image',
'title' => t('Taxonomy Image'),
'callback' => 'drupal_get_form',
'callback arguments' => array('taxonomy_image_admin_settings'),
'access' => user_access('administer site configuration'),
'description' => t('Global configuration of taxonomy image functionality.'),
'type' => MENU_NORMAL_ITEM,
);
}
return $items;
}
/**
* Implementation of hook_user().
*/
function taxonomy_image_user($type, $edit, $user) {
switch ($type) {
case 'form':
if (user_access('can disable taxonomy images')) {
$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' => $user->taxonomy_image_disable_images,
'#description' => t('Check this box to disable the display of content images.'),
);
return $form;
}
break;
}
}
/**
* Administration Page
*/
function taxonomy_image_admin_settings() {
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.'));
}
$form['pictures'] = array(
'#type' => 'fieldset',
'#title' => t('Pictures'),
);
$form['pictures']['taxonomy_image_path'] = array(
'#type' => 'textfield',
'#title' => t('Picture image path'),
'#default_value' => variable_get('taxonomy_image_path', 'category_pictures'),
'#size' => 45,
'#maxlength' => 255,
'#description' => t('Subdirectory in the directory "%dir" where category pictures will be stored.', array('%dir' => variable_get('file_directory_path', 'files') . '/')) . $error,
);
$form['pictures']['taxonomy_image_resize'] = array(
'#type' => 'radios',
'#title' => t('Picture resize'),
'#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, 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).'),
);
$form['pictures']['taxonomy_image_height'] = array(
'#type' => 'textfield',
'#title' => t('Picture height'),
'#default_value' => variable_get('taxonomy_image_height', ''),
'#size' => 5,
'#maxlength' => 6,
'#description' => t('Specify a height in pixels. If left blank or set to 0 this field is ignored.'),
);
$form['pictures']['taxonomy_image_width'] = array(
'#type' => 'textfield',
'#title' => t('Picture width'),
'#default_value' => variable_get('taxonomy_image_width', ''),
'#size' => 5,
'#maxlength' => 6,
'#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'),
);
$form['advanced']['taxonomy_image_recursive'] = array(
'#type' => 'radios',
'#title' => t('Recursive image display'),
'#default_value' => variable_get('taxonomy_image_recursive', 0),
'#options' => array(1 => 'Enabled', 0 => 'Disabled'),
'#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.'),
);
$form['node_view'] = array(
'#type' => 'fieldset',
'#title' => t('Node Display'),
);
$form['node_view']['taxonomy_image_node_view'] = array(
'#type' => 'checkboxes',
'#title' => t('Display taxonomy images on tagged nodes'),
'#options' => array_map('check_plain', node_get_types('names')),
'#default_value' => variable_get('taxonomy_image_node_view', array()),
);
$form['node_view']['taxonomy_image_node_view_teaser'] = array(
'#type' => 'checkbox',
'#title' => t('Show Taxonomy Image in node teaser view'),
'#default_value' => variable_get('taxonomy_image_node_view_teaser', TRUE),
);
$form['node_view']['taxonomy_image_node_view_link'] = array(
'#type' => 'checkbox',
'#title' => t('Link displayed Taxonomy Image to taxonomy/term/n page'),
'#default_value' => variable_get('taxonomy_image_node_view_link', TRUE),
);
$form['node_view']['taxonomy_image_node_view_weight'] = array(
'#type' => 'weight',
'#title' => t('Display weight of taxonomy images'),
'#options' => array_map('check_plain', node_get_types('names')),
'#default_value' => variable_get('taxonomy_image_node_view_weight', -5),
);
return system_settings_form($form);
}
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");
}
// support for pre-PHP 4.3+
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() {
$output = '';
if (variable_get('taxonomy_image_recursive', 0)) {
$output .= '