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.

KANAYO AUGUSTIN UG
  • 2,078
  • 3
  • 17
  • 31
  • 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 – KANAYO AUGUSTIN UG May 18 '16 at 17:38

3 Answers3

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

Community
  • 1
  • 1
Dezefy
  • 2,048
  • 1
  • 14
  • 23
  • 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 – KANAYO AUGUSTIN UG May 18 '16 at 18:57
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.

Thomas Galue
  • 160
  • 9
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);
        }   
    }
});
KANAYO AUGUSTIN UG
  • 2,078
  • 3
  • 17
  • 31
  • who down voted my answer, and why? I got it very correct. – KANAYO AUGUSTIN UG May 18 '16 at 18:15
  • 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. – zzzzBov May 18 '16 at 18:19