1

I'm trying to get all image elements on a html page then check each image if the width < height (portrait) then rotate it.

The problem is my script doesn't work for each image but for all of them because i get the images by "img" tag, then i have no idea how to apply the rotation for individual images.

Is it possible to do this without having to modify the markup for each images (I want to avoid putting ids for every image)?

<script>
$(document).ready(function() {
    var imgs = document.getElementsByTagName("img");

    for (var i = 0; i < imgs.length; i++) {
        img = document.getElementsByTagName("img")[i];
        var width = img.clientWidth;
        var height = img.clientHeight;
        if(width<height){
            img.setAttribute('style','transform:rotate(90deg)'); //how do I apply this for each image?
        }
        else{
        }
    }
});
</script>
Code Maniac
  • 37,143
  • 5
  • 39
  • 60

3 Answers3

5

Your code seems to be working for images of the correct dimensions. However, you can change your code a little. For the code that you've given you don't need to do:

img = document.getElementsByTagName("img")[i];

in your for loop at each iteration, as this is an expensive function to run. Instead, you already have the result of document.getElementsByTagName("img"); stored in imgs, so you can use that instead:

img = imgs[i];

See working example below:

$(document).ready(function() {
  var imgs = document.getElementsByTagName("img");
  var imgSrcs = [];

  for (var i = 0; i < imgs.length; i++) {
    img = imgs[i];
    var width = img.clientWidth;
    var height = img.clientHeight;
    if (width < height) {
      img.setAttribute('style', 'transform:rotate(90deg)'); 
    }
  };
});
img {
  height: 150px;
  width: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Width &gt; Height</p>
<img src="https://media.wired.com/photos/5926db217034dc5f91becd6b/master/w_1904,c_limit/so-logo-s.jpg" />

<p>Width &lt; Height</p>
<img src="https://d1qb2nb5cznatu.cloudfront.net/startups/i/32728-274244db60c65e1cc32abb4c54a2c582-medium_jpg.jpg?buster=1442602512" />

<p>Width &lt; Height</p>
<img src="https://images.unsplash.com/photo-1522092787785-60123fde65c4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=94d6ebf03fdc6a3c8159ac9aeceb0483&w=1000&q=80" />

Also, since your using jQuery, this can be done even easier using jQuery methods:

$(document).ready(function() {
  $('img').each((_, elem) => {
    let width = $(elem).width();
    let height = $(elem).height();
    if(width < height) {
      $(elem).css({'transform': 'rotate(90deg)'});
    }
  });
});
img {
  height: 150px;
  width: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Width &gt; Height</p>
<img src="https://media.wired.com/photos/5926db217034dc5f91becd6b/master/w_1904,c_limit/so-logo-s.jpg" />

<p>Width &lt; Height</p>
<img src="https://d1qb2nb5cznatu.cloudfront.net/startups/i/32728-274244db60c65e1cc32abb4c54a2c582-medium_jpg.jpg?buster=1442602512" />

<p>Width &lt; Height</p>
<img src="https://images.unsplash.com/photo-1522092787785-60123fde65c4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=94d6ebf03fdc6a3c8159ac9aeceb0483&w=1000&q=80" />
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
2

I tried the code provided and on many occasion (reload for instance) it doesn't get the width and height of the image.

It seems that instead of using

$(document).ready(fn)

you should use

$(window).on("load", fn)

There was a question on this topic Official way to ask jQuery wait for all images to load before executing something

Eponyme Web
  • 966
  • 5
  • 10
0
<script>
$(document).ready(function() {
    var imgs = document.getElementsByTagName("img");
    var imgSrcs = [];

    for (var i = 0; i < imgs.length; i++) {
        var img = document.getElementsByTagName("img");//add all images in array
        var width = img[i].clientWidth;
        var height = img[i].clientHeight;
        if(width<height){
            img[i].setAttribute('style','transform:rotate(90deg)'); //change style for current image
        }
        else{
        }
    }
});
</script>
Theraot
  • 31,890
  • 5
  • 57
  • 86