0

I have multiple images which I want to resize to a fixed height and the width will be resized in the ratio of the resized height and i want to achieve this using jQuery. So I gave all the images the same class attribute and I ran this code:

<img src="img1.jpg" class="imgs">
<img src="img2.jpg" class="imgs">
<img src="img3.jpg" class="imgs">
<script>
$(document).ready(function(){
    $('.imgs').each(function(){
        oldH = $(this).naturalHeight;
        oldW = $(this).naturalWidth;
        divH = "500px";
        if(oldH > divH){
            newH = divH;
            calW = newH / oldH;
            newW = calW * oldW;
            $(this).naturalHeight = newH;
            $(this).naturalWidth = newW;
        } else{
            newH = oldH;
            newW = oldW;
            $(this).naturalHeight = newH;
            $(this).naturalWidth = newW;
        } 
    })  
});
</script>

But the images are not resizing, I am still new to jQuery so I am not to good. I don't know if that's the right approach to achieve this. Please if it's not I need a clue on how to achieve this.

1
  • 1
    I discovered that when I console.log(oldH, oldW); it says "undefined" so probably the problem is from this ` $('.imgs').each(function()` and if the oldH and oldW is undefined then the calculation is void Commented May 18, 2016 at 17:38

3 Answers 3

3

$(this).naturalHeight; will not work. Because naturalHeight is not a jQuery function.

You can try this

var image = new Image();
image.src = $(this).attr("src");
oldW = image.naturalWidth;
oldH = image.naturalHeight;

This approach doesn't work in IE 8 and below versions, because it doesn't support 'naturalWidth' and 'naturalHeight' properties. To achieve the same use this code

var image = new Image();
image.src = $(this).attr("src");
image.onload = function() {
oldW = this.width;
oldH = this.height;
};

Reference : Get Each Image Natural Height With jQuery

Sign up to request clarification or add additional context in comments.

1 Comment

Okay I am getting a little bit confused. Please can you @Mash show me the full code on how to run it. And I don't want to resize all the images on the page. Just the images with class of imgs and the images are been uploaded so I can't tell the actual size of the images
0
<div style="height: 100px">
<img src="random-img.jpg"
style="max-height: 100%; max-width: 100%">
</div>​

This isn't JQuery, but is the easier solution i can give you.

Try and tell me.

3 Comments

it makes no difference.
I know of that, it won't work. if the image height is 200 and the width is 200 also and the width of the div is 350px you will end up having the image size to be H=100px and W=350px. and its not rationalized. I just figured it out check my new answer
It's a good way. Although i prefer a less-code file. Good work! :)
0

I have worked on it and this is what I came out with and it works perfectly for me:

$(document).ready(function(){
    get = document.getElementsByClassName('imgs');
    for(var i=0; i < get.length; i++){
        oldH = get[i].naturalHeight;
        oldW = get[i].naturalWidth;
        divH = "500";
        if(oldH > divH){
            newH = divH;
            calW = newH / oldH;
            newW = calW * oldW; 
        get[i].height = newH;
        get[i].width = newW;
        console.log(newH, newW);
        } else {
            newH = oldH;
            newW = oldW;
        get[i].height = newH;
        get[i].width = newW;
        console.log(newH, newW);
        }   
    }
});

2 Comments

who down voted my answer, and why? I got it very correct.
Future visitors might think this is an appropriate solution, but it really isn't. You're being misled by the browser cache into thinking you have an appropriate solution. Clear your cache and try again, and you'll soon discover that this is just as broken as it was before.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.