? image_13.patch Index: image.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/image/image.module,v retrieving revision 1.197.2.3 diff -u -F^f -r1.197.2.3 image.module --- image.module 23 Aug 2006 20:00:31 -0000 1.197.2.3 +++ image.module 9 Nov 2006 22:13:33 -0000 @@ -159,8 +159,14 @@ function image_prepare(&$node, $field_na return; } $node->images['_original'] = $file->filepath; - _image_build_derivatives($node, true); $node->new_file = TRUE; + + /* + * Call hook to allow other modules to modify the original image. + */ + module_invoke_all('image_alter', $node, $file->filepath, '_original'); + + _image_build_derivatives($node, true); } } @@ -235,6 +241,33 @@ function image_form_add_thumbnail($form_ return $form; } +function image_form_image_alter($form, $form_values) { + if (!form_get_errors() && !isset($form_values['new_file'])) { + $node = (object)$form_values; + /* + * Call hook_image_alter() since the original maybe being altered for a + * second or thrid time. + */ + module_invoke_all('image_alter', $node, $file->filepath, '_original'); + + /* + * We need to remove all the images and recreate the derivatives, so + * that hook_image_alter() will be called again. + */ + _image_remove($node); + _image_build_derivatives($node, true); + } +} + +/** + * Implementation of hook_form_alter + */ +function image_form_alter($form_id, &$form) { + if ($form_id == 'image_node_form') { + $form['#validate']['image_form_image_alter'] = array(); + } +} + /** * Implementation of hook_form */ @@ -244,6 +277,9 @@ function image_form(&$node, &$param) { $form['#attributes'] = array("enctype" => "multipart/form-data"); $form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#size' => 60, '#maxlength' => 128, '#required' => TRUE, '#default_value' => $node->title); + if ($node->new_file) { + $form['new_file'] = array('#type' => 'value', '#value' => TRUE); + } $sizes = _image_get_sizes(); $form['images']['#tree'] = TRUE; if ($node->new_file) { @@ -364,7 +400,8 @@ function image_views_tables() { function image_display(&$node, $label = 'preview', $attributes = array()) { // regenerate images? if ($node->images[$label] != $node->images['_original'] && - (!file_exists(file_create_path($node->images[$label])) || + (!isset($node->images[$label]) || + !file_exists(file_create_path($node->images[$label])) || filemtime(file_create_path($node->images[$label])) < variable_get('image_updated', 0))) { _image_build_derivatives($node); } @@ -401,14 +438,14 @@ function image_fetch($nid = 0, $size = ' * Theme a teaser */ function theme_image_teaser($node) { - return l(image_display($node, 'thumbnail'), 'node/'. $node->nid, array(), NULL, NULL, TRUE, TRUE) . $node->teaser; + return l(image_display($node, 'thumbnail'), 'node/'. $node->nid, array(), NULL, NULL, TRUE, TRUE) . $node->teaser; } /** * Theme a body */ function theme_image_body($node, $size) { - return image_display($node, $size) . $node->body; + return image_display($node, $size) . $node->body; } /** @@ -458,6 +495,14 @@ function image_get_latest($count = 1, $t */ function image_views_handler_image_img($fieldinfo, $fielddata, $value, $data) { $node = node_load($data->nid); + if ($node->type != 'image') { + if ($node->iid) { + $node = node_load($node->iid); + } + else { + return ''; + } + } return image_display($node, $fielddata['options']); } @@ -466,6 +511,14 @@ function image_views_handler_image_img($ */ function image_views_handler_image_img_link($fieldinfo, $fielddata, $value, $data) { $node = node_load($data->nid); + if ($node->type != 'image') { + if ($node->iid) { + $node = node_load($node->iid); + } + else { + return ''; + } + } return l(image_display($node, $fielddata['options']), "node/{$node->nid}", array(), NULL, NULL, FALSE, TRUE); } @@ -517,6 +570,15 @@ function _image_build_derivatives(&$node _image_remove($node); } foreach ($sizes as $size) { + if ((!$size['width'] || !$size['height']) && ($info = image_get_info(file_create_path($node->images['_original'])))) { + $aspect = $info['height'] / $info['width']; + if ($size['width'] && !$size['height']) { + $size['height'] = (int)round($size['width'] * $aspect); + } + elseif ($size['height'] && !$size['width']) { + $size['width'] = (int)round($size['height'] / $aspect); + } + } if ($size['label'] && $size['width'] && $size['height']) { if ($info['width'] > $size['width'] || $info['height'] > $size['height']) { $source = file_create_path($node->images['_original']); @@ -529,10 +591,29 @@ function _image_build_derivatives(&$node if (!$temp) { _image_insert($node, $size['label'], file_create_path($destination)); } + module_invoke_all('image_alter', $node, file_create_path($destination), $size['label']); } } else { - $node->images[$size['label']] = $node->images['_original']; + /** + * If there is a module that implements hook_image_alter() then there + * possibility of modifying the derived images, then copy the + * original to the devired and call hook_image_alter() + */ + if (module_implements('image_alter')) { + $source = file_create_path($node->images['_original']); + $destination = _image_filename(basename($source), $size['label'], $temp); + file_copy($source, $destination); + + $node->images[$size['label']] = $destination; + if (!$temp) { + _image_insert($node, $size['label'], file_create_path($destination)); + } + module_invoke_all('image_alter', $node, file_create_path($destination), $size['label']); + } + else { + $node->images[$size['label']] = $node->images['_original']; + } } } }