12

I am using image_style_url to get a thumbnail of a picture I added through the administrator interface.

// $foo->field_picture['und'][0]['uri'] has value public://foo.jpg
image_style_url('foo-thumb', $foo->field_picture['und'][0]['uri']);

The API states that the image will be created when the URL is requested. However, when visiting the page the image isn't displayed (404). The URL returned from the function seems to be well formed, but the file hasen't been created.

The 'foo-thumb' folder is in the public data folder and the file permissions should be OK. I tried flushing the image cache (drupal image-flush foo-thumb) but still no results.

Any ideas?

7 Answers 7

23

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.

Instead you can do something like this;

$image_uri      = $node->field_image[LANGUAGE_NONE][0]['uri']; // or any public://my_image
$style          = 'my_style';
$derivative_uri = image_style_url($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);
6
  • 2
    Shoud there not be an if at the end? ie: if ($success) { $new_image_url = file_create_url($derivative_uri); } Commented Oct 31, 2013 at 19:56
  • 3
    A little update for the image_style_create_derivative the $style variable should be array $style = image_style_load('my_style'); Commented Mar 5, 2014 at 14:07
  • I've edited the answer to pass $style though image_style_load() before passing to image_style_create_derivative() as per the docs.
    – Felix Eve
    Commented Jul 21, 2015 at 2:41
  • Everything worked in this except that file_create_url($derivative_uri); did not work for me. What did work was substituting that (on the last line) with image_style_url($style, $image_uri);
    – Mark
    Commented Oct 20, 2015 at 17:57
  • This method of file_create_url does not apply image style. I found a solution that applying another image style. Really strange.
    – Henry
    Commented Oct 22, 2015 at 3:34
7

One common mistake is that URIs are not properly formatted. Check that your URI parameter is prefixed with public://.

Bad Practice

$imageUrl = image_style_url('style_name', 'myimage.jpg');
$imageUrl = image_style_url('style_name', $node->field_x[$node->language][0]['filename']);

This outputs a URL to the styled image (with image token), but does not generate a styled image when it hasn't been previously generated.


Good Practice

$imageUrl = image_style_url('style_name', file_build_uri('myimage.jpg')); // changes to 'public://myimage.jpg'
$imageUrl = image_style_url('style_name', $node->field_x[$node->language][0]['uri']);

This outputs a styled URL, like the above, and generates a styled image if none exists.

1

The public file system path was wrong.

1

Another option is to use the theme function.

$img = theme('image_style', array('style_name' => 'your_image_style_machine_name', 'path' => $node->field_image['und'][0]['uri']));

Rather than returning the URL it will return the html for the img.

1
$fid = file_load($field_yuorfield_fid);          
$image_uri = image_style_url('yuor_style', $fid->uri);
fopen($image_uri, 'r');

here can use url for create html

0

Make sure you have foo-thumb style, you can check at admin/config/media/image-styles.

The first parameters of image_style_url is a style name, not a folder name.

4
  • The style exists, the folder name is the same as the style name in my case.
    – Bart
    Commented Sep 19, 2011 at 13:42
  • If you got the error 404, that mean the menu doesnot exist. I think all of your image styles have this problem. Can you explain more about you Drupal version and information: - What webserver you are using? - Is clean url setting is correct? Commented Sep 19, 2011 at 14:25
  • I don't know if the menu doesn't exist, but the file surely doesn't. The URL returned seems about right, but if I check my folder on the server the image isn't there. The folder created by Drupal ('foo-thumb') is there though. Running Apache 2.2 with clean URLs and all seem to be working fine.
    – Bart
    Commented Sep 19, 2011 at 15:01
  • I can see your image generate link was not found. Can you check the .htaccess file to check the line ErrorDocument 404 /index.php, are you running Drupal on top of your domain? Other try is install imagemagick toolkit but i see your problem isnot problem by the image render of php. Commented Sep 20, 2011 at 5:07
0

If you receive a 404 when trying to view the image, a possibility is that the image style name is not correct. In my case, the style name that showed up in Image Styles was not the machine name of the style name. When hovering over the style the url will display the machine name of the style name. Either change it to the machine name or delete the current and recreate with the name needed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.