Index: image_resize_filter.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/image_resize_filter/image_resize_filter.module,v retrieving revision 1.37 diff -u -r1.37 image_resize_filter.module --- image_resize_filter.module 6 Jan 2010 22:38:00 -0000 1.37 +++ image_resize_filter.module 28 Jan 2010 13:01:04 -0000 @@ -323,56 +323,62 @@ } } $local_path = urldecode($local_path); - } - // If this is an ImageCache preset, generate the source image if necessary. - if ($location == 'local' && !file_exists($local_path) && strpos($local_path, 'imagecache') !== FALSE && function_exists('imagecache_build_derivative')) { - $imagecache_path = preg_replace('/^'. preg_quote(file_directory_path(), '/') .'\/imagecache\//', '', $local_path); - $imagecache_args = explode('/', $imagecache_path); - $preset_name = array_shift($imagecache_args); - $original_path = implode('/', $imagecache_args); - // ImageCache can resize images both inside and outside of the files - // directory. If the image doesn't exist at the given path, check inside - // the files directory. - if (!file_exists($original_path)) { - $original_path = file_directory_path() . '/' . $original_path; - } - if (file_exists($original_path) && $preset = imagecache_preset_by_name($preset_name)) { - imagecache_build_derivative($preset['actions'], $original_path, imagecache_create_path($preset_name, $original_path)); + // If this is an ImageCache preset, generate the source image if necessary. + if (!file_exists($local_path) && strpos($local_path, 'imagecache') !== FALSE && function_exists('imagecache_build_derivative')) { + $imagecache_path = preg_replace('/^'. preg_quote(file_directory_path(), '/') .'\/imagecache\//', '', $local_path); + $imagecache_args = explode('/', $imagecache_path); + $preset_name = array_shift($imagecache_args); + $original_path = implode('/', $imagecache_args); + // ImageCache can resize images both inside and outside of the files + // directory. If the image doesn't exist at the given path, check inside + // the files directory. + if (!file_exists($original_path)) { + $original_path = file_directory_path() . '/' . $original_path; + } + if (file_exists($original_path) && $preset = imagecache_preset_by_name($preset_name)) { + imagecache_build_derivative($preset['actions'], $original_path, imagecache_create_path($preset_name, $original_path)); + } } - } - // Get the image size. - if ($location == 'local') { $image_size = @getimagesize($local_path); - } - else { - $image_size = @getimagesize($src); - } - - // All this work and the image isn't even there. Bummer. Next image please. - if ($image_size == FALSE) { - continue; - } - $actual_width = $image_size[0]; - $actual_height = $image_size[1]; - - // If either height or width is missing, calculate the other. - if (!$height) { - $ratio = $actual_height/$actual_width; - $height = round($ratio * $width); - } - elseif (!$width) { - $ratio = $actual_width/$actual_height; - $width = round($ratio * $height); + // All this work and the image isn't even there. Bummer. Next image please. + if ($image_size == FALSE) { + continue; + } + + $actual_width = $image_size[0]; + $actual_height = $image_size[1]; + + // If either height or width is missing, calculate the other. + if (!$height && $width) { + $ratio = $actual_height/$actual_width; + $height = round($ratio * $width); + } + elseif ($height && !$width) { + $ratio = $actual_width/$actual_height; + $width = round($ratio * $height); + } + elseif (!$height && !$width) { + $height = $actual_height; + $width = $actual_width; + } + + // Finally, if width and heights match up, don't do anything. + if ($actual_width == $width && $actual_height == $height) { + continue; + } } - - // Finally, if width and heights match up, don't do anything. - if ($actual_width == $width && $actual_height == $height) { - continue; + + // Get the image size. + if ($location != 'local') { + //$image_size = @getimagesize($src); + // We don't really care about the actual size for remote images, as we always cache them anyway. + $actual_width = null; + $actual_height = null; } - + // Check the image extension by name. $extension_matches = array(); preg_match('/\.([a-zA-Z0-9]+)$/', $src, $extension_matches); @@ -381,18 +387,23 @@ } // If the name extension failed (such as an image generated by a script), // See if we can determine an extension by MIME type. - elseif (isset($image_size['mime'])) { - switch ($image_size['mime']) { - case 'image/png': - $extension = 'png'; - break; - case 'image/gif': - $extension = 'gif'; - break; - case 'image/jpeg': - case 'image/pjpeg': - $extension = 'jpg'; - break; + else { + if ($location != 'local') { + $image_size = @getimagesize($src); + } + if (isset($image_size['mime'])) { + switch ($image_size['mime']) { + case 'image/png': + $extension = 'png'; + break; + case 'image/gif': + $extension = 'gif'; + break; + case 'image/jpeg': + case 'image/pjpeg': + $extension = 'jpg'; + break; + } } } @@ -405,7 +416,7 @@ // Add all information to a list of images that need resizing. $images[] = array( 'expected_size' => array('width' => $width, 'height' => $height), - 'actual_size' => array('width' => $image_size[0], 'height' => $image_size[1]), + //'actual_size' => array('width' => $actual_width, 'height' => $actual_height), // NOT used 'add_properties' => array('width' => $needs_width, 'height' => $needs_height), 'title' => $title, 'has_link' => $has_link, @@ -435,22 +446,14 @@ function image_resize_filter_process($images, $text, $settings) { $file_directory_path = file_directory_path(); $local_file_path = ''; + foreach ($images as $image) { - // Copy remote images locally. + + // Determin the destination file path. if ($image['location'] == 'remote') { - $result = drupal_http_request($image['original']); - if ($result->code == 200) { - $tmp_file = tempnam(file_directory_temp(), 'image_resize_filter_'); - $handle = fopen($tmp_file, 'w'); - fwrite($handle, $result->data); - fclose($handle); - $local_file_path = 'resize/remote/'. md5($result->data) .'-'. $image['expected_size']['width'] .'x'. $image['expected_size']['height'] .'.'. $image['extension']; - $image['local_path'] = $tmp_file; - $image['destination'] = $file_directory_path .'/'. $local_file_path; - } - else { - continue; - } + $local_file_path = 'resize/remote/'. md5($image['original']) .'-'. $image['expected_size']['width'] .'x'. $image['expected_size']['height'] .'.'. $image['extension']; + $image['destination'] = $file_directory_path .'/'. $local_file_path; + } else { $path_info = image_resize_filter_pathinfo($image['local_path']); @@ -460,20 +463,36 @@ $image['destination'] = $file_directory_path .'/'. $local_file_path; } - // Ensure that the destination directories exist. - $folders = explode('/', dirname($local_file_path)); - $current_directory = $file_directory_path .'/'; - foreach ($folders as $folder) { - $current_directory .= $folder .'/'; - $check_directory = $current_directory; - // Use the "quiet" version of file_check_directory() provided by FileField - // if it exists. This suppresses "Directory was created" messages. - $file_check_directory = function_exists('field_file_check_directory') ? 'field_file_check_directory' : 'file_check_directory'; - $file_check_directory($check_directory, FILE_CREATE_DIRECTORY); - } - - // Resize the local image. if (!file_exists($image['destination'])) { + + // Ensure that the destination directories exist. + $folders = explode('/', dirname($local_file_path)); + $current_directory = $file_directory_path .'/'; + foreach ($folders as $folder) { + $current_directory .= $folder .'/'; + $check_directory = $current_directory; + // Use the "quiet" version of file_check_directory() provided by FileField + // if it exists. This suppresses "Directory was created" messages. + $file_check_directory = function_exists('field_file_check_directory') ? 'field_file_check_directory' : 'file_check_directory'; + $file_check_directory($check_directory, FILE_CREATE_DIRECTORY); + } + + // Fetch remote image, to prepare for resizing. + if ($image['location'] == 'remote') { + $result = drupal_http_request($image['original']); + if ($result->code == 200) { + $tmp_file = tempnam(file_directory_temp(), 'image_resize_filter_'); + $handle = fopen($tmp_file, 'w'); + fwrite($handle, $result->data); + fclose($handle); + $image['local_path'] = $tmp_file; + } + else { + continue; + } + } + + // Resize if (module_exists('imageapi') && imageapi_default_toolkit()) { $res = imageapi_image_open($image['local_path']); imageapi_image_resize($res, $image['expected_size']['width'], $image['expected_size']['height']);