/path/to/file.ext.newext * * where newext is the extension of the file after conversion. * * The module also overides the formatters for CCK Imagefields * provided by ImageCache module so that for presets which contain the * convert action, links to the above URL are made. * */ /********************************************************************************************* * Drupal Hooks *********************************************************************************************/ /** * Implementation of hook_menu(). */ function imagecache_rename_menu() { $items = array(); // standard imagecache callback. $items[file_directory_path() .'/imagecache_rename'] = array( 'page callback' => 'imagecache_rename_cache', 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); // private downloads imagecache callback // $items['system/files/imagecache_rename'] = array( // 'page callback' => 'imagecache_rename_cache_private', // 'access callback' => TRUE, // 'type' => MENU_CALLBACK, // ); return $items; } /** * Implementation of hook_theme(). */ function imagecache_rename_theme() { $theme = array( 'imagecache_rename' => array( 'arguments' => array( 'namespace' => NULL, 'path' => NULL, 'alt' => NULL, 'title' => NULL, )), 'imagecache_rename_imagelink' => array( 'arguments' => array( 'namespace' => NULL, 'path' => NULL, 'alt' => NULL, 'title' => NULL, 'attributes' => array(), )), ); foreach (imagecache_presets() as $preset) { $theme['imagecache_rename_formatter_'. $preset['presetname'] .'_default'] = array( 'arguments' => array('element' => NULL), 'function' => 'theme_imagecache_rename_formatter_default', ); $theme['imagecache_rename_formatter_'. $preset['presetname'] .'_linked'] = array( 'arguments' => array('element' => NULL), 'function' => 'theme_imagecache_rename_formatter_linked', ); $theme['imagecache_rename_formatter_'. $preset['presetname'] .'_imagelink'] = array( 'arguments' => array('element' => NULL), 'function' => 'theme_imagecache_rename_formatter_imagelink', ); // $theme['imagecache_rename_formatter_'. $preset['presetname'] .'_path'] = array( // 'arguments' => array('element' => NULL), // 'function' => 'theme_imagecache_rename_formatter_path', // ); $theme['imagecache_rename_formatter_'. $preset['presetname'] .'_url'] = array( 'arguments' => array('element' => NULL), 'function' => 'theme_imagecache_rename_formatter_url', ); } return $theme; } function imagecache_rename_get_extension($preset) { $format = ''; $extension = ''; foreach ($preset['actions'] as $action) { if (($action['action'] == 'imagecache_convert') && isset($action['data']['format'])) { $format = $action['data']['format']; } } if (isset($format)) { $formats = imagecache_file_formats(); $extension = $formats[$format]; } return $extension; } /** * Return a URL that points to the location of a derivative of the * original image transformed with the given preset. * * Special care is taken to make this work with the possible combinations of * Clean URLs and public/private downloads. For example, when Clean URLs are not * available an URL with query should be returned, like * http://example.com/?q=files/imagecache/foo.jpg, so that imagecache is able * intercept the request for this file. * * This code is very similar to the Drupal core function file_create_url(), but * handles the case of Clean URLs and public downloads differently however. * * @param $presetname * @param $filepath * String specifying the path to the image file. * @param $bypass_browser_cache * A Boolean indicating that the URL for the image should be distinct so that * the visitors browser will not be able to use a previously cached version. * This is */ function imagecache_rename_create_url($presetname, $filepath, $bypass_browser_cache = FALSE) { // Find out if this preset has conversion step // If no, just use the imagecache dir // If yes, find out what the target is, and append this to the imagecache // path and replace "imagecache" with "imagecache_rename". $format = imagecache_rename_get_extension(imagecache_preset_by_name($presetname)); if (isset($format)) { $formats = imagecache_file_formats(); $filepath .= '.' . $formats[$format]; $path = _imagecache_strip_file_directory($filepath); if (module_exists('transliteration')) { $path = transliteration_get($path); } $args = array('absolute' => TRUE, 'query' => $bypass_browser_cache ? time() : $bypass_browser_cache); switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) { case FILE_DOWNLOADS_PUBLIC: return url($GLOBALS['base_url'] . '/' . file_directory_path() .'/imagecache_rename/'. $presetname .'/'. $path, $args); case FILE_DOWNLOADS_PRIVATE: return url('system/files/imagecache_rename/'. $presetname .'/'. $path, $args); } } else { return imagecache_create_url($presetname, $filepath, $bypass_browser_cache); } } /** * callback for handling public files imagecache requests. */ function imagecache_rename_cache() { $args = func_get_args(); // Strip the last extension off the $filename, and then // retrieve from imagecache as usual $filename = array_pop($args); $filename_parts = pathinfo($filename); array_push($args, $filename_parts['filename']); call_user_func_array('imagecache_cache', $args); } /** * callback for handling private files imagecache requests */ // function imagecache_rename_cache_private() { // $args = func_get_args(); // $preset = check_plain(array_shift($args)); // $source = implode('/', $args); // if (user_access('view imagecache '. $preset)) { // _imagecache_cache($preset, $source); // } // else { // // if there is a 403 image, display it. // $accesspath = file_create_path('imagecache/'. $preset .'.403.png'); // if (is_file($accesspath)) { // imagecache_rename_transfer($accesspath); // exit; // } // header('HTTP/1.0 403 Forbidden'); // exit; // } // } /** * Implementation of hook_field_formatter_info(). * * imagecache formatters are named as $presetname_$style * $style is used to determine how the preset should be rendered. * If you are implementing custom imagecache formatters please treat _ as * reserved. * * @todo: move the linking functionality up to imagefield and clean up the default image * integration. */ function imagecache_rename_field_formatter_info() { $formatters = array(); foreach (imagecache_presets() as $preset) { // Does the preset change the format? $extension = imagecache_rename_get_extension($preset); if (isset($extension)) { $formatters[$preset['presetname'] .'_default'] = array( 'label' => t('@preset image', array('@preset' => $preset['presetname'])), 'field types' => array('image', 'filefield'), ); $formatters[$preset['presetname'] .'_linked'] = array( 'label' => t('@preset image linked to node', array('@preset' => $preset['presetname'])), 'field types' => array('image', 'filefield'), ); $formatters[$preset['presetname'] .'_imagelink'] = array( 'label' => t('@preset image linked to image', array('@preset' => $preset['presetname'])), 'field types' => array('image', 'filefield'), ); // $formatters[$preset['presetname'] .'_path'] = array( // 'label' => t('@preset file path', array('@preset' => $preset['presetname'])), // 'field types' => array('image', 'filefield'), // ); $formatters[$preset['presetname'] .'_url'] = array( 'label' => t('@preset URL', array('@preset' => $preset['presetname'])), 'field types' => array('image', 'filefield'), ); } } return $formatters; } function theme_imagecache_rename_formatter_default($element) { // Inside a view $element may contain NULL data. In that case, just return. if (empty($element['#item']['fid'])) { return ''; } // Extract the preset name from the formatter name. $presetname = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_')); $style = 'linked'; $style = 'default'; $item = $element['#item']; $item['data']['alt'] = isset($item['data']['alt']) ? $item['data']['alt'] : ''; $item['data']['title'] = isset($item['data']['title']) ? $item['data']['title'] : NULL; $class = "imagecache imagecache-$presetname imagecache-$style imagecache-{$element['#formatter']}"; return theme('imagecache_rename', $presetname, $item['filepath'], $item['data']['alt'], $item['data']['title'], array('class' => $class)); } function theme_imagecache_rename_formatter_linked($element) { // Inside a view $element may contain NULL data. In that case, just return. if (empty($element['#item']['fid'])) { return ''; } // Extract the preset name from the formatter name. $presetname = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_')); $style = 'linked'; $item = $element['#item']; $item['data']['alt'] = isset($item['data']['alt']) ? $item['data']['alt'] : ''; $item['data']['title'] = isset($item['data']['title']) ? $item['data']['title'] : NULL; $imagetag = theme('imagecache_rename', $presetname, $item['filepath'], $item['data']['alt'], $item['data']['title']); $path = empty($item['nid']) ? '' : 'node/'. $item['nid']; $class = "imagecache imagecache-$presetname imagecache-$style imagecache-{$element['#formatter']}"; return l($imagetag, $path, array('attributes' => array('class' => $class), 'html' => TRUE)); } function theme_imagecache_rename_formatter_imagelink($element) { // Inside a view $element may contain NULL data. In that case, just return. if (empty($element['#item']['fid'])) { return ''; } // Extract the preset name from the formatter name. $presetname = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_')); $style = 'imagelink'; $item = $element['#item']; $item['data']['alt'] = isset($item['data']['alt']) ? $item['data']['alt'] : ''; $item['data']['title'] = isset($item['data']['title']) ? $item['data']['title'] : NULL; $imagetag = theme('imagecache_rename', $presetname, $item['filepath'], $item['data']['alt'], $item['data']['title']); $path = file_create_url($item['filepath']); $class = "imagecache imagecache-$presetname imagecache-$style imagecache-{$element['#formatter']}"; return l($imagetag, $path, array('attributes' => array('class' => $class), 'html' => TRUE)); } // function theme_imagecache_rename_formatter_path($element) { // // Inside a view $element may contain NULL data. In that case, just return. // if (empty($element['#item']['fid'])) { // return ''; // } // // Extract the preset name from the formatter name. // $presetname = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_')); // return imagecache_rename_create_path($presetname, $element['#item']['filepath']); // } function theme_imagecache_rename_formatter_url($element) { // Inside a view $element may contain NULL data. In that case, just return. if (empty($element['#item']['fid'])) { return ''; } // Extract the preset name from the formatter name. $presetname = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_')); return imagecache_rename_create_url($presetname, $element['#item']['filepath']); } /** * Create and image tag for an imagecache derivative * * @param $presetname * String with the name of the preset used to generate the derivative image. * @param $path * String path to the original image you wish to create a derivative image * tag for. * @param $alt * Optional string with alternate text for the img element. * @param $title * Optional string with title for the img element. * @param $attributes * Optional drupal_attributes() array. If $attributes is an array then the * default imagecache classes will not be set automatically, you must do this * manually. * @param $getsize * If set to TRUE, the image's dimension are fetched and added as width/height * attributes. * @return * HTML img element string. */ function theme_imagecache_rename($presetname, $path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) { // Check is_null() so people can intentionally pass an empty array of // to override the defaults completely. if (is_null($attributes)) { $attributes = array('class' => 'imagecache imagecache-'. $presetname); } if ($getsize && ($image = imageapi_get_info(imagecache_create_path($presetname, $path)))) { $attributes['width'] = $image['width']; $attributes['height'] = $image['height']; } $attributes = drupal_attributes($attributes); $imagecache_rename_url = imagecache_rename_create_url($presetname, $path); return ''. check_plain($alt) .''; } /** * Create a link the original image that wraps the derivative image. * * @param $presetname * String with the name of the preset used to generate the derivative image. * @param $path * String path to the original image you wish to create a derivative image * tag for. * @param $alt * Optional string with alternate text for the img element. * @param $title * Optional string with title for the img element. * @param attributes * Optional drupal_attributes() array for the link. * @return * An HTML string. */ function theme_imagecache_rename_imagelink($presetname, $path, $alt = '', $title = '', $attributes = NULL) { $image = theme('imagecache_rename', $presetname, $path, $alt, $title); $original_image_url = file_create_url($path); return l($image, $original_image_url, array('absolute' => FALSE, 'html' => TRUE)); }