function image_style_url

7.x image.module image_style_url($style_name, $path)

Returns the URL for an image derivative given a style and image path.

Parameters

$style_name: The name of the style to be used with this image.

$path: The path to the image.

Return value

The absolute URL where a style image can be downloaded, suitable for use in an <img> tag. Requesting the URL will cause the image to be created.

See also

image_style_deliver()

10 calls to image_style_url()
ImageAdminStylesUnitTest::createSampleImage in drupal-7.x/modules/image/image.test
Given an image style, generate an image.
ImageAdminStylesUnitTest::testStyleReplacement in drupal-7.x/modules/image/image.test
Test deleting a style and choosing a replacement style.
ImageDimensionsTestCase::testImageDimensions in drupal-7.x/modules/image/image.test
Test styled image dimensions cumulatively.
ImageFieldDisplayTestCase::testImageFieldSettings in drupal-7.x/modules/image/image.test
Tests for image field settings.
ImageFieldDisplayTestCase::_testImageFieldFormatters in drupal-7.x/modules/image/image.test
Test image formatters on node display.

... See full list

File

drupal-7.x/modules/image/image.module, line 1013
Exposes global functionality for creating image styles.

Code

function image_style_url($style_name, $path) {
  $uri = image_style_path($style_name, $path);

  // The passed-in $path variable can be either a relative path or a full URI.
  $original_uri = file_uri_scheme($path) ? file_stream_wrapper_uri_normalize($path) : file_build_uri($path);

  // The token query is added even if the 'image_allow_insecure_derivatives'
  // variable is TRUE, so that the emitted links remain valid if it is changed
  // back to the default FALSE.
  $token_query = array(IMAGE_DERIVATIVE_TOKEN => image_style_path_token($style_name, $original_uri));

  // 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, 'query' => $token_query));
  }

  $file_url = file_create_url($uri);
  // Append the query string with the token.
  return $file_url . (strpos($file_url, '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
}