ActionScript: Get dimensions of loaded jpeg?

Discussion in 'Web Design and Development' started by londonweb, Jan 30, 2006.

  1. macrumors 6502


    Another Actionscript problem (but hopefully a quick one):

    How do I obtain the dimensions of a jpeg that has been loaded into a movieclip using the loadMovie() function? If I ask for the _width and _height values, I just get the size of the original movieclip into which the jpeg was loaded (ie. 0, since it's an empty clip), but not the size of the jpeg itself.

    Here is the offending code (nb. thanks to Robx2 for helping with this part!):

    for (var j=1; j<20; j++) {
    thumbstrip.createEmptyMovieClip("thumbnail_"+j, j);

    var prevwidth = thumbstrip["thumbnail_"+(j-1)]._width; //should get the width of the previously loaded image
    var prevx = thumbstrip["thumbnail_"+(j-1)]._x;
    var xpos = (prevwidth+prevx+3);

    thumbstrip["thumbnail_"+j]._x= xpos; //should position next image at the right edge of the last
    thumbstrip["thumbnail_"+j]._y= -100;
    trace (prevwidth);
  2. macrumors 6502a


    hmm... I did that once... I can't for the life of me remember how I did it though :confused:. I'll get back to you once I get home, I have the code there.
  3. macrumors 6502


    Thanks, I'd appreciate that. I've been scouring various websites and the manuals I have here, but can't find any obvious way to do it. I can see how you could do it by passing variables for the image along with the file itself, but there must be a simpler way...
  4. macrumors member

    Glad that basic code lives on.

    You know, the problem might be that it looks for the width before the jpg gets through loading. You might need to put in some sort of onLoad action to make it work properly.

    Otherwise you might need to build an external xml file where you could store file names and jpeg dimensions.

  5. macrumors 6502


    Hi Rob

    The code actually looks for the width of the previously loaded thumbnail (j-1)- I understand that this won't work until the loop gets to the 2nd one, but when I trace the value of prevwidth, I get something like this:


    I'm trying to avoid having to have all my thumbnails the same size (as some will be landscapes) and also having to write an accompanying xml file to go with it...
  6. macrumors 6502a


    Sorry, I couldn't find the code, but robx2 is right. You get the wrong dimensions because the code execution is usually much faster than loading a picture. You need something like this to read the proper values:

    if (this.getBytesLoaded() >= this.getBytesTotal())
          //Read dimensions here
    I think you substitute this with the name of the movieclip, I havent used flash in a while...

    I'm not sure what's the most effective way of doing this though. There's a tutorial on loading jpg's here.
  7. macrumors 6502


    Thanks floyde

    I'll have a look at that tutorial later on today. I take the point about the jpeg not being loaded fully before the size is returned, but I'm confused because a lot of tutorials I've read say that the jpeg you load takes on the dimensions of the clip it's loaded into...

    I'll let you know what happens tomorrow
  8. macrumors newbie

    I know this is an old thread, but I am sick of reading posts that i personally want answers for and there not being one after all of that reading! Below is how you should load images and gain access to their height/width and so on... The answer is onLoadInit(); onLoadComplete will not work because all of the variables and things are not initialized until the onLoadInit(); Hope this saves someone some time!

    Use the MovieClipLoader object (with a Listener):

    var my_mcl:MovieClipLoader = new MovieClipLoader();
    var mclListener:Object = new Object();

    mclListener.onLoadProgress = function(target_mc:MovieClip,numBytesLoaded:Number,numBytesTotal:Number):Void {
    var pctLoaded:Number =Math.ceil(100*(numBytesLoaded/numBytesTotal));
    // This will get you the percent amount loaded for use in a preloader, etc...
    mclListener.onLoadInit = function(mc:MovieClip):Void {
    trace("LoadInit Rules! "+mc._width); // This is the method that will return the proper values for height/width


    my_mcl.loadClip(imageVariable, MovieClipToLoadImageInto);

Share This Page