0

In a custom module I'm trying to display results from mysql DB based on items users selected in a form. One of the fields in the content type I'd like to display is an image associated with the node. This image appears to be housed in the table 'file_managed' in a field called 'filename' - or at least that's where the NAME of the file is located. I'm able to retrieve that field fine using SQL and joins and when I echo the result it shows the following (for example):

nameoffile.png

So I tried calling that with php using:

$icon = $row['filename'];
echo $icon; // works fine

$output .= '<img src="' . $icon . '" />';

And it just shows a blank image icon (not the image nameoffile.png).

So then I think well I must have the path wrong. So I dragged the image icon from the search results page (where it displays just fine) into a new tab and this is the URL I get:

http://pf.dev/sites/default/files/styles/thumbnail/public/PPP_CCYCL_CLP_ChrevonCycle_A_8Red.png

But of course I don't want to use an absolute path, esp. since this is on the dev. server.

So my question is this: how do I find the paths to my content type images so I can display them correctly in my custom module?

2 Answers 2

1

There are a couple of things you can do. First, instead of grabbing the filename field from the {file_managed} table, you'll want to grab the uri.

Then you can use file_create_url() to generate a url to the original file:

$icon = file_create_url($row['uri']);
$output .= '<img src="' . $icon . '" />';

Or if you want to run the image through an image style, you can use image_style_url() (you'll need to know the machine name of the image style)

$icon = image_style_url('image_style', $row['uri']);
$output .= '<img src="' . $icon . '" />';
1
  • That worked very well thank you. Now I just need to set the correct image style, but I'll figure that out. The image appears now! :)
    – Sage
    Feb 13, 2015 at 5:08
0

You will get tid and nid from table taxonomy_index and then using the nid you can able to fetch fid, uri from table file_managed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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