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

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


$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


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

... See full list


modules/image/image.module, line 1023
Exposes global functionality for creating image styles.


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.
  // However, sites which need to prevent the token query from being emitted at
  // all can additionally set the 'image_suppress_itok_output' variable to TRUE
  // to achieve that (if both are set, the security token will neither be
  // emitted in the image derivative URL nor checked for in
  // image_style_deliver()).
  $token_query = array();
  if (!variable_get('image_suppress_itok_output', 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)
    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, if necessary.
  if ($token_query) {
    $file_url .= (strpos($file_url, '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
  return $file_url;


jyee’s picture

The parameter $path is actually a uri in the form public://path/to/file

To get a proper uri you can use file_build_uri() or you can find it in the uri column of the file_managed table.

If you don't use a uri and only use a path (e.g. "path/to/file"), you'll receive a php fatal error: Call to a member function getDirectoryPath() on a non-object

AlmogBaku’s picture

thanks for saving my day! :)
** I think this information sould be on the wiki

angel.angelio’s picture

I always do it with the URI

New Zeal’s picture

Note also that this function will not work with an invalid uri. I tried using: $uri = file_build_uri($path); to convert a path to a uri, and in my case the path was a complete path such as /sites/default/files/rest/of/path. file_build_uri simply converted my path to public://sites/default/files/rest/of/path which is invalid for this function. To make it work I removed the sites/default/files part so that the uri becomes public://rest/of/path so that the path part consists of only that part within the public file repository.

Dharini’s picture


I have a requirement in my project for 360 degree view of product Image. I am using Version 6 for drupal. I went through Jquery Reel Formatter module but it is still under development for Drupal 6. Could anyone suggest me a module for the same or could anyone suggest me a solution or a customized module if someone has gone through a similar requirement.

Many thanks in advance,

matt2000’s picture

The discussion forum is over there. Comments here are for sharing information about the PHP functions documented here.

jibran’s picture

how can i get image_style_uri?

jibran’s picture

found this :) image_style_path

manderson725’s picture

BarisW’s picture

The Imagecache External module can do this for you (caching external images).

rogical’s picture

seems doesn't work with outside images, I"m using storage api for saving images

J. Cohen’s picture

The changes related to this in Drupal 7 are bad for SEO. Changing image URLs can potentially drop a site's traffic significantly.

Sibiraj PR’s picture

Usage of this fuunction

$img_url = $node->field_image['und'][0]['uri'];
<img src="<?php print image_style_url("thumbnail", $img_url); ?>" />

You can change 'thumbnail' to your image style name

Puppetmast0r’s picture

thank you, this was exactly what I needed.

I was trying to get my data from $content. Should have peeked inside $node rightaway. You've been a big help.

Puppetmast0r’s picture

I also had to replace "field_image" with my image field machine name :)

subgero’s picture

Thanks man! This works perfect.

matthieu.symetris’s picture

From: https://drupal.org/drupal-7.20-release-notes

In addition, any code which programmatically generates a link to an image derivative without using the standard image_style_url() API function will no longer work correctly if the image does not already exist in the file system, since the necessary token will not be present in the URL.

You can try something like that if you absolutely need the image to be created

$image_uri      = $node->field_image['und'][0]['uri']; // or any public://my_image
$style          = 'my_style';
$derivative_uri = image_style_path($style, $image_uri);
$success        = file_exists($derivative_uri) || image_style_create_derivative($style, $image_uri, $derivative_uri);

$new_image_url  = file_create_url($derivative_uri);;
matthieu.symetris’s picture

I missread the documentation release note, it says "if you *don't* use image_style_url()"
However, I used it in a .tpl and I had to use the workaround above.

will_richards’s picture

Thanks, this worked for me - however to get it to work I discovered that image_style_create_derivative requires the image style to be an array. A comment on the documentation for that function reveals you can use image_style_load($style) to load the image style.

So the updated code becomes:

$image_uri      = $node->field_image['und'][0]['uri']; // or any public://my_image
$style          = 'my_style';
$derivative_uri = image_style_path($style, $image_uri);
$success        = file_exists($derivative_uri) || image_style_create_derivative(image_style_load($style), $image_uri, $derivative_uri);
$new_image_url  = file_create_url($derivative_uri);
jstnhffmn’s picture

I'm trying to use an image style with an image that is NOT stored under a uri directive (eg. public://), but is still on the local server. (eg. A path like /sites/all/modules/my_module/img.jpg.)

However, my image is not being generated (nor any of the directories) and I am getting a page access error when navigating to the generated URL. (eg. my-site[dot]com/sites/default/files/styles/my_image_style/public/sites/all/modules/my_module/img.jpg?itok=[token].)

Can someone verify whether I am using the function correctly?

Luzgan’s picture

Someone tried to use this function in a drush command? I getting totally wrong url, when I call this in drush. When I use outside of drush, everything is ok.

graceman9’s picture

If you pass NULL (empty/undefined) in $image_style parameter you will get url like this:
You can see a "//" here in url.
Please be careful :) I've spent a lot of time with that.

graceman9’s picture

Sorry, I mean parameter $style_name instead $image_style.

drpl’s picture

to get the path without domain I did this

function _custom_image_style_url($style_name, $path) {
  $style_url = image_style_url($style_name, $path);
  $url = parse_url($style_url);
  $url_path = $url['path'] . '?' . $url['query'];
  return $url_path;
pribeh’s picture

I'm trying to use
image_style_url('style', $url)

but it's giving me this:


How can that be? What could be going wrong here?

caiobianchi’s picture

Same here. Has anything changed in the API?

rineez’s picture

Looks like the problem is passing a URL there. image_style_url expects a file URI, not a URL.

Sorry pribeh, I understand this answer is too late for you, but I hope this will be useful for someone else.

TheBigCat’s picture

Hey man! I'm having exactly the same issue that pribeh mentioned... is there a fix documented somewhere for this?

rayvan’s picture

This stackoverflow worked out for me for when "file_exists()" failed and I wanted to present the certain content type's default image

btopro’s picture

image_style_path requires a uri so the documentation is wrong. it's not style, path. It's style,uri