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"
class="java.awt.Toolkit">
</cfobject>
<cfobject type="JAVA" action="Create" name="img"
class="java.awt.Image">
</cfobject>
<cfscript>
img = tk.getDefaultToolkit().getImage("c:\img.gif");
width = img.getWidth();
height = img.getHeight();
</cfscript>
<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.
Possibly related:
- Writing/Reading images to/from DB
- AskTom Search Engine Plugin Revived
- Free screen capture software
- Using CUBE to generate rows
- Preview of Salary Survey Results
Tagged image, java | Post a Comment


















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(); img.flush(); #width##height#January 24th, 2006, at 10:45 am #Emma, I appreciate sharing your problem and solution here. Thank you.
January 24th, 2006, at 12:08 pm #but,how to get the image size?
March 7th, 2006, at 5:33 pm #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.
March 7th, 2006, at 7:50 pm #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 ??
March 28th, 2006, at 5:10 am #Mark, try to use cfheader to force the client browser to purge its cache of the requested file:
March 28th, 2006, at 12:33 pm #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.
March 29th, 2006, at 12:20 am #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.
June 1st, 2006, at 1:16 pm #You can see it in action here: Photo Gallery
I just want to say thanks. You really saved me some headaches.
Adrian, I’m glad that this post was helpful to you
June 2nd, 2006, at 9:41 pm #Eddie-
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();
document.write(”")
newImageLocation, of course, is the location of the image file on the server.
July 25th, 2006, at 12:07 pm #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.
August 7th, 2006, at 8:09 am #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)
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.
August 7th, 2006, at 9:00 am #I’m assuming there was supposed to be code or something there, but I see nothing.
August 7th, 2006, at 9:07 am #Sorry about that.
August 7th, 2006, at 9:12 am #Uh… that didn’t post either. Weird.
This is what I wrote:
[cfset thisNumber = RandRange(00000,99999)]
August 7th, 2006, at 9:14 am #[img src=”foo.gif?#thisNumber#” height=”#height#” width=”#width#” alt=”"]
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?
August 7th, 2006, at 10:46 am #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?
August 7th, 2006, at 11:03 am #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)> >cfscript> img = tk.getDefaultToolkit().getImage ("#Request.Server_Path#\images\PhotoGallery\Image_#getFirstPhoto.PK_autPhotoGalleryID#.jpg"); width = img.getWidth(); height = img.getHeight(); img.flush(); >/cfscript> >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#" >cfelse>width="#variables.newWidth#" >/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?
August 7th, 2006, at 11:27 am #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.
August 7th, 2006, at 12:42 pm #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?
August 7th, 2006, at 2:34 pm #Try using cfheader as I mentioned in my comment above.
August 7th, 2006, at 9:29 pm #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).
August 7th, 2006, at 9:59 pm #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.
August 8th, 2006, at 7:03 am #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.
August 8th, 2006, at 7:08 am #Re: Bart
August 8th, 2006, at 7:10 am #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.
some one point me to a shared host that will allow createObject?
March 28th, 2007, at 2:21 am #google.com/search?q=coldfusion+hosting+createobject
April 1st, 2007, at 5:07 pm #This is a great solution.
June 7th, 2007, at 3:17 pm #I’ll use it till hostmysite starts using cf8.
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=""> <cfscript> 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)"); } </cfscript>September 11th, 2007, at 7:56 am #