diff --git modules/image/image.module modules/image/image.module index 23f2e9b..fae5c90 100644 --- modules/image/image.module +++ modules/image/image.module @@ -798,16 +798,18 @@ function image_style_flush($style) { * @see image_style_deliver() */ function image_style_url($style_name, $path) { - $scheme = file_uri_scheme($path); - if ($scheme === 'private') { - $target = file_uri_target($path); - $url = url('system/files/styles/' . $style_name . '/' . $scheme . '/' . $target, array('absolute' => TRUE)); + $uri = image_style_path($style_name, $path); + + // If not using clean URLs, the image derivative callback is only available + // with the query string. If the file does not exist, use url() to ensure + // that it is included. Once the file exists it's fine to fall back to the + // actual file path, this avoids bootstrapping PHP once the files are built. + if (!variable_get('clean_url') && file_uri_scheme($uri) == 'public' && !file_exists($uri)) { + $directory_path = file_stream_wrapper_get_instance_by_uri($uri)->getDirectoryPath(); + return url($directory_path . '/' . file_uri_target($uri), array('absolute' => TRUE)); } - else { - $destination = image_style_path($style_name, $path); - $url = url(file_stream_wrapper_get_instance_by_scheme($scheme)->getDirectoryPath() . '/' . file_uri_target($destination), array('absolute' => TRUE)); - } - return $url; + + return file_create_url($uri); } /** @@ -1085,16 +1087,7 @@ function image_effect_apply($image, $effect) { * @ingroup themeable */ function theme_image_style($variables) { - $style_name = $variables['style_name']; - $path = $variables['path']; - - // The derivative image is not created until it has been requested so the file - // may not yet exist, in this case we just fallback to the URL. - $style_path = image_style_path($style_name, $path); - if (!file_exists($style_path)) { - $style_path = image_style_url($style_name, $path); - } - $variables['path'] = $style_path; + $variables['path'] = image_style_url($variables['style_name'], $variables['path']); return theme('image', $variables); } diff --git modules/image/image.test modules/image/image.test index 7529add..435e41f 100644 --- modules/image/image.test +++ modules/image/image.test @@ -156,12 +156,27 @@ class ImageStylesPathAndUrlUnitTest extends DrupalWebTestCase { } /** + * Test image_style_url() with the "public://" scheme and unclean URLs. + */ + function testImageStylUrlAndPathPublicUnclean() { + $this->_testImageStyleUrlAndPath('public', FALSE); + } + + /** + * Test image_style_url() with the "private://" schema and unclean URLs. + */ + function testImageStyleUrlAndPathPrivateUnclean() { + $this->_testImageStyleUrlAndPath('private', FALSE); + } + + /** * Test image_style_url(). */ - function _testImageStyleUrlAndPath($scheme) { + function _testImageStyleUrlAndPath($scheme, $clean_url = TRUE) { // 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'); + variable_set('file_default_schema', $clean_url); // Create the directories for the styles. $directory = $scheme . '://styles/' . $this->style_name; @@ -183,6 +198,10 @@ class ImageStylesPathAndUrlUnitTest extends DrupalWebTestCase { $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.')); $generate_url = image_style_url($this->style_name, $original_uri); + if (!$clean_url) { + $this->assertTrue(strpos($generate_url, '?q=') !== FALSE, 'When using non-clean URLS, the system path contains the query string.'); + } + // Fetch the URL that generates the file. $this->drupalGet($generate_url); $this->assertResponse(200, t('Image was generated at the URL.'));