Hopefully there'll be a couple PHP guys here who can give me a hand.
I house all my forum posting pictures on my webserver, and finding a picture used to require me to open an FTP connection and go through them to find the one I want. So, I created a simple
PHP script page that is a gallery of sorts. It works OK, but there are a couple of problems:
- Speed. Currently, I just list all the images I have by using PHP to automatically get a directory listing of all PNG, GIF, and JPG files stored. But it seems to be quite slow doing this. Is the filesystem object supposed to be that slow? My code is the following:
At the top of the page:
Code:
<?php
$imgdir = '.';
$allowed_types = array('png','jpg','gif');
$dimg = opendir($imgdir);
while($imgfile = readdir($dimg)){
if(in_array(strtolower(substr($imgfile,-3)),$allowed_types)){
$a_img[] = $imgfile;
sort($a_img);
reset ($a_img);
}
}
$totimg = count($a_img); // total image number
?>
and then, where the image listing is to be shown:
Code:
<?php
for($x=0; $x<$totimg; $x++){
$size = getimagesize($imgdir.'/'.$a_img[$x]);
echo "\t".'<tr valign="top" align="left">'."\n\r";
echo "\t\t".'<td width="35"><b>'.($x+1).'.</b></td>'."\n\r";
echo "\t\t".'<td><a href="javascript:void(0)" onmouseover="showPic(this,\''.$imgdir.'/'.$a_img[$x].'\','.$size[0].','.$size[1].')">'.$a_img[$x].'</a></td>'."\n\r";;
echo "\t\t".'<td>'.$size[0].'x'.$size[1].'</td>'."\n\r";
echo "\t\t".'<td align="right">'.round(filesize($imgdir.'/'.$a_img[$x])/1000,1).'KB</td>'."\n\r";
echo "\t".'</tr>'."\n\r";
}
?>
Is there a way I can improve the speed?
- Thumbnail images. Currently, no images are initially shown, but show up when I hover over a link. Ideally, to speed loading time, I'd like to show a thumbnail image. Right now, I just geometrically resize the image just so it fits nicely in the window (this window contains a link to view the full-size version), but the filesize can be large and therefore a delay while it downloads. I could use client-side script to preload those images, but it wouldn't have time to do its job because I tend to start hunting for the image I want right away. Not to mention the bandwidth required to load unnecessary images.
I tried using some PHP code to automatically generate thumbnail images for the files in a directory, but it tends to stop making them after a random image, and it also won't create thumbnails of GIF or PNG files - it just skips those. It does work to a degree because some are generated, so the necessary graphics object IS present and works. And PHP doesn't throw any errors, it just stops generating the files. And since it stops generating the thumbnails on random images, it's not a matter of an unsupported image type, corrupt image, or whatever. Any idea why, or how I could debug to find out?