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

17 April, 2024

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!

Login or Register to Comment!