Another Way to Display Content Images in Sava CMS

CFML , Mura CMS Add comments

I've been working with Sava CMS lately and ran into an issue with how content images are displayed. First, let me be clear that I'm NOT talking about images that are uploaed via the integrated FCKEditor that Sava uses for content editing. Sava also lets you attach an image to a content node via an upload field when you're adding/editing content:

Edit Content Form

Sava automatically generates small, medium, and large versions of the image (sizes are configurable in the Site Settings). What would be the benefit of uploading an image here instead of directy adding the image within the WYSIWYG editor, you ask? Well, sometimes you really want to lock down the design of the site and just make it easy for content editors to just upload images for which will show up in pre-defined areas of the site template. Basically, this allows you to integrate the assiociated image into the site template where you can apply whatever styling you want instead of relying on the user to be able to apply consistent styling to the image across all the pages of the site using FCKEditor (which can be tricky for non-tech savvy users).

By default, Sava displays the medium version of the image when you view the page (or the small image on listing pages). When you view the source of the page, though, this is how the image is displayed:

So, it gets the image file by calling the index.cfm template in the "tasks/render/medium/" directory and passing in the fileID. What this essentially does is call a fileManager component which uses cfcontent to render the image, whether it was stored in the file system or directly in the database (the storage method is also configurable in the Site Settings). I decided that I wasn't too crazy about this. Being a front-end developer as well, I'm very particular about how images are presented. I'd prefer for the image itself to be linked to (not a template that serves the image), and I also prefer there to be height and width attributes.

So what I did to get around this is write my own image display method and placed it in my local contentRenderer.cfc component. Each Sava install can manage multiple sites and each site is equipped with one of these components. They extend a base contentRenderer component which is in the core Sava files and can come in really handy because you can add custom content display code to it without having to hack into the core files. Here is the method that I wrote:

      

The only argument that needs to be passed in is "small" or "medium" for those versions of the image (by default, it will display the "large" version or the original version if it was smaller than the limit set in the site settings). Uploaded images are stored in the "/tasks/sites/[siteID]/cache/file" directory and are named for their file ID. The small and medium image versions have "_small" or "_medium" appended to the file name. After getting the file extension for the image from the database (and determining whether the small or medium versions was requested), the image is read in using the ImageRead() function. Then I use the ImageGetWidth() and ImageGetHeight() functions to get the values for the the height and width attributes for the image tag. By default, I use the page title for the alt tag of the image. So knowing the values for the path, file extension, width, height, and alt values, the image tag is then set up and returned.

So now, in my site template, I can do:

...and I'll get a "normal" image tag:

A couple of things to keep in mind: 1) this method as written will only work is your site is configured to store image in the file system, not the database. 2) since this method uses image functions, it will only work on CF8 or above (it was also tested on Railo 3 and should also work on Open BD). To improve performance, I'd also suggest you use this function in conjuction with Sava's CacheOMatic tag in your site template:

Hopefully, this will help others working with Sava CMS. If you have any questions, ask away.

6 responses to “Another Way to Display Content Images in Sava CMS”

  1. Rob Says:
    First, thanks for the function to be able to control the image display.
    I'm using Mura/Sava to implement a site and I was looking to something similar.

    I've tried your code above and I'm getting the following error getting the file extension.

    Invalid CFML construct found.
    ColdFusion was looking at the following text:
    [


    The issue seem to be with the ['fileExt']
    <cfset var ext = "#application.serviceFactory.getBean('fileManager').readMeta(arguments.fileID)['fileExt']#">

    Any ide what's going on.

    Thank you
  2. Tony Garcia Says:
    Hi Rob,
    Turns out that my code above only works on Railo and not ColdFusion, because Railo lets you use brackett notation for structs directly from method calls, but CF8 doesn't (I'm not sure about CF9).
    Try changing that line to:

    <cfset var ext = "#application.serviceFactory.getBean('fileManager').readMeta(arguments.fileID).fileExt#">

    try that and let me know if it works (and thanks for testing the code on CF)
  3. Rob Says:
    Tony;

    I've changed the line of code and now I'm getting this error when outputting #renderer.dspContentImage("medium")#
    =
    [empty string]
    ErrNumber    0
    Message    Variable RENDERER is undefined.

    If I output like this: #dspContentImage("medium")# I get this error:
    =File: /tasks/sites/mysite/cache/file/06E7BD58-03E4-ADC5-0F59FA693259B2F5_medium.jpg does not exist.

    =Message:    An exception occurred while trying to read the image.

    But when I copy the path into the browser that is the correct location for the file.
    Any idea what could be blocking code not to display the image.
    Thanks again.
  4. Rob Says:
    I can't get the imageRead() to read the image. Error:image does not exist. I've checked your code and everything seems to be OK. Does it need the #ImagePath# var twice when setting the strRet online 39???

    It does have the right path but for some reason I can't get it to work. I'm going to bed. I'll try again tomorrow.

    Thanks
  5. Tony Garcia Says:
    Rob,
    The repeating of the #imagePath# variable was a glitch in the code highlighter, so no you don't need that twice. I'm going to test this on CF8 and see if I can figure out what's happening.
  6. Lauw Says:
    I've run into the same problem on my cf9 install: File /savaWRM/tasks/sites/evafelice/cache/file/21EB563C-B835-5E95-149425AFD9B8D201_medium.jpg does not exist.

    Removing the basedir didn't help either although I know for sure the image exists at that location (tried browsing to it and that worked). Any ideas yet?

Leave a Reply

Leave this field empty:

Powered by Mango Blog. Design and Icons by N.Design Studio