/    Sign up×
Community /Pin to ProfileBookmark

Image scaling in a slide-show

Hi,

I have a small application which gets an array of URLs of pictures of a user. My application then shows these pictures in a 120px X 120px slide-show in a random order. Since the pictures are going to be different resolutions (And I don’t have control over that), I need to correctly scale the images to fit inside the box. This is the best logic that I could come up with:

[CODE]
<div id=”apDiv2″ align=”center”><img id=”apOwnerPic” style=”border:2px solid red; max-width:120px; max-height:120px” src=”pic1.png” alt=”Unknown name” /></div>

function littleAnimator() {
var rand_no = Math.floor(picsArray.length*Math.random());
var theImg = document.getElementById(“apOwnerPic”);
scaleMainImage(theImg, picsArray[rand_no], ownerName);
}

function scaleMainImage(img, src, alt)
{
if(img.src == src) return;
img.vspace = 0;
img.style.visibility = “hidden”; // Hide the image and show it only after all changes are done
img.src = src;
img.alt = alt;
var boundaryX = 120;
var boundaryY = 120;
var imageX = img.width;
var imageY = img.height;
if ((img != null && img.width > 0 && img.height > 0) && (img.width > boundaryX || img.height > boundaryY))
{
var scaleX = boundaryX / imageX;
var scaleY = boundaryY / imageY;

if (scaleX < scaleY)
{
imageX = imageX * scaleX;
imageY = imageY * scaleX;
}
else
{
imageX = imageX * scaleY;
imageY = imageY * scaleY;
}
img.style.width = imageX;
img.style.height = imageY;
}
img.vspace = Math.floor(((boundaryY-imageY)/2));
img.style.visibility = “visible”;
}[/CODE]

I see two problems here.

  • 1. In Firefox, sometimes the “next” image takes up the vspace of the “previous” image (although I force a vspace=0). Then in the next cycle, the vspace gets set correctly. This does not happen every time.

  • 2. (This looks terrible) In IE, sometimes the “next” image shows up in its full size. Then in the next cycle, it shows up correctly. This does not happen every time.
  • I am a noob in the HTML/Javascript world .. so maybe this problem can be fixed very easily. Oh, and I don’t have the option of importing other Javascript files. So all logic needs to reside in this one file.

    to post a comment
    JavaScript

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @gil_davisMar 24.2008 — When you change the src of the img, the width and height are unknown until the img has loaded. You are not waiting for the image to finish loading before you look at the height and width. Fortunately there is an onload event for images that you can use, but you will have to split up your function. Basically you need to have the "change the source" part separate from the "change the size" part. It might get tricky, but you should be able to use the "this" operator to reference the image in the onload function.
    Copy linkTweet thisAlerts:
    @mNeoauthorMar 24.2008 — I split the function and started calling the "scaling" part from onload. Now it shows up correctly (at least so far). Thank you so much!
    ×

    Success!

    Help @mNeo spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...