PHP getimagesize & embed - is image downloading twice?

Associate
Joined
17 Apr 2006
Posts
549
Location
Staffordshire
As the subject says. If the image is downloading twice from the server, is there a way to make it download once? The bandwidth use from the site is higher than I expected. Here's the code I'm using:

PHP:
list($width, $height) = getimagesize("http://www.example.com/image/" . $row['image']);

echo "<img src=\"http://www.example.com/image/" . $row['image'] . "\" width=\"" .$width ."\" height=\"" .$height ."\" />";

I'm a PHP newbie.

Cheers. :)
 
Soldato
Joined
3 Jun 2005
Posts
3,119
Location
The South
If the images are stored locally to the PHP then use a local filename instead of remote for the getimagesize() function.
However, if they're not then i'd be inclined to scrap adding dimensions to the image tags unless there is a particular reason for doing so ('layout shift' during page loading is the obvious).
 
Last edited:
Associate
Joined
7 Apr 2012
Posts
2,101
Location
Tampa Bay
As that code stands, yes it's doubling bandwidth.

Firstly PHP is downloading the image.
Secondly the end-user is downloading the image.

If it's important to know the height/width for display purposes, then try store that information so it's only fetched once (even if it's a local image). Otherwise just output the <img /> tag without the height/width and ditch getimagesize.
 
Associate
OP
Joined
17 Apr 2006
Posts
549
Location
Staffordshire
Thanks. Some of the images are stored on Amazon S3 so I think I'll add an extra field to my table for the dimensions.

I hoped getimagesize would be able to access the dimensions without downloading the file.
 
Back
Top Bottom