--- imagecache.module 2007-05-08 13:31:42.046875000 +0200 +++ imagecache-patchdownloads.patch 2007-05-08 13:28:02.390625000 +0200 @@ -41,8 +41,14 @@ function imagecache_menu($may_cache) { $items = array(); if ($may_cache) { - $items[] = array( - 'path' => file_directory_path() .'/imagecache', + $items[] = array( // public downloads + 'path' => file_directory_path() .'/imagecache', + 'callback' => 'imagecache_cache', + 'access' => TRUE, + 'type' => MENU_CALLBACK + ); + $items[] = array( // private downloads + 'path' => 'system/files/imagecache', 'callback' => 'imagecache_cache', 'access' => TRUE, 'type' => MENU_CALLBACK @@ -69,15 +75,6 @@ $t = get_t(); if ($phase == 'runtime') { - // Clean URLS. - if (!variable_get('clean_url', 0)) { - $requirements['clean_urls'] = array( - 'title' => $t('Clean URLs'), - 'value' => $t('Not enabled'), - 'severity' => REQUIREMENT_ERROR, - 'description' => $t('Imagecache will not operate properly if Clean URLs is not enabled on your site.', array('!url' => url('admin/settings/clean-urls'))), - ); - } // Check for an image library. if (count(image_get_available_toolkits()) == 0) { $requirements['clean_urls'] = array( @@ -87,16 +84,6 @@ 'description' => $t('Imagecache requires an imagetoolkit such as GD2 or Imagemagick be installed on your server.'), ); } - // Check for public files. - if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) { - $requirements['file_system'] = array( - 'title' => $t('File Download Method'), - 'value' => $t('Private Downloads'), - 'severity' => REQUIREMENT_ERROR, - 'description' => $t('Imagecache will not operate properly using Private Files. Please enable Public File Transfer.', array('!url' => url('admin/settings/file-system'))), - ); - } - // Check for JPEG/PNG/GIF support. if ('gd' == image_get_toolkit()) { foreach (array('gif', 'jpeg', 'png') as $format) { if (!function_exists('imagecreatefrom'. $format)) { @@ -115,97 +102,77 @@ } function imagecache_cache() { - $generated = FALSE; $args = func_get_args(); - $preset = array_shift($args); - $preset_id = _imagecache_preset_load_by_name($preset); - $actions = _imagecache_actions_get_by_presetid($preset_id); + $presetname = array_shift($args); $path = implode('/', $args); - // Verify that the source exists, if not exit. Maybe display a missing image. $source = file_create_path($path); + if (!is_file($source)) { return drupal_not_found(); // @todo: Add a global not found image, and a not found image per preset, // and deliver here if the preset is flagged to provide a not found image. } - $destination = file_create_path() .'/imagecache/'. $preset .'/'. $path; + $destination = trim(file_create_path(), '\\ /'). '/imagecache/'. $presetname .'/'. $path; // append imagecache file to file_create_path() + $generated = is_file($destination); // Check if destination file already exists - // Prepend presetname to tmp file name to prevent namespace clashes when - // multiple presets for the same image are called in rapid succession. - $tmpdestination = file_directory_temp() .'/'. $preset . str_replace(dirname($path) .'/', '', $path); - - $dir = dirname($destination); - - // Build the destination folder tree if it doesn't already exists. - if (!file_check_directory($dir)) { - $folders = explode("/", $dir); - - foreach ($folders as $folder) { - $tpath[] = $folder; - $checkpath = implode("/", $tpath); - if (!file_check_directory($checkpath)) { - $p = implode("/", $tpath); - if (!file_exists($p)) { - mkdir($p); - } + if (!$generated) { // try to generate the modified image + $preset_id = _imagecache_preset_load_by_name($presetname); + $actions = _imagecache_actions_get_by_presetid($preset_id); + $tmpdestination = tempnam(file_directory_temp(), str_replace(array('.', '\\', '//', ), '_', basename($path))); + $dir = dirname($destination); + if (!file_check_directory($dir)) { + _imagecache_mkdir_r($dir); // try to (recursively) create the directory + if (!file_check_directory($dir)) { + watchdog('imagecache', t('Could not create destination: %dir', array('%dir' => $destination)), WATCHDOG_ERROR); + return; } } - } - if (!file_check_directory($dir)) { - watchdog('imagecache', t('Could not create destination: %dir', array('%dir' => $destination)), WATCHDOG_ERROR); - return; - } - - // Check if file exists to prevent multiple apache children from trying to generate. - if (!is_file($tmpdestination)) { - $generated = TRUE; - file_copy($source, $tmpdestination); - - foreach ($actions as $action) { - $size = getimagesize($tmpdestination); - $new_width = _imagecache_filter('width', $action['data']['width'], $size[0], $size[1]); - $new_height = _imagecache_filter('height', $action['data']['height'], $size[0], $size[1]); - foreach ($action['data'] as $key => $value) { - $action['data'][$key] = _imagecache_filter($key, $value, $size[0], $size[1], $new_width, $new_height); - } - switch ($action['data']['function']) { - case 'resize': - if (!image_resize($tmpdestination, $tmpdestination, $action['data']['width'], $action['data']['height'])) { - watchdog('imagecache', t('Imagecache resize action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); - } - break; - case 'scale': - if ($action['data']['fit'] == 'outside' && $action['data']['width'] && $action['data']['height']) { - $ratio = $size[0]/$size[1]; - $new_ratio = $action['data']['width']/$action['data']['height']; - $action['data']['width'] = $ratio > $new_ratio ? 0 : $action['data']['width']; - $action['data']['height'] = $ratio < $new_ratio ? 0 : $action['data']['height']; - } - // Set impossibly large values if the width and height aren't set. - $action['data']['width'] = $action['data']['width'] ? $action['data']['width'] : 9999999; - $action['data']['height'] = $action['data']['height'] ? $action['data']['height'] : 9999999; - if (!image_scale($tmpdestination, $tmpdestination, $action['data']['width'], $action['data']['height'])) { - watchdog('imagecache', t('Imagecache scale action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); - } - break; - case 'crop': - if (!image_crop($tmpdestination, $tmpdestination, $action['data']['xoffset'], $action['data']['yoffset'], $action['data']['width'], $action['data']['height'])) { - watchdog('imagecache', t('Imagecache crop action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); - } - break; + // copy the source file to the temporary destination, and perform actions + if (file_copy($source, $tmpdestination, FILE_EXISTS_REPLACE)) { + + foreach ($actions as $action) { + $size = getimagesize($tmpdestination); + $new_width = _imagecache_filter('width', $action['data']['width'], $size[0], $size[1]); + $new_height = _imagecache_filter('height', $action['data']['height'], $size[0], $size[1]); + foreach ($action['data'] as $key => $value) { + $action['data'][$key] = _imagecache_filter($key, $value, $size[0], $size[1], $new_width, $new_height); + } + switch ($action['data']['function']) { + case 'resize': + if (!image_resize($tmpdestination, $tmpdestination, $action['data']['width'], $action['data']['height'])) { + watchdog('imagecache', t('Imagecache resize action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); + } + break; + case 'scale': + if ($action['data']['fit'] == 'outside' && $action['data']['width'] && $action['data']['height']) { + $ratio = $size[0]/$size[1]; + $new_ratio = $action['data']['width']/$action['data']['height']; + $action['data']['width'] = $ratio > $new_ratio ? 0 : $action['data']['width']; + $action['data']['height'] = $ratio < $new_ratio ? 0 : $action['data']['height']; + } + // Set impossibly large values if the width and height aren't set. + $action['data']['width'] = $action['data']['width'] ? $action['data']['width'] : 9999999; + $action['data']['height'] = $action['data']['height'] ? $action['data']['height'] : 9999999; + if (!image_scale($tmpdestination, $tmpdestination, $action['data']['width'], $action['data']['height'])) { + watchdog('imagecache', t('Imagecache scale action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); + } + break; + case 'crop': + if (!image_crop($tmpdestination, $tmpdestination, $action['data']['xoffset'], $action['data']['yoffset'], $action['data']['width'], $action['data']['height'])) { + watchdog('imagecache', t('Imagecache crop action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR); + } + break; + } } + $generated = file_move($tmpdestination, $destination, FILE_EXISTS_REPLACE); } - file_move($tmpdestination, $destination); - } - else { - $generated = TRUE; } - if ($generated) { + if ($generated) { if (function_exists('mime_content_type')) { $mime = mime_content_type($destination); } @@ -217,10 +184,24 @@ } else { // Generate an error if image could not generate. - watchdog('imagecache', t('There were problems generating an image from %image using imagecache preset %preset.', array('%image' => $path, '%preset' => $preset['presetname'])), WATCHDOG_ERROR); + watchdog('imagecache', t('There were problems generating an image from %image using imagecache preset %preset.', array('%image' => $path, '%preset' => $presetname)), WATCHDOG_ERROR); + } +} + +/* recursively create directory */ +function _imagecache_mkdir_r($dirname, $rights = 0777) { + $dirs = explode('/', $dirname); + $dir = ''; + foreach ($dirs as $part) { + $dir.= $part. '/'; + if (!is_dir($dir) && strlen($dir) > 0) { + mkdir($dir, $rights); + chmod($dir, $rights); + } } } + /** * Implementation of hook_field_formatter_info(). */ @@ -364,7 +345,7 @@ } function imagecache_admin() { - drupal_set_title('Imagecache administration'); + drupal_set_title('Image cache administration'); $form = array(); $form['title'] = array('#type' => 'markup', '#value' => t('Imagecache presets')); $form['presets']['#tree'] = TRUE; @@ -434,7 +415,7 @@ foreach ($_POST['preset-op'] as $presetid => $op) { // Check for illegal characters in preset names if (preg_match('/[^0-9a-zA-Z_\-]/', $form_values['presets'][$presetid]['name'])) { - form_set_error('presets]['. $presetid .'][name', t('Please only use alphanumic characters, underscores (_), and hyphens (-) for preset names.')); + form_set_error('presets]['. $presetid .'][name', t('Please only use alphanumeric characters, underscores (_), and hyphens (-) for preset names.')); } } } @@ -550,8 +531,8 @@ function _imagecache_preset_flush($id) { if (user_access('flush imagecache')) { drupal_set_message(t('Flushed Preset Images (ID: @id)', array('@id' => $id))); - $preset = _imagecache_preset_load($id); - $presetdir = realpath(file_directory_path() .'/imagecache/'. $preset); + $presetname = _imagecache_preset_load($id); + $presetdir = file_create_path('imagecache/'. $presetname); if (is_dir($presetdir)) { _imagecache_recursive_delete($presetdir); } @@ -581,13 +562,12 @@ } function _imagecache_action_create($action) { - //debug_msg($action, 'action@create: '); $next_id = db_next_id('{imagecache_action}_actionid'); db_query('INSERT INTO {imagecache_action} (actionid, presetid, weight, data) VALUES (%d, %d, %d, \'%s\')', $next_id, $action['presetid'], $action['weight'], serialize($action['data'])); } function _imagecache_action_update($action) { - //debug_msg($action, 'action@update'); + _imagecache_preset_flush($action['presetid']); db_query('UPDATE {imagecache_action} SET weight = %d, data = \'%s\' WHERE actionid = %d', $action['weight'], serialize($action['data']), $action['actionid']); } @@ -695,31 +675,18 @@ return $form; } -/** - * Theme an img tag for displaying the image. - */ + +//Theme an img tag for displaying the image. function theme_imagecache_display($node, $label, $url, $attributes) { return ''. check_plain($node->title) .''; } -/** - * Verify the image module and toolkit settings. - */ -function _imagecache_check_settings() { - // Sanity check : make sure we've got a working toolkit. - if (!image_get_toolkit()) { - drupal_set_message(t('Make sure you have a working image toolkit installed and enabled, for more information see: %settings.', array('%settings' => l(t('Image toolkit settings'), 'admin/settings/image-toolkit'))), 'error'); - return FALSE; - } - return TRUE; -} - -function theme_imagecache_admin($form) { - $output = ''; - $output .= '

$Id: imagecache.module,v 1.19.2.27 2007/04/19 22:58:44 quicksketch Exp $

'; - $output .= '

'. drupal_render($form['title']) .'

'; - $output .= drupal_render($form); - return $output; +// Get location of imagecache file +function theme_imagecache_location($presetname, $path) { + // find where the file is relative to the file_create_path() directory, and remove trailing slashes: + $relpath = trim(substr(file_create_path($path), strlen(file_create_path())), '\\ /'); + $imagecache_path = file_create_path('imagecache/'. $presetname. '/'. $relpath, NULL, NULL, TRUE); + return($imagecache_path); } function theme_imagecache_formatter($field, $item, $formatter) { @@ -734,21 +701,35 @@ return $output; } -function theme_imagecache($namespace, $path, $alt = '', $title = '', $attributes = NULL) { + +function theme_imagecache($presetname, $path, $alt = '', $title = '', $attributes = NULL) { + // find where the file is relative to the file_create_path() directory, and remove trailing slashes: + $relpath = trim(substr(file_create_path($path), strlen(file_create_path())), '\\ /'); + if ( (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) && + (!variable_get('clean_url', '0')) && + (!is_file(file_create_path('imagecache/'. $presetname.'/'. $relpath))) + ) { // create link to file with query string + $imagecache_path = url(file_directory_path(). '/imagecache/'. $presetname .'/'. $relpath); + } + else { // create link to file with file_create_url + $imagecache_path = file_create_url(file_directory_path() .'/imagecache/'. $presetname .'/'. $relpath); + } $attributes = drupal_attributes($attributes); - $imagecache_path = file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. $path); return ''. check_plain($alt) .''; } + /** * Clear cached versions of a specific file in all presets. * @param $path * The Drupal file path to the original image. */ function imagecache_image_flush($path) { + // find where the file is relative to the file_create_path() directory, and remove trailing slashes: + $relpath = trim(substr(file_create_path($path), strlen(file_create_path())), '\\ /'); $presets = _imagecache_get_presets(); foreach ($presets as $presetid => $presetname) { - $ipath = file_directory_path() .'/imagecache/'. $presetname .'/'. $path; + $ipath = file_create_path('imagecache/'. $presetname.'/'. $relpath); file_delete($ipath); } -} +} \ No newline at end of file