getURLForImage - Mura Docs v7.0

getURLForImage

A Mura Scope helper method to output the URL for an image based on a fileID. The result can then be used to populate the src attribute of a HTML image tag (<img>) to display the image via a browser.

Function Syntax

m.getURLForImage(
  fileID
  , size
  , complete
  , height
  , width
  , siteID
)

Parameters

Parameter Type Req/Opt Default Description
fileID string Req  

The fileID as stored in Mura's database to use as the source image file.

size string Opt large

The image size to use. Valid options are:

complete boolean Opt false If true, will output the entire path to the image, including the domain. If false, will output the absolute path to the image, from the webroot.
height numeric* Opt AUTO If size parameter is set to custom, you may pass in your desired height in pixels. Or, if passing in the width parameter, you may use AUTO, and Mura will maintain the aspect ratio of the original source image.
width numeric* Opt AUTO If size parameter is set to custom, you may pass in your desired width in pixels. Or, if passing in the height parameter, you may use AUTO, and Mura will maintain the aspect ratio of the original source image.
siteID string Opt SiteID of the current request context If you wish to obtain an image from another site hosted under the same instance of Mura, you may optionally pass the desired siteID.

Usage

Use this function to output the path to a Mura image. If an invalid fileID is used, the result will be an empty string.

Examples

There are several ways you can use this method. Using Class Extensions (covered in the Mura Developer Guide), there could be any number of associated images to a content item. Using the fileID of any image, you can use the following examples to output an image.

The examples use a simple bit of CFML logic first, to make sure the image actually exists. For more information on the CFML tag cfif, please visit https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-i/cfif.html.

Large Image

The following example outputs a Large image of the current content item. This is very similar to the Get Associated Image example found under the content object section.

<cfoutput>
<cfif Len(m.getURLForImage(fileid=m.content('fileid')))>
  <img src="#m.getURLForImage(fileid=m.content('fileid'))#">
</cfif>
</cfoutput>

Medium Image

The following example outputs a Medium image of the current content item.

<cfoutput>
<cfif Len(m.getURLForImage(fileid=m.content('fileid')))>
  <img src="#m.getURLForImage(fileid=m.content('fileid'), size='medium')#">
</cfif>
</cfoutput>

Medium Image with Alt Attribute

The following example outputs a Medium image of the current content item, and populates the alt attribute of the <img> tag with the content item's Title.

<cfoutput>
<cfif Len(m.getURLForImage(fileid=m.content('fileid')))>
  <img src="#m.getURLForImage(fileid=m.content('fileid'), size='medium')#"
  alt="#esapiEncode('html_attr', m.content('title'))#">
</cfif>
</cfoutput>

Site Placeholder Image

As covered under How to Manage Image Sizes, each site can have a Placeholder Image. This example demonstrates how to output a placeholder image.

<cfoutput>
<cfif Len(m.getURLForImage(fileid=m.siteConfig('placeholderImgID')))>
  <img src="#m.getURLForImage(fileid=m.siteConfig('placeholderImgID'))#">
</cfif>
</cfoutput>

Display Site Placeholder Image if Associated Image Doesn't Exist

The example below is useful for when you want to output the placeholder image for content that doesn't have an associated image.

<cfoutput>
<cfif Len(m.getURLForImage(fileid=m.content('fileid')))>
  <img src="#m.getURLForImage(fileid=m.content('fileid'))#">
  <cfelseif Len(m.getURLForImage(fileid=m.siteConfig('placeholderImgID')))>
  <img src="#m.getURLForImage(fileid=m.siteConfig('placeholderImgID'))#">
</cfif>
</cfoutput>