does anyone knows how to resize image proportionaly using javascript?, i have tried to modify the DOM adding attributes height & Width on the fly, but seems did not work on IE6
Instead of modifying the height and width attributes of the image, try modifying the CSS height and width.
myimg = document.getElementById('myimg');
myimg.style.height = "50px";
myimg.style.width = "50px";
One common "gotcha" is that the height and width styles are strings that include a unit, like "px" in the example above.
Edit - I think that setting the height and width directly instead of using style.height and style.width should work. It would also have the advantage of already having the original dimensions. Can you post a bit of your code? Are you sure you're in standards mode instead of quirks mode?
This should work:
myimg = document.getElementById('myimg');
myimg.height = myimg.height * 2;
myimg.width = myimg.width * 2;
To modify an image proportionally, simply only alter one of the width/height css properties, leave the other set to auto.
image.style.width = '50%'
image.style.height = 'auto'
This will ensure that its aspect ratio remains the same.
Bear in mind that browsers tend to suck at resizing images nicely - you'll probably find that your resized image looks horrible.
Tried the following code, worked OK on IE6 on WinXP Pro SP3.
function Resize(imgId)
{
var img = document.getElementById(imgId);
var w = img.width, h = img.height;
w /= 2; h /= 2;
img.width = w; img.height = h;
}
Also OK in FF3 and Opera 9.26.
okay it solved, here is my final code
if($(this).width() > $(this).height()) {
$(this).css('width',MaxPreviewDimension+'px');
$(this).css('height','auto');
} else {
$(this).css('height',MaxPreviewDimension+'px');
$(this).css('width','auto');
}
Thanks guys
function resize_image(image, w, h) {
if (typeof(image) != 'object') image = document.getElementById(image);
if (w == null || w == undefined)
w = (h / image.clientHeight) * image.clientWidth;
if (h == null || h == undefined)
h = (w / image.clientWidth) * image.clientHeight;
image.style['height'] = h + 'px';
image.style['width'] = w + 'px';
return;
}
just pass it either an img DOM element, or the id of an image element, and the new width and height.
or you can pass it either just the width or just the height (if just the height, then pass the width as null or undefined) and it will resize keeping aspect ratio
Use JQuery
`
var scale=0.5;
minWidth=50;
minHeight=100;
if($("#id img").width()*scale>minWidth && $("#id img").height()*scale >minHeight) {
$("#id img").width($("#id img").width()*scale);
$("#id img").height($("#id img").height()*scale);
} `