473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

how to find image dimensions?

Is there a way in javascript to find out the size of an image you've just
preloaded?

I have the following object:

function tourImage(imgName) {
// used for passing image details to a function.
this.name=imgName;
//this.imgLoadImg = document.getElementById("imgLoadImg");
function preload(callback) {
this.img=new Image();
this.img.src=this.name;
// call tour.showPano when the image has loaded.
this.img.onLoad=this.runCallback(callback);
};
this.preload=preload;
function runCallback(cb) {
// first do what's needed for every image.
// puit this image into the hidden image container to find the size.
//this.imgLoadImg.src=this.name;
//this.width=this.imgLoadImg.width;
//this.height=this.imgLoadImg.height;
//this.imgLoadImg.src="";
this.height=tour.resolution;
*** // TODO - fix this so it finds the image size automatically.
this.width=tour.resolution*6.889;
this.ratio=this.width / this.height;
// then call the callback function.
cb();
}
this.runCallback=runCallback;
};

what I want to do is fix this so it finds out the size of the just-loaded
image by itself, so that the true image dimensions can be used to scale
the image properly.

I've tried using this.img.width; I've also tried loading the image into a
dummy img tag with no width and height properties to see if I can get the
details that way, but that doesn't work either.

can anyone suggest a good way of doing this?

--
http://www.niftybits.ukfsn.org/

remove 'n-u-l-l' to email me. html mail or attachments will go in the spam
bin unless notified with [html] or [attachment] in the subject line.

May 27 '06 #1
2 5539
I tested .width and .height and it works perfectly.
http://dttvb.yi.org/imgsize.html
Tested and works in IE and Firefox.

function getImageSizePreload(src) {
var theImageObject = new Image();
theImageObject.onload = function () {
// When loaded, get its size by using
// theImageObject.width and theImageObject.height
};
theImageObject.src = src;
// We set the src after onload because of IE's caching problem.
}

May 28 '06 #2
the DtTvB said:
I tested .width and .height and it works perfectly.
http://dttvb.yi.org/imgsize.html
Tested and works in IE and Firefox.

function getImageSizePreload(src) {
var theImageObject = new Image();
theImageObject.onload = function () {
// When loaded, get its size by using
// theImageObject.width and theImageObject.height
};
theImageObject.src = src;
// We set the src after onload because of IE's caching problem.
}


OK, sorry - I thought I had tested that.

--
http://www.niftybits.ukfsn.org/

remove 'n-u-l-l' to email me. html mail or attachments will go in the spam
bin unless notified with [html] or [attachment] in the subject line.

May 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
8
by: Abs | last post by:
Hi! I have an image inside a DIV box with its dimensions specified. The problem is that I don't know the dimensions of the image beforehand, therefore the image grows outside the box if it's...
5
by: John | last post by:
I am rotating images at one location of my web site. My problem is if I set the width and height of the new image before I show the new image, the old image is stretched first to the new image...
1
by: John | last post by:
I am rotating images of different dimensions. My problem is that when a new image is displayed in a new position which had an image of a different dimension, the old image is first stretched to the...
14
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
6
by: MurrayTh | last post by:
Is there any way to determine the dimensions of an image using the image's URL? ie load image based on URL, and then get dimensions? or perhaps better method?
3
by: Andy Baxter | last post by:
Is there a way in javascript to find out the size of an image you've just preloaded? I have the following object: function tourImage(imgName) { // used for passing image details to a...
1
by: johnrees | last post by:
I am building a photo gallery using code which creates thumbnails of a specific size from any image. When clicked the thumbnail expands dynamically to a pre-determined size. The sizes of the...
2
by: Adam Teale | last post by:
hey guys Is there a builtin/standard install method in python for retrieving or finding out an image's dimensions? A quick google found me this:...
6
by: Samuel | last post by:
Hi I use the image controls to show images but it distorts the image if the image dimensions are different to the image itself Thank you, Samuel
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.