--- ../../../../drupal-6.10-unpatched/sites/all/modules/image/image.module 2009-01-13 04:39:09.000000000 -0500 +++ image/image.module 2009-03-13 02:08:19.000000000 -0400 @@ -4,11 +4,14 @@ define('IMAGE_ORIGINAL', '_original'); define('IMAGE_PREVIEW', 'preview'); define('IMAGE_THUMBNAIL', 'thumbnail'); +define('IMAGE_ORIGINAL_SIZE', 'original_size'); define('IMAGE_LINK_HIDDEN', 0); define('IMAGE_LINK_SHOWN', 1); define('IMAGE_LINK_NEW', 2); +define('IMAGE_DEFAULT_DERIVATIVE', 'png'); + /** * Implementation of hook_help */ @@ -57,6 +60,12 @@ function image_theme() { 'attributes' => NULL, ), ), + 'image_original_link' => array( + 'arguments' => array( + 'node' => NULL, + 'image_info' => NULL, + ), + ), ); } @@ -470,7 +479,8 @@ function image_load(&$node) { // original. foreach ($unneeded_sizes as $key) { if (empty($node->images[$key])) { - $node->images[$key] = $original_path; + $image_info = image_get_info($original_path); + $node->images[$key] = image_viewable($image_info['extension']) ? $original_path : $node->images[IMAGE_ORIGINAL_SIZE]; } else { // Need to remove an extra derivative image in the database. @@ -608,6 +618,14 @@ function image_delete($node) { } /** + * Is an image of this type viewable in a browser? If not then we shouldn't + * put it in an img tag. This could be made user-configurable. + */ +function image_viewable($type) { + return in_array($type, array('gif', 'png', 'jpg', 'jpeg')); +} + +/** * Create an tag for an image. */ function image_display(&$node, $label = IMAGE_PREVIEW, $attributes = array()) { @@ -616,11 +634,16 @@ function image_display(&$node, $label = } $image_info = image_get_info(file_create_path($node->images[$label])); - $attributes['class'] = "image image-$label ". (isset($attributes['class']) ? $attributes['class'] : ""); - $attributes['width'] = $image_info['width']; - $attributes['height'] = $image_info['height']; + if (image_viewable($image_info['extension'])) { + $attributes['class'] = "image image-$label ". (isset($attributes['class']) ? $attributes['class'] : ""); + $attributes['width'] = $image_info['width']; + $attributes['height'] = $image_info['height']; - return theme('image_display', $node, $label, file_create_url($node->images[$label]), $attributes); + return theme('image_display', $node, $label, file_create_url($node->images[$label]), $attributes); + } + else { + return theme('image_original_link', $node, $image_info); + } } /** @@ -744,6 +767,14 @@ function _image_check_settings() { } /** + * Theme a link to the original (unviewable) image file. + */ +function theme_image_original_link($node, $image_info) { + $url = file_create_url($node->images[IMAGE_ORIGINAL]); + return l('Download original image (' . basename($node->images[IMAGE_ORIGINAL]) . ')', $url); +} + +/** * Determine which sizes of derivative images need to be built for this image. * * @param $image_path @@ -761,6 +792,11 @@ function image_get_derivative_sizes($ima } $all_sizes = image_get_sizes(NULL, $image_info['height'] / $image_info['width']); + + if (!image_viewable($image_info['extension'])) { + $all_sizes[IMAGE_ORIGINAL_SIZE] = array('width' => $image_info['width'], 'height' => $image_info['height'], 'label' => t('Original size'), 'operation' => 'convert', 'link' => IMAGE_LINK_HIDDEN); + } + foreach ($all_sizes as $key => $size) { // We don't want to include the original. if ($key == IMAGE_ORIGINAL) { @@ -769,7 +805,7 @@ function image_get_derivative_sizes($ima // If the original isn't bigger than the requested size then there's no // need to resize it. - if ($image_info['width'] > $size['width'] || $image_info['height'] > $size['height']) { + if ($image_info['width'] >= $size['width'] || $image_info['height'] >= $size['height']) { $sizes[$key] = $size; } } @@ -809,7 +845,11 @@ function _image_build_derivatives($node, $status = FALSE; switch ($size['operation']) { - // Depending on the operation, the image will be scaled or resized & cropped + // Depending on the operation, the image will be converted, scaled or resized & cropped + case 'convert': + $status = image_convert($original_path, $destination); + break; + case 'scale': $status = image_scale($original_path, $destination, $size['width'], $size['height']); break; @@ -836,26 +876,24 @@ function _image_build_derivatives($node, /** * Creates an image filename. */ -function _image_filename($filename, $label = IMAGE_ORIGINAL, $temp = FALSE) { +function _image_filename($file, $label = IMAGE_ORIGINAL, $temp = FALSE) { $path = variable_get('image_default_path', 'images') .'/'; if ($temp) { $path .= 'temp/'; } - $filename = basename($filename); + $filename = basename($file); // Insert the resized name in non-original images if ($label && ($label != IMAGE_ORIGINAL)) { - $pos = strrpos($filename, '.'); - if ($pos === false) { - // The file had no extension - which happens in really old image.module - // versions, so figure out the extension. - $image_info = image_get_info(file_create_path($path . $filename)); - $filename = $filename .'.'. $label .'.'. $image_info['extension']; - } - else { - $filename = substr($filename, 0, $pos) .'.'. $label . substr($filename, $pos); - } + $image_info = image_get_info($file); + + // we leave the original extension in the filename so that we don't get + // conflicts when we upload images with the same name but different + // extensions. + + $extension = image_viewable($image_info['extension']) ? $image_info['extension'] : IMAGE_DEFAULT_DERIVATIVE; + $filename = $filename .'.'. $label .'.'. $extension; } return file_create_path($path . $filename);