News, views, tips and tricks on Oracle and other fun stuff

Get image dimensions

Here is a quick and dirty (and easy) way to get the width and height of an image. It should work for both JPG and GIF files:

<cfobject type="JAVA" action="Create" name="tk" 
<cfobject type="JAVA" action="Create" name="img" 
img = tk.getDefaultToolkit().getImage("c:\img.gif");
width = img.getWidth();
height = img.getHeight();
<cfoutput>#width#<br />#height#</cfoutput>

That’s the beauty of ColdFusion, you have all of the Java language available to you right out of the box. ColdFusion is after all a Java application behind the scene.


Filed in ColdFusion on 16 Jun 05 | Tags: ,

Reader's Comments

  1. |

    I was using this code and ran into sporadic problems with the image being locked when I tried to do any additional image manipulation like deleting it if it is too large.

    If you add the line


    just before closing the cfscript the image will be released from memory.

    img = tk.getDefaultToolkit().getImage("c:\img.gif");
    width = img.getWidth();
    height = img.getHeight();
  2. |

    Emma, I appreciate sharing your problem and solution here. Thank you.

  3. |

    but,how to get the image size?

  4. |

    samon, if by size you mean how many bytes, the answer is simple. Since you know the image file name, c:\img.gif for example, you can easily read its size in ColdFusion using the cfdirectory tag.

  5. |

    this works quite good for what i’m wanting to achieve, i’m just wondering if there are any problems with caching ? if i save an image at say 500 wide 200 high and visit the page it outputs the dimensions just fine, when i change the image though and save it over the existing one ( so it’s now 300 x 100 for example ) i still get the previous w / h output and not the new image size. is there anyway around this ?

    hopefully that makes sense, am i missing something ??

  6. |

    Mark, try to use cfheader to force the client browser to purge its cache of the requested file:

    <cfheader name="Expires" value="#GetHttpTimeString(Now())#">
  7. |

    Hi Eddie, thanks for the response still don’t seem to have any luck with it though. When i resize and resave the images, hit refresh the image itself does change size and so updates. The w / h output values remain from the previous.

    i’m not ready to give up yet though, no one else probably has this problem anyway lol.

  8. |

    I ran across this page while looking for ways to get my photo gallery to play nice. I’ve used these bits of code in several different places now for getting images to display nicely. Basicly, i use this code to determine if its a portrait or landscape image and then size the image correctly. Its a real life saver because otherwise I ended up with images that were too tall or too wide. You can see it in action here: Photo Gallery I just want to say thanks. You really saved me some headaches.

  9. |

    Adrian, I’m glad that this post was helpful to you 🙂

  10. |


    Thanks. This was VERY helpful.

    Re: Mark’s question, I use this Javascript:

     var thisDate=new Date()
     var thisTime=thisDate.getSeconds()
     var i="#newImageLocation#"+time.valueOf();

    newImageLocation, of course, is the location of the image file on the server.

  11. |

    I’m getting the same issues as Mark now. With my photo gallery like it is, if I try to update the photos giving them larger dimensions the page remembers the smaller dimensions and doesnt display the image in the new larger size. If I add a new image instead of replaing the older one it works fine. It appears that the coldfusion/java server is what is storing the information as disabling my cache has no effect on it. Bart – I dont understand how that is supposed to work or where I’m supposed to put it. (my javascript skills are currently limited)

  12. |

    I could be wrong but I think all you have to do is add a throwaway token to the URL to pull the photos with the updated dimensions. For example…


    The JS in the previous post accomplishes the same thing but you could just as easily do it with CF.

  13. |

    I’m assuming there was supposed to be code or something there, but I see nothing.

  14. |

    Sorry about that.

  15. |

    Uh… that didn’t post either. Weird.

    This is what I wrote:

    [cfset thisNumber = RandRange(00000,99999)] [img src=”foo.gif?#thisNumber#” height=”#height#” width=”#width#” alt=””]

  16. |

    I did that and it picked up the new dimensions for the images I had already updated but any images i have updated since are not getting the new dimensions. How would I go about making coldfusion/java forget the image it is looking up?

  17. |

    I don’t understand what you mean…

    “…it picked up the new dimensions for the images I had already updated but any images I have updated since are not getting the new dimensions.”

    Can you post the code and explain exactly what you’re trying to do?

    My impression was this:

    You have an image with x-y dimensions. You resize it (say, in Fireworks) with new dimensions and save it to your server, but when you hit “refresh” the new image and new dimensions do not appear. Eddie’s script will get you the new width/height dynamically and adding the throwaway to the URL should get you the new image.

    That’s not what you’re trying to do?

  18. |

    The problem is that Eddie’s script from what i’ve been able to tell is not getting the new width/height for the new image. Here’s a snippit:

        >cfset thisNumber = RandRange(00000,99999)>
            img = tk.getDefaultToolkit().getImage
            width = img.getWidth();
            height = img.getHeight();
        >cfset newHeight = height>
        >cfset newWidth = width>
        >img src="images/PhotoGallery/Image_#getFirstPhoto.PK_autPhotoGalleryID#.jpg?#thisNumber#" 
             alt="#getFirstPhoto.txtPhotoName#" border="0" 
            >cfif height GT width>height="#variables.newHeight#"
            >/cfif> name="bigimage" />

    Example: Image1.jpg is 400×300 I open the page and the image shows up at 400×300 I upload a new Image1.jpg that is 640×480 I refresh the page and the image still shows up at 400×300

    The only thing I can come up with is the cfscript is somehow caching the height and width and is using that instead. Ideas?

  19. |

    Yep, I know what you mean.

    A solution (though not necessarily a good one) is this…

    (1) store the correct name in a variable; (2) create a temporary name using a random number; (3) rename the file using CFFILE (action=”rename” source=”correctName” destination=”tempName”) (4) use Eddie’s script to retrieve the h x w of the tempFile (5) rename the file using CFFILE (source=”tempName destination=”correctName”) (6) populate the h x w parameters of the img tag with the variables retrieved from the script.

    That works but I wouldn’t necessarily consider it a good solution, certainly not if lots of people are uploading/resizing lots of files.

  20. |

    After searching online and finding nothing new, I checked my code and page again and it picked up the new dimensions. Apparantly the cache expired. Any idea how I can adjust the caching time for this?

  21. |

    Try using cfheader as I mentioned in my comment above.

  22. |

    Bart, I know you have tried to post your last comment several times. I apologize. The blog’s spam filter mistakenly identified your comment as spam (I have no idea why).

  23. |

    I added the cfheader like you mentioned with no luck. Fortunatly I dont plan on having people updating existing photos too often. Its just one of those things.

  24. |

    If it’s primarily for your purposes, then you might as well use the cffile option and strip it out once you’re done.

    It’s far from ideal but it works.

  25. |

    Re: Bart I’d use that but i’m using this script for the display of already uploaded images. So it would do that whenever someone accessed the page.

  26. |

    some one point me to a shared host that will allow createObject?

  27. |

  28. |

    This is a great solution. I’ll use it till hostmysite starts using cf8.

  29. |

    Hi, came across this site while looking for a solution to finding image dimensions in CF. Eventually came across a great CFC that other readers may find useful. Download the CFC from here.

    It can also crop/resize etc etc but I only wanted to get the image dimensions.

    In case it’s useful, here’s a severely chopped version of my code:

    <cfparam name="URL.imgsrc" default="">
    if (URL.imgsrc is not "") {
        imgObj=CreateObject("component", "tmt_img");
        theWidth = imgObj.getDimensions(URL.imgsrc).width;
        theHeight = imgObj.getDimensions(URL.imgsrc).height;
        writeOutput("Image width = " & theWidth & "<br />");
        writeOutput("Image height = " & theHeight);
    } else {
        writeOutput("Need image path (absolute or local)");