11

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?

closed as off-topic by kiamlaluno Dec 16 '15 at 10:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

22

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);
  • 2
    Shoud there not be an if at the end? ie: if ($success) { $new_image_url = file_create_url($derivative_uri); } – stefgosselin Oct 31 '13 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'); – Victor Lazov Mar 5 '14 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 Jul 21 '15 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 Oct 20 '15 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 Oct 22 '15 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.

  • The style exists, the folder name is the same as the style name in my case. – Bart Sep 19 '11 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? – Sang Le Thanh Sep 19 '11 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 Sep 19 '11 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. – Sang Le Thanh Sep 20 '11 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.