--- imagecache.module +++ imagecache.module @@ -46,10 +46,10 @@ */ define('IMAGECACHE_STORAGE_OVERRIDE', 2); +/** + * Define maximum number of subdomain (if enabled) to split image. + */ +define('IMAGECACHE_MAX_SUBDOMAIN', 3); /********************************************************************************************* * Drupal Hooks @@ -323,7 +323,7 @@ $args = array('absolute' => TRUE, 'query' => empty($bypass_browser_cache) ? NULL : time()); switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) { case FILE_DOWNLOADS_PUBLIC: - return url($GLOBALS['base_url'] . '/' . file_directory_path() .'/imagecache/'. $presetname .'/'. $path, $args); + return url(_imagecache_get_subdomain($filepath) . '/' . file_directory_path() .'/imagecache/'. $presetname .'/'. $path, $args); case FILE_DOWNLOADS_PRIVATE: return url('system/files/imagecache/'. $presetname .'/'. $path, $args); } @@ -1109,3 +1109,43 @@ imagecache_presets(TRUE); } +/** Return domain to add to image path. + * + * Generate domain to add to image to generate complete URL. + * This function use first letter of name to redirect the same image into + * the same domain everytime. + * + * @param $file_name + * Path and name of image. + * + * @return + * Domain do add into image absolute path. + */ +function _imagecache_get_subdomain($file_name = NULL) { + // Return empty if subdomain is not enabled + if(!variable_get('imagecache_subdomain_enable_global', FALSE) || is_null($file_name)) { + return $GLOBALS['base_url']; + } + + // Extract config + $config = variable_get('imagecache_subdomain_config', array()); + + // Count number of subdomain + $num_subdomain = count($config); + + // Extract first char of filename + // TODO: use regexp to extract only alphanumeric char + $first_char = substr(basename($file_name), 0, 1); + + // Define list of possible first char value and generate array + // TODO: it's possible improve performance using a defined array (no generation to runtime) + $char_list = 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789-_.+'; + $char_key = array_flip(str_split($char_list)); + + // Find subdomain to use + $subdomain = floor(($num_subdomain + 1) * $char_key[$first_char] / strlen($char_list)); + + // Return correct subdomain + return $config[$subdomain]['uri']; +} +