The following code works:
var image = {width:0,height:0};
var imageObject = new Image();
function getImageSize(id) {
imageObject.src = id.attr("src");
image.width = imageObject.width;
image.height = imageObject.height;
}
I want to get the original width and height of an image and tried different code to achieve it. My function triggers on click event and all images are resized to 400x300.
I tried to resize the Image to its original size by using auto
:
var image = {width:0,heigth:0};
function getImageSize(id) {
id.css({
'width': 'auto',
'heigth': 'auto'
});
image.width = id.attr('width');
image.heigth = id.attr('heigth');
}
Attaching auto
didn't change the size of the image.
alert(id.attr('width'));
always returns 'undefined'. so it doesn't work
var image = {width:0,heigth:0};
var imageObject = new Image();
function getImageSize(id) {
imageObject.src = id.attr("src");
image.width = imageObject.width;
image.heigth = imageObject.heigth;
}
alert(image.width);
works perfectly.
alert(image.heigth);
returns undefined.
Using imageObject.naturalWidth
works as the one above - the width is given correctly but the heigth is undefined.
I read that you have to append the new image object, but when I use imageObject.appendTo('body');
I can't even click the image.
Thanks for any suggestions.
Right click the image, then click Properties.... Clear the Width and Height fields, then click OK. The image will automatically revert back to its original size.
Go to images.google.com and enter the search terms as before. Then append imagesize:WIDTHxHEIGHT to your query and hit Enter. Google Images will remove the operator from the query but the results will only display images that match the specified size.
The origin of standard photo print sizes lies with film photography where a 35 mm film was used to produce negatives with a 3:2 width to height ratio. This format is still widely used in digital cameras today. All other print sizes developed out of the original standard size of 3:2.
This is a simple contained example of replacing an image with one that has its original sizes after having been altered.
<img src="http://www.finesttreeserviceaz.com/wp-content/uploads/2012/02/tree2.gif" id="bar" />
<script type="text/javascript">
var myImg = document.getElementById("bar");
myImg.setAttribute("style", "width:400px;height:300px;");
function resetBar() {
var newImg = new Image();
var oldImage = document.getElementById("bar");
newImg.src = oldImage.src;
oldImage.parentNode.replaceChild(newImg, oldImage);
}
setTimeout("resetBar();", 2000);
</script>
An alternative would be to remove the style that was applied:
myImg.removeAttribute("style");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With