PHP - loading images - best practices

Soldato
Joined
1 Feb 2006
Posts
8,188
Hi,

I have a site where I have a number of folders containing images. For each folder I want to display a random image on the homepage.

I have got it working but I am not sure if it is done in the best way possible.
Basically I load all the image file names in the folder into an array, pick one at random and then output img tags for the random image. Is there any better way of doing this?

Each time I load up the page the images seem to always take a bit longer to download than id like. This could just be down to the image size but they are all thumbnails so it shouldn't be an issue.

Is there any way that the list of images could be cached on the server and then a random one picked each time? I'm thinking maybe the delay for images loading is due to the processing involved to pick an image but I could be wrong.

I'm happy with the script I have but if it could be done in a better way i'd be keen to know.

I'm also looking to build a search function to search images by descriptions etc. Again would this be best to load all images with matching descriptions into an array and then output the img tags as necessary?

Sorry if this isn't explained well but would appreciate any input from the php pros on this!

Thanks
 
I'm no php expert yet, but what I'm thinking is that if you are having to read all the image files to ascertain their names, then it is going to be relatively slow.

I don't know if this is feasible but normally when I am working with images I just number them rather than giving them a name. If you new what number ranges would be in each folder, or use the same number range in each folder, things could be a lot faster because then you could just choose a random number and load the appropriate image.

Rgds
 
It doesn't really matter how you're getting the filenames really, virtually anything is going to be fast. Nothing is cached when you do an `ls`, and think how stupidly fast that is.

if you are having to read all the image files to ascertain their names, then it is going to be relatively slow.

You don't, don't worry :)
 
Sic said:
depends how you're loading the images into an array - I take it you're using glob()?

Hmm, nope I just use the array keyword. Is that different from glob()?

Radderfire said:
I'm no php expert yet, but what I'm thinking is that if you are having to read all the image files to ascertain their names, then it is going to be relatively slow.

I don't know if this is feasible but normally when I am working with images I just number them rather than giving them a name. If you new what number ranges would be in each folder, or use the same number range in each folder, things could be a lot faster because then you could just choose a random number and load the appropriate image.

Rgds

Thanks but this won't really work as the idea behind the gallery is just drop files into a folder and they will be picked up by the gallery scripts.

robmiller said:
It doesn't really matter how you're getting the filenames really, virtually anything is going to be fast. Nothing is cached when you do an `ls`, and think how stupidly fast that is.

Quote:
Originally Posted by radderfire
if you are having to read all the image files to ascertain their names, then it is going to be relatively slow.

You don't, don't worry

Ok thanks for that - I will just look at the image sizes again also and see what I can do there.

Thanks for the advice on this. Its often easy enough to get something up and working but i'm always keen to know is there a better way of doing things.
 
just reading up a bit more on this now that you mention glob(). I think it could be very useful.

What I was doing was opening a directory, navigating through every image, checking for a .jpg using preg_match, and then it the image matches a certain dimension then add it to array.

I think glob may do this a lot more efficiently.

Edit: I found a resource which says that opendir is quicker than glob - not that it would really matter for a smallish array of around 50 filenames
 
Last edited:
rather than do that, shouldn't the folder just contain jpg files only? :p

Yeah in this instance it does but I want to write the code so that if circumstances change then it still will work! Would using preg_match in instances like this cause much of an impact on efficiency? I'm sorry if these questions all sound very basic but i'm just very keen on getting things working as best as i can.
 
instead of preg_match, try strrpos? It can't be efficient using preg_match over strrpos
 
i'd just use glob. i hardly think it's going to bring your server down crashing down on it's knees. you can just use certain file extensions so it's pretty flexible if you just want to use image files for example.

PHP:
$files = glob('{*.PNG,*.png,*.JPG,*.jpg,*.GIF,*.gif}', GLOB_BRACE);
shamelessly stolen from rob's rotating sig tutorial... :p

$files is now an array. it doesn't get any easier than that. :)
 
i'd just use glob. i hardly think it's going to bring your server down crashing down on it's knees. you can just use certain file extensions so it's pretty flexible if you just want to use image files for example.

PHP:
$files = glob('{*.PNG,*.png,*.JPG,*.jpg,*.GIF,*.gif}', GLOB_BRACE);
shamelessly stolen from rob's rotating sig tutorial... :p

$files is now an array. it doesn't get any easier than that. :)

Yeah that is maybe a better idea then. That way I get an opendir, read_dir, preg_match all in one really? How can I use glob to specify a path to read?
For example if i want to trawl down a few levels to get to the files.
 
just been having a play with this again. Is there anyway to return only the filenames using glob() and not the full path? For things like generating image alt tags i would use the filename only. I know I could do a str_pos to remove the path but just curious if its doable directly with glob(). Also using open_dir seems slightly quicker here but that could just be me imagining it!

Edit: found this resource on a comparison between glob() and open_dir()
http://www.code2design.com/forums/glob_vs_opendir
Not sure how must one can trust a single review though.
 
Last edited:
You could also look into storing the image names in a database table, if they're added to the folder by script anyway. Then just pull them out of there. May well prove to be quicker to do than reading the filesystem as the table will likely be small enough to stay in memory.
 
nope i'm not that bothered about the exact time to be honest - just like to have the code as efficient as possible. Thanks for mentioning basename - that will do the trick nicely.
 
Back
Top Bottom