473,326 Members | 2,076 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,326 software developers and data experts.

getting image size when loading gif in page?

I am using Ajax with a PHP script to obtain the name of gif files from
a MySQL database. Javascript then embeds the actual file in the web
page, thus displaying the image on the fly. For precise positioning, I
would like to know the gif image size. Is there a way to obtain it
using Javascript or otherwise?
(I know the size is indeed contained in the gif format, as I can see it
with a binary editor...)
Thanks!

Andrew

Nov 4 '06 #1
2 4731
ab****@yahoo.com wrote:
I am using Ajax with a PHP script to obtain the name of gif files from
a MySQL database. Javascript then embeds the actual file in the web
page, thus displaying the image on the fly. For precise positioning, I
would like to know the gif image size. Is there a way to obtain it
using Javascript or otherwise?
PHP should be able to read out the image size directly on the server.
Client-side JavaScript can give you the pixel size once the image has
been completely loaded e.g.
var img = new Image();
img.onload = function () {
alert(this.width + 'x' + this.height);
};
img.src = 'whatever.gif';

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 4 '06 #2
ASM
ab****@yahoo.com a écrit :
I am using Ajax with a PHP script to obtain the name of gif files from
a MySQL database. Javascript then embeds the actual file in the web
page, thus displaying the image on the fly. For precise positioning, I
would like to know the gif image size. Is there a way to obtain it
using Javascript or otherwise?
first :
if your styles sheet is well made
you have no use of any placement or size ...

but,
size of image can be obtained via PHP (better) or via JS
In JS simplest way is :
-----------------------

<img src="1.jpg" alt="photo" title="" name="i_1" id="i_1"
onload="alert('width = '+this.width+'\nheight = '+this.height)" />

<a href="#" onclick="document.i_1.src = '2.jpg';">photo 2</a>
Example to know (in image's title) width and height and refresh that
information on each change of images

<script type="text/javascript">
function setImg(id) {
document.getElementById(id).onload=function(){getI mage(this)}
}
function getImage(imag) { //
imag.title=imag.width+','+imag.height;
}
onload = function() {
var I = document.images;
for(var i=0; i<I.length; i++) {
I[i].title=I[i].width+','+I[i].height;
setImg(I[i].id);
}
}
</script>
In PHP it is yet easier :
-------------------------

<?php
list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
echo "<img src=\"img/flag.jpg\" alt=\"photo\" title=\"$attr />";
?>

--
ASM
Nov 4 '06 #3

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

Similar topics

4
by: Kenny | last post by:
I have been trying to write a script that will increase the size of and image when you mouse over it, and decrease it to original size, when you mouse out. After a couple of attempts, this is what...
1
by: Antonie C Malan Snr | last post by:
Dear All, I tried posting this before, but it seems to have disappeared. I need to check image sizes before the user uploads them. This works in the Mozilla family on second and subsequent...
4
by: dgk | last post by:
I have an app that has many subdirectories containing one or more images (jpg, gif, bmp). When the user selects a directory, my plan is to show the images if there are only one or two, or show...
10
by: abcd | last post by:
I have PIL 1.1.5 on python 2.4.1 and I am attempting to get a smaller (file size) of an image. for example: im = ImageGrab.grab() im.save("tmp.gif") ---- about 1.7mb im.save("tmp.jpeg") ...
13
by: Ari Krupnik | last post by:
Image.width returns the width of an image as it is displayed on a browser page, which may be different from the image's intrinsic size if the 'img' element had a 'width' attribute specified. Is...
2
by: bizt | last post by:
Hi, Is it possible to obtain the width/ height of an image when that image is dyanically created using a PHP script and passing GET attributes. For example: <img...
5
by: Emmi | last post by:
Hi there, just starting in PHP and found some code that would switch out pages depending on link clicked. It worked great loading different pages. Then I thought I would try the same code by...
6
by: sarika | last post by:
Hi All i m making a web site having number of big images. My requirement is i want to dunamically split the images while downloading so that my website works fast and image downloading does not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.