imagefield.module
<?php
include_once dirname(__FILE__) . '/imagefield_file.inc';
include_once dirname(__FILE__) . '/imagefield_widget.inc';
function imagefield_init() {
if (!module_exists('filefield')) {
module_disable(array('imagefield'));
drupal_set_message(t('The ImageField module has been disabled. The <a href="http://drupal.org/project/filefield">FileField module</a> needs to be installed for it to work properly.'));
return;
}
}
function imagefield_theme() {
return array(
'imagefield_image' => array(
'arguments' => array('file' => NULL, 'alt' => '', 'title' => '', 'attributes' => NULL, 'getsize' => TRUE),
),
'imagefield_item' => array(
'arguments' => array('item' => NULL),
),
'imagefield_widget' => array(
'arguments' => array('element' => NULL),
'file' => 'imagefield_widget.inc',
),
'imagefield_widget_preview' => array(
'arguments' => array('item' => NULL),
),
'imagefield_widget_item' => array(
'arguments' => array('element' => NULL),
),
'imagefield_admin_thumbnail' => array(
'arguments' => array('item' => NULL),
),
'imagefield_formatter_image_plain' => array(
'arguments' => array('element' => NULL),
'file' => 'imagefield_formatter.inc',
),
'imagefield_formatter_image_nodelink' => array(
'arguments' => array('element' => NULL),
'file' => 'imagefield_formatter.inc',
),
'imagefield_formatter_image_imagelink' => array(
'arguments' => array('element' => NULL),
'file' => 'imagefield_formatter.inc',
),
);
}
function imagefield_elements() {
$elements = array();
$filefield_elements = filefield_elements();
$elements['imagefield_widget'] = $filefield_elements['filefield_widget'];
$elements['imagefield_widget']['#process'][] = 'imagefield_widget_process';
$elements['imagefield_widget']['#element_validate'][] = 'imagefield_widget_validate';
$elements['imagefield_widget']['#value_callback'] = 'imagefield_widget_value';
return $elements;
}
function imagefield_file_download($filepath) {
if (strpos($filepath, 'imagefield_thumbs') !== FALSE) {
$original_path = str_replace('imagefield_thumbs/', '', $filepath);
$original_full_path = file_create_path($original_path);
$thumb_full_path = file_create_path($filepath);
$status = db_result(db_query("SELECT status FROM {files} WHERE filepath = '%s'", $original_full_path));
$access = ($status == 0 || module_invoke_all('file_download', $original_path));
if ($access && $info = getimagesize($thumb_full_path)) {
return array(
'Content-Type: ' . $info['mime'],
'Content-Length: ' . filesize($thumb_full_path)
);
}
}
if (strpos($filepath, 'imagefield_default_images') !== FALSE) {
$full_path = file_create_path($filepath);
if ($info = getimagesize($full_path)) {
return array(
'Content-Type: ' . $info['mime'],
'Content-Length: ' . filesize($full_path)
);
}
}
}
function imagefield_widget_info() {
$module_path = drupal_get_path('module', 'imagefield');
return array(
'imagefield_widget' => array(
'label' => t('Image'),
'field types' => array('filefield'),
'multiple values' => CONTENT_HANDLE_CORE,
'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM),
'description' => t('An edit widget for image files, including a preview of the image.'),
),
);
}
function imagefield_widget_settings($op, $widget) {
switch ($op) {
case 'form':
return imagefield_widget_settings_form($widget);
case 'validate':
return imagefield_widget_settings_validate($widget);
case 'save':
return imagefield_widget_settings_save($widget);
}
}
function imagefield_widget(&$form, &$form_state, $field, $items, $delta = 0) {
if (empty($items[$delta])) {
$items[$delta] = array('alt' => '', 'title' => '');
}
$element = filefield_widget($form, $form_state, $field, $items, $delta);
$element['#upload_validators'] = array_merge($element['#upload_validators'], imagefield_widget_upload_validators($field));
return $element;
}
function imagefield_widget_upload_validators($field) {
$validators = array();
$web_extensions = array('jpg', 'jpeg', 'gif', 'png');
$extensions = array_filter(explode(' ', $field['widget']['file_extensions']));
if (empty($extensions)) {
$extensions = $web_extensions;
}
$validators['filefield_validate_extensions'][0] = implode(' ', array_intersect($extensions, $web_extensions));
$validators['filefield_validate_is_image'] = array();
if (!empty($field['widget']['max_resolution']) || !empty($field['widget']['min_resolution'])) {
$validators['filefield_validate_image_resolution'] = array(
$field['widget']['max_resolution'],
$field['widget']['min_resolution'],
);
}
return $validators;
}
function imagefield_field_formatter_info() {
$module_path = drupal_get_path('module', 'imagefield');
$formatters = array(
'image_plain' => array(
'label' => t('Image'),
'field types' => array('filefield'),
'description' => t('Displays image files in their original size.'),
),
'image_nodelink' => array(
'label' => t('Image linked to node'),
'field types' => array('filefield'),
'description' => t('Displays image files in their original size.'),
),
'image_imagelink' => array(
'label' => t('Image linked to file'),
'field types' => array('filefield'),
'description' => t('Displays image files in their original size.'),
),
);
return $formatters;
}
function imagefield_default_value(&$form, &$form_state, $field, $delta) {
return filefield_default_value($form, $form_state, $field, $delta);
}
function imagefield_form_content_field_overview_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'imagefield_form_content_field_overview_submit';
}
function imagefield_form_content_field_overview_submit(&$form, &$form_state) {
if (isset($form_state['fields_added']['_add_new_field']) && isset($form['#type_name'])) {
$new_field = $form_state['fields_added']['_add_new_field'];
$node_type = $form['#type_name'];
$field = content_fields($new_field, $node_type);
if ($field['widget']['module'] == 'imagefield') {
foreach ($field['display_settings'] as $display_type => $display_settings) {
if ($field['display_settings'][$display_type]['format'] == 'default') {
$field['display_settings'][$display_type]['format'] = 'image_plain';
}
}
content_field_instance_update($field);
}
}
}
function theme_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
$file = (array)$file;
if (!is_file($file['filepath'])) {
return '<!-- File not found: '. $file['filepath'] .' -->';
}
if ($getsize) {
if (!empty($file['data']['width']) && !empty($file['data']['height'])) {
$attributes['width'] = $file['data']['width'];
$attributes['height'] = $file['data']['height'];
}
elseif (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])) {
$attributes['width'] = $width;
$attributes['height'] = $height;
}
}
if (!empty($title)) {
$attributes['title'] = $title;
}
$attributes['alt'] = $alt;
$query_string = '';
if (isset($file['timestamp'])) {
$query_character = (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE && variable_get('clean_url', '0') == '0') ? '&' : '?';
$query_string = $query_character . $file['timestamp'];
}
$url = file_create_url($file['filepath']) . $query_string;
$attributes['src'] = $url;
$attributes = drupal_attributes($attributes);
return '<img '. $attributes .' />';
}
function theme_imagefield_item($item) {
return theme('imagefield_image', $item, $item['alt'], $item['title']);
}
function theme_imagefield_widget_preview($item = NULL) {
return '<div class="imagefield-preview">' . theme('imagefield_admin_thumbnail', $item) . '</div>';
}
function theme_imagefield_widget_item($element) {
return theme('filefield_widget_item', $element);
}
function theme_imagefield_admin_thumbnail($item = NULL) {
if (is_null($item) || empty($item['filepath'])) {
return '<!-- link to default admin thumb -->';
}
$thumb_path = imagefield_file_admin_thumb_path($item);
return '<img src="'. file_create_url($thumb_path) .'" title="' . check_plain($item['filename']) . '" />';
}