1

I just want to know, can I detect attributes of an image before displaying it on the web? In my case, I have a thousand photos. I want to display them on my web page but some photos have a different orientation (like Landscape and Portrait).

Can I detect the width and height of the image before it is displayed on my web page?

Maybe using JavaScript like:

If height>width than rotate
else just display.

Is there any JavaScript function for doing that? Just for getting the attribute not displaying it. Is there a trick?

2 Answers 2

1

You have to wait until it is loaded.

setInterval(function () {
   if(loaded)
   {
      clearInterval(wait);
   }
   else
   {
      log(img.width, 'x', img.height);
   }
}, 0);

I found an Example here.

Hope it helps.

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

Comments

1

When your page is loading, you can control each <img/>

//function rotation
jQuery.fn.rotate = function(degrees) {
    $(this).css({'-webkit-transform' : 'rotate('+ degrees +'deg)',
                 '-moz-transform' : 'rotate('+ degrees +'deg)',
                 '-ms-transform' : 'rotate('+ degrees +'deg)',
                 'transform' : 'rotate('+ degrees +'deg)'});
};

// When your page is loading
$( window ).load(function() {
    // Each image, you can change selector
    $('img').each(function(){
        if(this.height() < this.width()){ // Size control
            $(this).rotate(90); // rotation function call with the angle in degrees
        }
    });
});

5 Comments

$(window).load is after all the images are loaded not when it is loading. This will work but after all the images are loaded.
I doubt the speed in case of loading a thousand photo as mentioned in the question.
Yes, but it's very fast, you can use animate() to create something more beautiful. You can also add display:none or visibility:hidden on all your img. And change this displaying in the each()
sorry i am still newbie, in your answer, are you using jquery external function?
.load(), .each(), .height(), .width() are native function in jQuery. rotate is a function that I create to rotate an element and which is supported in many browser.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.