Forum Moderators: coopster

Message Too Old, No Replies

getimagesize()

Extreeeeeemely slooooow.

         

yowza

4:22 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



Any ideas why the following code would cause the page to take about two minutes to load?

<img src="http:/*www.example.com/thumbs/$row_rsExample['thumbnail']" <?php $dims = getimagesize("http:/*www.example.com/thumbs/".str_replace(" ","%20",$row_rsExample['thumbnail'])."");?> width="<?php echo $dims[0];?>" height="111" />

As soon as I remove the getimagesize() function the page loads within seconds.

httpwebwitch

4:36 pm on Jul 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try rearranging the code like this; it may not speed it up, but will make it easier to read and debug. Watch as the page loads - the "flushes" will send the code to your browser as it works, and you can see where the hangup is occuring.

<?php
$thumbpath = "http:/*www.example.com/thumbs/";
$thumbpath .= str_replace(" ","%20",$row_rsExample['thumbnail']);

echo $thumbpath; // for debugging
flush();

$dims = getimagesize($thumbpath);
flush();

print_r($dims); // for debugging
flush();

print("<img src='".$thumbpath."'" width='".$dims[0]."' height='111'>");
flush();

yowza

5:09 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



The problem is definitely in the getimagesize() function. I tried as httpwebwitch said to use flush() and the page always hangs when getting the image dimensions. The odd thing is that now some pages don't hang at all, while others hang for 30 seconds or so.

Could the problem be with the server and their php configurations?

httpwebwitch

7:40 pm on Jul 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This link [forums.devshed.com] is a discussion of getimagesize() performance.

One good idea is to keep a database table for your image dimensions.

ID ¦ imagepath ¦ height ¦ width

here it is in pseudo-code:


function imagetag_from_db(imagepath){
if (image is in the database){
return the image tag
}else{
use getimagesize();
write them in the database;
return the image tag
}
}

print(imagetag_from_db("myimage.jpg"));

httpwebwitch

2:13 pm on Jul 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The advantage of the method above is that you will only need to use getimagesize() once, ever, for each image. After the image has been requested once, the height and width will be available henceforth in the database, so they can be retrieved really quickly. fancy, huh?

This is a "band-aid" solution; it doens't explain why getimagesize() is going so slowly for you. I've never noticed any sluggishness from getimagesize(), it's usually quite speedy.

dreamcatcher

8:50 am on Jul 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have the same problem now on my server. getimagesize() has been working fine for the last two months, but since my hosting company upgraded their servers to PHP v4.3.6 its been hanging. Once I comment out the getimagesize() function, the page loads ok.

mrsocks

2:25 am on Aug 17, 2004 (gmt 0)

10+ Year Member



has there been any updated info to this?
i am having the same problem and sporadically also.

any news or info would be great, thanks.

The Cricketer

3:02 pm on Aug 17, 2004 (gmt 0)

10+ Year Member



There is an update from yowza, but I don't think it will help you.
[webmasterworld.com ]

mrsocks

3:32 pm on Aug 17, 2004 (gmt 0)

10+ Year Member



what i think i am finding is that i was using a full url to to the image. when i removed the [www....] portion, and use a relative path it is working like a charm.

The really weird part is that it was working and then stopped...... i wonder if it has something to do with traffic along the network going out and then coming back...

we'll see though.

thanks