3a09c0954a8ee7951398becbb5553f37

As a noob in js I ask help for a image's resize function

function redim_img(apodimg)
{
    var apodimg  = document.getElementById("apodimg");
    var w=apodimg.width;
    var h=apodimg.height;
    var calc
    if (w>=h && w>=200)
	{
	apodimg.style.width="200px";
	calc=h*200/w;
	apodimg.style.height=calc+"px";
	}
    if (h>w && h>=160)
	{
	apodimg.style.height="200px";
	calc=w*200/h;
	apodimg.style.width=calc+"px";
	}
}

Refactorings

No refactoring yet !

5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

January 19, 2010, January 19, 2010 20:57, permalink

No rating. Login to rate!

The code is quite good to me.
Just some glitches to be corrected:
-- var apodimg = document.getElementById("apodimg");
you use an hardcoded string in place of the parameters you passed to the function, I think it was an oversight.
-- your calculations need to be rounded as width and height properties need integer values.
-- I introduced a test on the type of the input parameter in order to let you choice to pass
an element id or a DOM element.

function redim_img(apodimg)
{
   // you can pass a string with the id of the
   // image you want to resize or a reference 
   // to the image
   if (typeof(apodimg) == "string") {
    apodimg  = document.getElementById(apodimg);
   }

    var w=apodimg.width;
    var h=apodimg.height;

    if (w>=h && w>=200)	{
       h=Math.round(h*200/w);
       w=200;
    }
    if (h>w && h>=160) {
       w=Math.round(w*200/h);
       h=160;
    }

    apodimg.style.width=w+'px';
    apodimg.style.height=h+'px';
}
5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

January 19, 2010, January 19, 2010 20:58, permalink

No rating. Login to rate!

The code is quite good to me.
Just some glitches to be corrected:
-- var apodimg = document.getElementById("apodimg");
you use an hardcoded string in place of the parameters you passed to the function, I think it was an oversight.
-- your calculations need to be rounded as width and height properties need integer values.
-- I introduced a test on the type of the input parameter in order to let you choice to pass
an element id or a DOM element.

function redim_img(apodimg)
{
   // you can pass a string with the id of the
   // image you want to resize or a reference 
   // to the image
   if (typeof(apodimg) == "string") {
    apodimg  = document.getElementById(apodimg);
   }

    var w=apodimg.width;
    var h=apodimg.height;

    if (w>=h && w>=200)	{
       h=Math.round(h*200/w);
       w=200;
    }
    if (h>w && h>=160) {
       w=Math.round(w*200/h);
       h=160;
    }

    apodimg.style.width=w+'px';
    apodimg.style.height=h+'px';
}
3a09c0954a8ee7951398becbb5553f37

mr6686

January 19, 2010, January 19, 2010 22:06, permalink

No rating. Login to rate!

Sorry but it doesn't work much
P.S.:change max height

function redim_img(apodimg)
{
   if (typeof(apodimg) == "string") {
    apodimg  = document.getElementById("apodimg");
   }

    var w=apodimg.width;
    var h=apodimg.height;

    if (w>=h && w>=200)	{
       h=Math.round(h*200/w);
       w=200;
    }
    if (h>w && h>=200) {
       w=Math.round(w*200/h);
       h=200;
    }

    apodimg.style.width=w+"px";
    apodimg.style.height=h+"px";
}
5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

January 19, 2010, January 19, 2010 23:32, permalink

No rating. Login to rate!

How call you the method?
Why do you use getElementById("apodimg") with the "" instead that
getElementById(apodimg)?
Have your image id="apodimg"?
I've tested the function with firebug on this page (redim_img(document.getElementsByTagName("img")[2])) and it worked well (after reverting the max size to 50px)

3a09c0954a8ee7951398becbb5553f37

mr6686

January 20, 2010, January 20, 2010 00:03, permalink

No rating. Login to rate!

Yes my image have id="apodimg"

3a09c0954a8ee7951398becbb5553f37

mr6686

January 20, 2010, January 20, 2010 00:06, permalink

No rating. Login to rate!

Without the "" doesn't work too

5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

January 21, 2010, January 21, 2010 12:58, permalink

No rating. Login to rate!

How is the function misbehaving?

3a09c0954a8ee7951398becbb5553f37

mr6686

January 21, 2010, January 21, 2010 16:07, permalink

No rating. Login to rate!

In fact this is part of a windows gadget which get back images from a dll and I think that is what that bugs

3a09c0954a8ee7951398becbb5553f37

mr6686

January 25, 2010, January 25, 2010 16:10, permalink

No rating. Login to rate!

Image resizes with a onload event but always 200*100 in size

Your refactoring





Format Copy from initial code

or Cancel