Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Image Resize

Does anyone know how to resize images proportionally using JavaScript?

I have tried to modify the DOM by adding attributes height and width on the fly, but seems did not work on IE6.

like image 967
Komang Avatar asked Oct 04 '08 16:10

Komang


People also ask

How do you enlarge an image in JavaScript?

Answer: Use the JavaScript width and height property You can use either width or height JavaScript property to proportionally increase and decrease the dimension of an image like zoom-in and zoom-out feature.

How do you resize an image in HTML?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.

How do I reduce the size of an image in canvas?

Right-click on the image and select Resize Image.... After selecting Resize Image... a dialog will appear that gives you precise control over the size of the image.


2 Answers

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.

like image 97
Dan Avatar answered Sep 24 '22 18:09

Dan


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

like image 29
Komang Avatar answered Sep 23 '22 18:09

Komang