Get Image Size After Resize JavaScript
I am trying to resize an image based off of a max-width/max-height array. Here's what I've come up with so far: Image resize test
Solution 1:
Instead of setting width/height you can try to set image.style.maxWidth/image.style.maxHeight then image will resize with preserving aspect ratio.
Solution 2:
You didn't change the height yet, because your image's height > width, make portrait = false;
When you change the width, in IE8, it will only resize the width, but in chrome it will both change the width and height, I think this is browser depended.
You can fix this by both change the height and with manually.
Solution 3:
IE doesnt recognize addEvenetListener and you must handle it your self based on brower.. possible solution
<html>
<head>
<title>Image resize test</title>
<script>
function createImage(){
//The max-width/max-height
var maxDim = [500,500];
//Create the image
var img = document.createElement("img");
img.setAttribute("src","http://nyrixcorp.com/images/eggplant_service.png");
document.body.appendChild(img);
//Function for resizing an image
function resize(){
//resize the image
var portrait = this.width < this.height;
this[portrait ? "height" : "width"] = maxDim[portrait ? 1 : 0];
//What is the height? PROBLEM HERE!!!
console.log(this.height);
}
//Is the image loaded already?
var temp = new Image();
temp.src = img.src;
var loaded = typeof temp.complete != "undefined" ? temp.complete : !isNaN(temp.width+temp.height) && temp.width+temp.height != 0;
//Fire the resize function
if (loaded) resize.call(img);
else{
if(isIE == true) // check here for browser
img.onload = resize;
else
img.addEventListener("load",resize,false);}
}
</script>
</head>
<body onload="createImage()">
</body>
</html>
it will work fine just u need to pass true or false to isIE based on the browser you are using
Post a Comment for "Get Image Size After Resize JavaScript"