{ Get The Path To An Attached Image }

When you attach an image to a node in Drupal, it becomes a node of it’s own. If you wanted to path to that image, you would have to dig through three tables to find it. Here’s a handy function to do that for you.

You pass it the node id of the node the image is attached to, and the size you want. It returns a string file path that you can use in your HTML IMG tag.

function lookupImagePath($nid, $size = '_original') {

	$sql  = 'SELECT f.filepath ';
	$sql .= 'FROM image_attach a, image i, files f ';
	$sql .= 'WHERE a.iid = i.nid ';
	$sql .= 'AND i.fid = f.fid ';
	$sql .= 'AND a.nid = ' . $nid . ' ';
	$sql .= 'AND i.image_size = "' . $size . '" ';

	$res = db_query($sql);
	$filepath = db_result($res);
	$filepath = htmlentities($filepath);

	return $filepath;

} // end lookupImagePath

Leave a Reply

Your email address will not be published. Required fields are marked *