Quick and Dirty Way to get Image Dimensions

…posted by davidjmedlock:





img = tk.getDefaultToolkit().getImage("C:image.jpg");
width = img.getWidth();
height = img.getHeight();

#width#
#height#

I’m by no means a master of all things Java, but this is an easy way to grab the height and width of an image. I haven’t tried it with GIF’s just yet, but I think it’ll work… Enjoy!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • QCWmstr

    I tried using this script and CF didn’t like the second period on the first script line – the one after getDefaultToolkit()

    Am I doing something wrong?

  • jonese

    try removing the () after getDefaultToolkit so it becomes img = tk.getDefaultToolkit.getImage(“C:image.jpg”);

  • tonyB

    A W E S O M E ! …thanks, I needed this!

  • lnxuser

    Wow! This is great! Thanks for posting this.

  • Tom Tedeschi

    Looks great, but I get this error… any thoughts?

    Element GETDEFAULTTOOLKIT is undefined in TK.
    The error occurred in D:InetpubflatfishdesigniiasisimageSize.cfm: line 4

    2 :
    3 :
    4 : img = tk.getDefaultToolkit.getImage(“./images/espn.gif”);
    5 : width = img.getWidth();
    6 : height = img.getHeight();

  • sam

    anyway of doing this with SWFs?

  • Pilar

    How can I use this script with online images. Ex. http://www.images.com/image.jpg ?

  • jrockfl

    Here is an example that uses it with online images.



    SELECT image
    FROM table







    img = tk.getDefaultToolkit().getImage(“C:wwwwww.yourdomain.comimages#image#”);
    width = img.getWidth();
    height = img.getHeight();


    #image#

  • http://www.rootskitchens.co.uk steveroot

    Does anyone have this working on Linux?

  • Joe

    I am also trying to make this work on Linux with no luck.

    Steveroot, have you found a solution on Linux.

    I`m looking for a way to determine a photo`s width and height so if any one has any suggestions it would be very much apreciated.

  • dmoola@gmail.com

    i always get -1 for both height and width…hrmph!