Wednesday, 13 December 2017

javascript - Getting image width on image load fails on IE

itemprop="text">


I have a image resizer
function that resize images proportional. On every image load a call this function with
image and resize if its width or height is bigger than my max width and max height. I
can get img.width and img.height in FF Chrome Opera Safari but IE fails. How can i
handle this?



Let me explain with a piece of
code.




src="images/img01.png" onload="window.onImageLoad(this, 120, 120)"
/>

function onImageLoad(img, maxWidth, maxHeight) {

var width = img.width; // Problem is in
here

var height = img.height
// Problem is in
here
}


In my highligted
lines img.width don't work on IE series.



Any
suggestion?



Thanks.



Answer





Don't use
width and height. Use
naturalWidth and naturalHeight
instead. These provide the image unscaled pixel dimensions from the image file and will
work across browsers.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...