--- imagecache.module 2008-07-22 11:44:54.000000000 +0100 +++ imagecache.module 2008-10-24 11:25:29.000000000 +0100 @@ -39,7 +39,7 @@ * Implementation of hook_perm(). */ function imagecache_perm() { - $perms = array('administer imagecache', 'flush imagecache'); + $perms = array('administer imagecache', 'flush imagecache', 'cache external images'); foreach (imagecache_presets() as $preset) { $perms[] = 'view imagecache '. $preset['presetname']; } @@ -197,6 +197,9 @@ function imagecache_action_definition($a */ function imagecache_create_url($presetname, $path) { $path = _imagecache_strip_file_directory($path); + if (stripos($path, 'http://') == 0){ + $path = str_ireplace('http://', 'http/', $path); + } switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) { case FILE_DOWNLOADS_PUBLIC: return url(file_directory_path() .'/imagecache/'. $presetname .'/'. $path, NULL, NULL, TRUE); @@ -270,6 +273,18 @@ function _imagecache_cache($presetname, header('HTTP/1.0 404 Not Found'); exit; } + + // handle external http files + if (user_access('cache external images')) { + $args = split('/', $path, 2); + $http = ($args[0]=='http'); + if($http){ + $dst = file_create_path() .'/imagecache/'. $preset['presetname'] .'/http/' . urlencode($args[1]); + if (file_exists($dst)) { + imagecache_transfer($dst); + } + } + } // umm yeah deliver it early if it is there. especially useful // to prevent lock files from being created when delivering private files. @@ -310,6 +325,34 @@ function _imagecache_cache($presetname, } } + // handle external http files + if($http){ + $url = 'http://'. $args[1]; + $src = file_create_path() . '/imagecache/http-originals/' . urlencode($args[1]); + if (!file_exists($src)) { + $dir = file_create_path() .'/imagecache/http-originals'; + if(!file_check_directory($dir)){ + mkdir($dir, 0775, true); + } + $result = drupal_http_request($url); + $code = floor($result->code / 100) * 100; + $types = array('image/jpeg', 'image/png', 'image/gif'); + + if($result->data && $code != 400 && $code != 500 && in_array($result->Content-Type, $types)){ + $src = file_save_data($result->data, $src); + } + else + { + //to prevent calling this external site repeatedly place blank file?? + $src = file_save_data('', $src); + } + } + $dst = file_create_path() .'/imagecache/'. $preset['presetname'] .'/http/' . urlencode($args[1]); + if(imagecache_build_derivative($preset['actions'], $src, $dst)){ + imagecache_transfer($dst); + } + } + // if there is a 404 image uploaded for the preset display it. $notfoundpath = file_create_path('imagecache/'. $preset['presetname'] .'.404.png'); if (file_exists($notfoundpath)) {