Csaba Gabor wrote:
Is there any way to determine the pixel
height and width of an original image?
Specifically, If I have
<IMG id=myImg src="pic.jpg" height=200 width=300>
can I figure out what the original size of pic.jpg
is (had I not placed the height and width attributes
on the image, whether in the original tag or later
via javascript)?
If the image height/width attributes have been set, then that is
what you will get - you can't reliably get the 'original' size
of the image. Below is a small test script to see what happens:
Note that Firefox's getComputedStyle is not exactly equivalent
to IE's currentStyle. Firefox always returns the size as px,
whereas IE may return 'auto' in some cases and if '%' or 'em' is
used for the width or height units, that is what IE returns.
My test image was the Google logo.
<style type="text/css">
.testImage {width: 75%; height: 75%}
</style>
<script type="text/javascript">
function getSize(a){
var msg = '';
// Check if attributes set:
if (a.height || a.width) {
msg += 'Height attribute: ' + a.height;
msg += '\nWidth attribute: ' + a.width;
}
// Check if style attributes set:
if (a.style) {
msg += '\n\nHeight style attribute: ';
msg += (a.style.height)? a.style.height : 'not set';
msg += '\nWidth style attribute: ';
msg += (a.style.width)? a.style.width : 'not set';
}
// Zilla: getComputedStyle
if (window.getComputedStyle) {
var h = document.defaultView.getComputedStyle(a,
'').getPropertyValue('height');
var w = document.defaultView.getComputedStyle(a,
'').getPropertyValue('width');
msg += '\n\nUsing getComputedStyle (h,w): ' + h + ', ' + w;
// IE: currentStyle
} else if (a.currentStyle) {
var h = eval('a.currentStyle.height');
var w = eval('a.currentStyle.width');
msg += '\n\nUsing currentStyle (h,w): ' + h + ', ' + w;
}
alert(msg);
}
</script>
<p>This image does not have the height/width
attributes defined</p>
<img src="1.gif" alt="" onclick="getSize(this);">
<p>This image has the height/width attributes
defined at full size</p>
<img src="1.gif" alt="" height="55" width="150"
onclick="getSize(this);">
<p>This image has the height/width attributes
defined at 27 x 75 using image attributes</p>
<img src="1.gif" height="27" width="75" alt=""
onclick="getSize(this);">
<p>This image has the height/width attributes
defined at 27 x 75 using style attributes</p>
<img src="1.gif" style="height: 27px; width: 75px;"
alt="" onclick="getSize(this);">
<p>This image has the height/width attributes
defined at 50% x 50% using style attributes</p>
<img src="1.gif" style="height: 50%; width: 50%;"
alt="" onclick="getSize(this);">
<p>This image has the height/width attributes
defined at 75% x 75% using CSS class</p>
<img src="1.gif" class="testImage"
alt="" onclick="getSize(this);">
--
Rob