--- imagecache.module 2007-04-20 10:10:28.000000000 +0200 +++ imagecache-nopatch.module 2007-04-27 22:08:16.000000000 +0200 @@ -1,5 +1,6 @@ /path @@ -27,30 +28,35 @@ * */ - +/** + * Implementation of hook_perm(). + */ function imagecache_perm() { - return array('administer imagecache','flush imagecache'); + return array('administer imagecache', 'flush imagecache'); } - +/** + * Implementation of hook_menu(). + */ function imagecache_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array( - 'path' => file_directory_path() .'/imagecache', + 'path' => 'imagecache', 'callback' => 'imagecache_cache', 'access' => TRUE, 'type' => MENU_CALLBACK ); $items[] = array( - 'path' => 'admin/settings/imagecache', + 'path' => 'admin/settings/imagecache', 'title' => t('Image cache'), 'description' => t('Configure rulesets and actions for imagecache.'), 'access' => user_access('administer imagecache'), 'callback' => 'drupal_get_form', 'callback arguments' => array('imagecache_admin'), - ); + ); } + return $items; } @@ -63,15 +69,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( @@ -81,16 +78,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)) { @@ -109,98 +96,76 @@ } 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)) { + + 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. + // @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 = trim(file_create_path(), '\\ /'). '/imagecache/'. $presetname .'/'. $path; // append imagecache file to file_create_path() + $generated = is_file($destination); // Check if destination file already exists - $destination = file_create_path() .'/imagecache/'. $preset .'/'. $path; - - // 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 folder for imagecache. - 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); } @@ -212,10 +177,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(). */ @@ -359,9 +338,7 @@ } function imagecache_admin() { - //drupal_set_message('
'. print_r($_POST, TRUE) .''); - - 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; @@ -431,7 +408,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.')); } } } @@ -547,8 +524,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); } @@ -567,10 +544,10 @@ function _imagecache_recursive_delete($path) { $listing = $path .'/*'; foreach (glob($listing) as $file) { - if (is_file($file) === true) { + if (is_file($file) === TRUE) { @unlink($file); } - elseif (is_dir($file) === true) { + elseif (is_dir($file) === TRUE) { _imagecache_recursive_delete($file); } } @@ -578,13 +555,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']); } @@ -602,7 +578,6 @@ '#type' => 'fieldset', '#title' => t($action['data']['function']), ); - $form[$actionid]['data']['function'] = array( '#type' => 'hidden', '#value' => $action['data']['function'], @@ -693,32 +668,12 @@ 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 '
$Id: imagecache.module,v 1.19.2.15.2.10 2007/04/19 23:03:55 quicksketch Exp $
'; - $output .= '