Index: modules/image/image.module =================================================================== RCS file: /cvs/drupal/drupal/modules/image/image.module,v retrieving revision 1.44 diff -u -p -r1.44 image.module --- modules/image/image.module 30 Jul 2010 01:37:54 -0000 1.44 +++ modules/image/image.module 2 Aug 2010 03:54:41 -0000 @@ -71,10 +71,10 @@ function image_help($path, $arg) { function image_menu() { $items = array(); - $items['image/generate/%image_style'] = array( + $items[file_directory_path() . '/styles/%image_style'] = array( 'title' => 'Generate image style', 'page callback' => 'image_style_generate', - 'page arguments' => array(2), + 'page arguments' => array(count(explode('/', file_directory_path())) + 1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); @@ -632,19 +632,16 @@ function image_style_generate() { $path = implode('/', $args); $path = $scheme . '://' . $path; - $path_hash = drupal_hash_base64($path); $destination = image_style_path($style['name'], $path); - // Check that it's a defined style and that access was granted by - // image_style_url(). - if (!$style || !cache_get('access:' . $style_name . ':' . $path_hash, 'cache_image')) { - drupal_access_denied(); + // Check that it's a defined style. + if (!$style) { drupal_exit(); } // Don't start generating the image if the derivate already exists or if // generation is in progress in another thread. - $lock_name = 'image_style_generate:' . $style_name . ':' . $path_hash; + $lock_name = 'image_style_generate:' . $style_name . ':' . drupal_hash_base64($path); if (!file_exists($destination)) { $lock_acquired = lock_acquire($lock_name); if (!$lock_acquired) { @@ -753,13 +750,6 @@ function image_style_flush($style) { /** * Return the URL for an image derivative given a style and image path. * - * This function is the default image generation method. It returns a URL for - * an image that can be used in an tag. When the browser requests the - * image at image/generate/[style_name]/[scheme]/[path] the image is generated - * if it does not already exist and then served to the browser. This allows - * each image to have its own PHP instance (and memory limit) for generation of - * the new image. - * * @param $style_name * The name of the style to be used with this image. * @param $path @@ -771,26 +761,13 @@ function image_style_flush($style) { */ function image_style_url($style_name, $path) { $destination = image_style_path($style_name, $path); - - // If the image already exists use that rather than regenerating it. - if (file_exists($destination)) { - return file_create_url($destination); + $url = file_directory_path() . '/' . file_uri_target($destination); + // If clean urls are disabled, then we need to prepend '?q=' to the image url + // so that the file will be served by php. + if (!variable_get('clean_url', FALSE)) { + $url = '?q=' . $url; } - - // Disable page cache for this request. This prevents anonymous users from - // needlessly hitting the image generation URL when the image already exists. - drupal_page_is_cacheable(FALSE); - - // Set a cache entry to grant access to this style/image path. This will be - // checked by image_style_generate(). - cache_set('access:' . $style_name . ':' . drupal_hash_base64($path), 1, 'cache_image', REQUEST_TIME + 600); - - $scheme = file_uri_scheme($path); - $target = file_uri_target($path); - - // Generate a callback path for the image. - $url = url('image/generate/' . $style_name . '/' . $scheme . '/' . $target, array('absolute' => TRUE)); - return $url; + return base_path() . $url; } /** @@ -816,7 +793,7 @@ function image_style_path($style_name, $ $path = $uri; $scheme = variable_get('file_default_scheme', 'public'); } - return $scheme . '://styles/' . $style_name . '/' . $path; + return $scheme . '://styles/' . $style_name . '/' . $scheme . '/' . $path; } /** @@ -1079,7 +1056,7 @@ function theme_image_style($variables) { if (!file_exists($style_path)) { $style_path = image_style_url($style_name, $path); } - $variables['path'] = file_create_url($style_path); + $variables['path'] = $style_path; $variables['getsize'] = FALSE; return theme('image', $variables); } Index: modules/image/image.test =================================================================== RCS file: /cvs/drupal/drupal/modules/image/image.test,v retrieving revision 1.23 diff -u -p -r1.23 image.test --- modules/image/image.test 30 Jun 2010 22:37:49 -0000 1.23 +++ modules/image/image.test 2 Aug 2010 03:54:41 -0000 @@ -131,12 +131,13 @@ class ImageStylesPathAndUrlUnitTest exte * Test image_style_path(). */ function testImageStylePath() { - $actual = image_style_path($this->style_name, 'public://foo/bar.gif'); - $expected = 'public://styles/' . $this->style_name . '/foo/bar.gif'; + $scheme = 'public'; + $actual = image_style_path($this->style_name, "$scheme://foo/bar.gif"); + $expected = "$scheme://styles/" . $this->style_name . "/$scheme/foo/bar.gif"; $this->assertEqual($actual, $expected, t('Got the path for a file URI.')); $actual = image_style_path($this->style_name, 'foo/bar.gif'); - $expected = 'public://styles/' . $this->style_name . '/foo/bar.gif'; + $expected = "$scheme://styles/" . $this->style_name . "/$scheme/foo/bar.gif"; $this->assertEqual($actual, $expected, t('Got the path for a relative file path.')); } @@ -161,12 +162,10 @@ class ImageStylesPathAndUrlUnitTest exte // Make the default scheme neither "public" nor "private" to verify the // functions work for other than the default scheme. variable_set('file_default_scheme', 'temporary'); - $d = 'temporary://'; - file_prepare_directory($d, FILE_CREATE_DIRECTORY); // Create the directories for the styles. - $d = $scheme . '://styles/' . $this->style_name; - $status = file_prepare_directory($d, FILE_CREATE_DIRECTORY); + $directory = $scheme . '://styles/' . $this->style_name; + $status = file_prepare_directory($directory, FILE_CREATE_DIRECTORY); $this->assertNotIdentical(FALSE, $status, t('Created the directory for the generated images for the test style.')); // Create a working copy of the file. @@ -176,26 +175,13 @@ class ImageStylesPathAndUrlUnitTest exte $original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME); $this->assertNotIdentical(FALSE, $original_uri, t('Created the generated image file.')); - // Get the URL of a file that has not been generated yet and try to access - // it before image_style_url has been called. - $generated_uri = $scheme . '://styles/' . $this->style_name . '/' . basename($original_uri); + // Get the URL of a file that has not been generated and try to create it. + $generated_uri = $scheme . '://styles/' . $this->style_name . '/' . $scheme . '/'. basename($original_uri); $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.')); - $expected_generate_url = url('image/generate/' . $this->style_name . '/' . $scheme . '/' . basename($original_uri), array('absolute' => TRUE)); + $expected_generate_url = url(file_directory_path($scheme) . '/styles/' . $this->style_name . '/' . $scheme . '/' . basename($original_uri), array('absolute' => TRUE)); $this->drupalGet($expected_generate_url); - $this->assertResponse(403, t('Access to generate URL was denied.')); - - // Check that a generate URL is returned. - $actual_generate_url = image_style_url($this->style_name, $original_uri); - $this->assertEqual($actual_generate_url, $expected_generate_url, t('Got the generate URL for a non-existent file.')); - - // Fetch the URL that generates the file while another process appears to - // be generating the same file (this is signaled using a lock). - $lock_name = 'image_style_generate:' . $this->style_name . ':' . drupal_hash_base64($original_uri); - $this->assertTrue(lock_acquire($lock_name), t('Lock was acquired.')); - $this->drupalGet($expected_generate_url); - $this->assertResponse(503, t('Service Unavailable response received.')); - $this->assertTrue($this->drupalGetHeader('Retry-After'), t('Retry-After header received.')); - lock_release($lock_name); + $this->assertResponse(200, t('Image was generated at the URL.')); + $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.')); // Fetch the URL that generates the file. $this->drupalGet($expected_generate_url); @@ -204,16 +190,6 @@ class ImageStylesPathAndUrlUnitTest exte $generated_image_info = image_get_info($generated_uri); $this->assertEqual($this->drupalGetHeader('Content-Type'), $generated_image_info['mime_type'], t('Expected Content-Type was reported.')); $this->assertEqual($this->drupalGetHeader('Content-Length'), $generated_image_info['file_size'], t('Expected Content-Length was reported.')); - $this->assertTrue(lock_may_be_available($lock_name), t('Lock was released.')); - - // Check that the URL points directly to the generated file. - $expected_generated_url = file_create_url($generated_uri); - $actual_generated_url = image_style_url($this->style_name, $original_uri); - $this->drupalGet($expected_generated_url); - $this->assertEqual($actual_generated_url, $expected_generated_url, t('Got the download URL for an existing file.')); - $this->assertRaw(file_get_contents($generated_uri), t('URL returns expected file.')); - $this->assertEqual($this->drupalGetHeader('Content-Type'), $generated_image_info['mime_type'], t('Expected Content-Type was reported.')); - $this->assertEqual($this->drupalGetHeader('Content-Length'), $generated_image_info['file_size'], t('Expected Content-Length was reported.')); } }