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>functioncreateImage(){
//The max-width/max-heightvar maxDim = [500,500];
//Create the imagevar img = document.createElement("img");
img.setAttribute("src","http://nyrixcorp.com/images/eggplant_service.png");
document.body.appendChild(img);
//Function for resizing an imagefunctionresize(){
//resize the imagevar 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 = newImage();
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 functionif (loaded) resize.call(img);
else{
if(isIE == true) // check here for browser
img.onload = resize;
else
img.addEventListener("load",resize,false);}
}
</script></head><bodyonload="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"