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 !
Eineki
January 19, 2010, January 19, 2010 20:57, permalink
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';
}
Eineki
January 19, 2010, January 19, 2010 20:58, permalink
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';
}
mr6686
January 19, 2010, January 19, 2010 22:06, permalink
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";
}
Eineki
January 19, 2010, January 19, 2010 23:32, permalink
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)
As a noob in js I ask help for a image's resize function