7.x image.module image_style_path($style_name, $uri)

Returns the URI of an image when using a style.

The path returned by this function may not exist. The default generation method only creates images when they are requested by a user's browser.

Parameters

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

$uri: The URI or path to the image.

Return value

The URI to an image style image.

See also

image_style_url()

8 calls to image_style_path()
ImageFileMoveTest::testNormal in modules/simpletest/tests/image.test
Tests moving a randomly generated image.
ImageStyleFlushTest::createSampleImage in modules/image/image.test
Given an image style and a wrapper, generate an image.
ImageStylesPathAndUrlTestCase::testImageStylePath in modules/image/image.test
Test image_style_path().
ImageStylesPathAndUrlTestCase::_testImageStyleUrlAndPath in modules/image/image.test
Test image_style_url().
image_path_flush in modules/image/image.module
Clears cached versions of a specific file in all styles.

... See full list

File

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

Code

function image_style_path($style_name, $uri) {
  $scheme = file_uri_scheme($uri);
  if ($scheme) {
    $path = file_uri_target($uri);
  }
  else {
    $path = $uri;
    $scheme = file_default_scheme();
  }
  return $scheme . '://styles/' . $style_name . '/' . $scheme . '/' . $path;
}

Comments

charlie-s’s picture

"The default generation method only creates images when they are requested by a user's browser."

This suggests there are other generation methods, but I can find none.

plong0’s picture

I had the need for direct access to the file of a styled derivative image. The thing is that the derivative image does not exist until it has been requested (through a call to image_style_deliver(...))

Here is a code block that will ensure the styled derivative image file exists.

$styleName = 'pdf_embed'; // this is the name of our image style we want the image for
foreach($node->field_my_images['und'] as $cImage){
  // $cImage['uri'] is stored by the File Field
  if(isset($cImage['uri'])){

    // get the actual filename of the styled derivative
    $styleFileName = drupal_realpath(
                                     image_style_path(
                                                     $styleName,
                                                     $cImage['uri']
                                                     )
                                    );

    if(!file_exists($styleFileName)){
      // if the file is missing
    
      // load the style definition - empty array returned if image_style_load fails
      $styleDef = image_style_load($styleName);

      if(count($styleDef)){
        // finally, we can create the styled derivative file
        image_style_create_derivative(
                                    $styleDef,
                                    $cImage['uri'],
                                    image_style_path(
                                                    $styleName,
                                                    $cImage['uri']
                                                    )
                                   );

        // file_exists($styleFileName); should now return TRUE
        // if not, something went wrong
      }
    }
  }
}
jkalawe’s picture

This really should be built into these image styles functions. Works great for our new image gallery!

joncjordan’s picture

Awesome, this works great! I needed to do this in various places on my site so I created a little module function based on this code, where I can pass in an image style and a uri.

acbramley’s picture

Drupal 7.20 added tokens to image style paths:

The security fixes in this release change all image derivative URLs generated by Drupal to append a token as a query string. (As an example, links that previously pointed to a URL like http://example.com/sites/default/files/styles/thumbnail/public/field/image/example.png will now point to a URL like http://example.com/sites/default/files/styles/thumbnail/public/field/image/example.png?itok=zD_VaCaD.)

Using this function will not create a url with a token so using this function to generate a url which is presented on the front-end no longer works. You must use image_style_url() instead.

CarlHinton’s picture

It would be really nice to be able to alter the location of the style directory via a hook. For example I am storing all my files in an S3 bucket via S3FS hooked onto the files directory. I do not really want the styles directory sitting in the S3 bucket, I would like them on my EC2 instance.