galvin Posted January 27, 2012 Share Posted January 27, 2012 I have this code below in a while loop, that gets looped through about 10 times and is simply resizing a slightly larger image to fit within a 50px max square. There is other code on my page, but I've narrowed the slowness down to this specific bit of code (i.e. if I take this snippet out, the page loads instantly). With this snippet, the page takes between 5 and 10 seconds to load, which seems absurd for 10 images. FYI, the original images are no larger than 200 px on either side, so it's not like it's looping through large image files. Anyone know why this might be taking so long? Should I be doing it differently somehow? list($width,$height) = getimagesize($myimage); if ($width > $height) { $datasofar .= "width=50 /></span>"; } elseif ($height > $width) { $datasofar .= "height=50 /></span>"; } else { //height and width must be equal so just set width = 50, but could just as easily set height to 50 $datasofar .= "width=50 /></span>"; } Quote Link to comment https://forums.phpfreaks.com/topic/255894-reason-for-slow-loading-images/ Share on other sites More sharing options...
php.ajax.coder Posted January 27, 2012 Share Posted January 27, 2012 I would think it is php working out the size of the image on the following line list($width,$height) = getimagesize($myimage); you could try setting the $width and $height to a constant here to see if it is indeed slowing down your script Not sure this really helps because I'm not sure of any other way of getting the size of the image dynamically. Quote Link to comment https://forums.phpfreaks.com/topic/255894-reason-for-slow-loading-images/#findComment-1311790 Share on other sites More sharing options...
galvin Posted January 27, 2012 Author Share Posted January 27, 2012 I commented out that line and added... $width=90; $height=110; It ran a little quicker (like 3 full seconds instead of 5+, which still seems long to me, but maybe I don't know what I'm talking about and this amount of time is perfectly normal Not sure if it matters, but the images that are being checked are NOT on my server, they are image files on my Amazon S3 account, so they are external image URLs. Maybe it takes significantly longer to check external image URLs??? Quote Link to comment https://forums.phpfreaks.com/topic/255894-reason-for-slow-loading-images/#findComment-1311792 Share on other sites More sharing options...
scootstah Posted January 27, 2012 Share Posted January 27, 2012 GD functions are just intense, they take a little while. It's best to make it in such a way that they need not be run often. For example don't resize pictures on every page load, resize them once and then save them in the resized state. Quote Link to comment https://forums.phpfreaks.com/topic/255894-reason-for-slow-loading-images/#findComment-1311797 Share on other sites More sharing options...
galvin Posted January 27, 2012 Author Share Posted January 27, 2012 Damn, I was afraid that was answer (i.e. that's just the way those functions are, they take longer). Thanks for the tip though, I'll see if I can only have them load once. And I guess this would be a good time to look at adding some sort of spinning "working" gif so the user knows something is happening, rather than them wondering what's taking so long and recklessly clicking the link over and over Quote Link to comment https://forums.phpfreaks.com/topic/255894-reason-for-slow-loading-images/#findComment-1311801 Share on other sites More sharing options...
scootstah Posted January 27, 2012 Share Posted January 27, 2012 A spinner gif is only going to work if you're running it through AJAX or something. Quote Link to comment https://forums.phpfreaks.com/topic/255894-reason-for-slow-loading-images/#findComment-1311802 Share on other sites More sharing options...
galvin Posted January 27, 2012 Author Share Posted January 27, 2012 Hmmm, i never implemented a spinning gif so I assumed it was simple to make an image show while a page is processing. Guess I was wrong Quote Link to comment https://forums.phpfreaks.com/topic/255894-reason-for-slow-loading-images/#findComment-1311804 Share on other sites More sharing options...
scootstah Posted January 27, 2012 Share Posted January 27, 2012 You could, but once you send it to the browser you can't take it away again when PHP is done processing. PHP doesn't have any way to tell you that it is done in the first place. Where are the images coming from to start with? Quote Link to comment https://forums.phpfreaks.com/topic/255894-reason-for-slow-loading-images/#findComment-1311808 Share on other sites More sharing options...
galvin Posted January 27, 2012 Author Share Posted January 27, 2012 They are all in my Amazon S3 account, so they are coming from Amazon's servers Quote Link to comment https://forums.phpfreaks.com/topic/255894-reason-for-slow-loading-images/#findComment-1311810 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.