Fee Fid Fo Fum - Retrieving an Image with a FID in Drupal 6

I had the need to add an image tag to page output that was generated inside a custom Drupal module. I had retrieved the image's FID (file ID) from the CCK field, and needed to get from there to having an ImageCache version of the image displayed. I'd done it within a view by selecting the appropriate field format in the Views UI, but how to do it in code? Like many other tasks in Drupal, the solution turned out to be easier than the hunt for it.

$my_array = field_file_load($fid);

The above line will return an array containing, among other things, the subscript 'filepath', which contains the path to the image file. We'll use that in the next line.

$my_image = theme('imagecache',$my_preset_name,$my_array['filepath']);

This line returns a complete image tag. We could have also passed contents for the alt and title tags.

And that's it!

3 comments

by chichi latté (not verified) on Wed, 2012-02-08 16:06

Man this was hard to find! Poor old Drupal, and me. Thanks!

by Grzegorz Nowak (not verified) on Tue, 2012-09-25 08:38

almost perfect.
What you should have changed is the theme function invocation; it should read:
$my_image = theme('imagecache', $my_preset_name,$my_array['filepath']);
with your method Drupal won't be able to include customizations made to base theme_imagecache.

by j ayen green on Tue, 2012-10-09 08:42

Yes, you're correct. I have changed the code in the article. Thanks!

©2005-2017 Ayen Designs. Underlying theme by Kiwi Themes.