4

In hook_node_load I need to get the width and height of a styled image, for a given fid.

The image style has only height effect and the width is scaled and calculated by Drupal, so using image_styles() is of no use.

My code so far is like this:

  $field_image = field_get_items('node', $node, 'field_image', LANGUAGE_NONE);
  $img_uri = file_load($field_image[0]['fid']);
  $img_url = image_style_url('mystyle', $img_uri->uri);
  $img_info = image_get_info ($img_url);

$img_info gives me FALSE instead of the info. How to get the width?

1

according to documentation, you just need image_style_path instead of image_style_url

and finally

 $field_image = field_get_items('node', $node, 'field_image', LANGUAGE_NONE);
 $img_uri = file_load($field_image[0]['fid']);
 $img_url = image_style_path('mystyle', $img_uri->uri);
$img_info = image_get_info ($img_url);
  • This finally worked for me (with two additional steps): $img_uri = file_load($field_image[0]['fid']); $img_style_uri = image_style_path('mystyle', $img_uri->uri); $img_url = file_create_url($img_style_uri); $img_server_path = drupal_realpath($img_style_uri); $img_info = image_get_info ($img_server_path); --- Besides, for this to work, the styled image must already be created (displayed at least once) – camcam Feb 9 '15 at 12:07

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.