Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the original size of an image -Jquery

SOLVED: Syntax-Error

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;
}

Original Question

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.

like image 229
CadanoX Avatar asked Sep 03 '12 22:09

CadanoX


People also ask

How do I return a picture to its original size?

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.

How do you find the exact size of an image?

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.

What is the original photo 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.


1 Answers

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");
like image 111
Travis J Avatar answered Nov 04 '22 07:11

Travis J