I create below function to create image style programmatically

function imh_get_image_from_uri($image_uri, $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);
    return theme_image(array('path' => $new_image_url));

and call in my tpl with


and a example of image created path is


I check the images style , as expamle this image image style set to scale 215 * 317,every thing seems work good but when I see image directly I see the image in this path not resized ( I call my function with another image style but also they are not resize or scale or ....) , The images that use build-in drupal image style work correctly but when I use top function to load image style programmatically the image style action (resize, scale ,... ) not applied, the image create on that path but noting applied on it.

where is problem?

update Also I try

$build = array(
  '#theme' => 'image_style',
  '#path' => $image_uri,
  '#style_name' => $style,
return render($build);

output <img src="http://mydomain.com/sites/default/files/styles/215x317/public/Hugh%20Jackman-2.jpg?itok=PcP0uFjT" width="215" height="317">

but the problem exists yet, the file in the image path exists but not resized. it seems change it by style ( not resized main image)

  • image_style_create_derivative expects path to image, not full URL. Are you sure you feed this function with proper data? And why don't you just prepare a renderable array for image?
    – Mołot
    Aug 6, 2014 at 11:43
  • @Mołot ,I am not sure that this function work correctly, can you help that how can I get image style from uri of file and specified image style?
    – Yuseferi
    Aug 6, 2014 at 11:57
  • @Mołot I see your question, and try something like it but the problem exists, the image not resized
    – Yuseferi
    Aug 6, 2014 at 12:16
  • Well, if renderable array way does not work for you either, then I have no idea. It worked for me all right.
    – Mołot
    Aug 6, 2014 at 12:29
  • @Mołot I found solution ,I should delete previous image created, they prevent to create
    – Yuseferi
    Sep 8, 2014 at 10:43

2 Answers 2


The solution is

  return  theme('image_style',array('style_name' => $style, 'path' => $image_uri));

but this not work first because the image style for that file created before , first I should remove it then try this ;)


I had a similar problem. This solved my issue, maybe it can help you out.

$image = field_get_items('node', $node, 'field_image');
$image_output = render(field_view_value('node', $node, 'field_image', $image[0],
array('settings' => array('image_style' => 'medium', 'label' => ''))));                   

print render($image_output);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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