Archive for the ‘php’ Category

PHP4 Automatic Photo List

Friday, November 3rd, 2006

I recently constructed an index.php file that automatically looks into a directory for files, images in my case, to construct an index page of thumbnails. This has greatly reduced the workload of having to manually edit the html file, like I'd been doing before.

Sure, I could use some online photo sharing website, but I already have a hosting service and this gave me a chance to code up my own, so I took the plunge.

The core of the function is called searchdir(), which I found on php.net by a guy named Nicolas Merlet on a manual page about Directory Functions.
The code uses filectime() which, I believe is a linux function to derive the most recent file change time. Anyway the code uses this value to reverse sort all of the files and then uses this array to construct an image thumbnail gallery.
The assumption in my scheme is that the thumbnail image name is the same as the large image name, except for an extra "_sm" just before the file extension.

For example, given a file called 2006-10-24_17'33_139.jpg, it's associated thumbnail will be 2006-10-24_17'33_139_sm.jpg.

Notice that my file names are date/times. This was done with an open source utility to "batch rename" image files based on their exif data. The utility is called Lupas Rename 2000 (I guess it's 6 years old???). Anyway, it's handy and free and works well and gives you a preview option to specify how you want your files named. There's also another feature to automatically lowercase the file extension, for example. You can find Lupas Rename here.

The code for my index.php file:

PHP:
  1. function searchdir ( $path , $maxdepth = -1 , $mode = "FULL" , $d = 0 )
  2. {
  3. if ( substr ( $path , strlen ( $path ) - 1 ) != '/' ) { $path .= '/' ; }
  4. $dirlist = array () ;
  5. if ( $mode != "FILES" ) { $dirlist[] = $path ; }
  6. if ( $handle = opendir ( $path ) )
  7. {
  8. while ( false !== ( $file = readdir ( $handle ) ) )
  9. {
  10. if ( $file != '.' && $file != '..' )
  11. {
  12. $file = $path . $file ;
  13. if ( ! is_dir ( $file ) ) { if ( $mode != "DIRS" ) {
  14. $dirlist[$file] =date ("Ymd H:i:s", filectime($file));
  15.  
  16. } }
  17.  
  18. elseif ( $d>=0 && ($d <$maxdepth || $maxdepth <0) )
  19. {
  20. $result = searchdir ( $file . '/' , $maxdepth , $mode , $d + 1 ) ;
  21. $dirlist = array_merge ( $dirlist , $result ) ;
  22. }
  23. }
  24. }
  25. closedir ( $handle ) ;
  26. }
  27. if ( $d == 0 ) { arsort($dirlist);}//natcasesort ( $dirlist ) ; }
  28. return ( $dirlist ) ;
  29. }
  30. function makeDate($dateIn) {
  31. $year = substr($dateIn, 0, 4);
  32. $month = substr($dateIn, 4, 2);
  33. $day = substr($dateIn, 6, 2);
  34. $hour = substr($timestamp, 8, 2);
  35. $min = substr($timestamp, 10, 2);
  36. $sec = substr($timestamp, 12, 2);
  37. return date('D, d M Y H:i:s O', mktime($hour, $min, $sec, $month, $day, $year));
  38. }
  39. $imglist = searchdir('/home/sroberso/public_html/family/',-1,'FILES',0);
  40.  
  41. header('Content-Type: application/xml');
  42. ?>
  43. <link />http://family.robersonsoftware.com/ Check out the latest pictures of our family.
  44. en-us Wed, 25 Oct 2006 00:00:00 -0500 Wed, 01 Nov 2006 00:00:00 -0600
  45. Weblog Editor 2.0
  46. scottroberson@tempinbox.com
  47. scottroberson@tempinbox.com
  48. //$counter = 0;
  49. foreach ($imglist as $img=>$time) {if (strpos($img,"sm.jpg")>0) {
  50. $img = str_replace("'","'",$img);
  51. $img = str_replace("(","(",$img);
  52. $img = str_replace(")",")",$img);
  53. $img = str_replace(" ","%20",$img);
  54. $imgsm = substr($img,strlen('/home/sroberso/public_html/family/'));
  55. $imglg = str_replace("_sm","",$imgsm);
  56. //$counter++;
  57. echo "\n";
  58. echo "\t\n";
  59. echo "\t
  60. <link />http://family.robersonsoftware.com/".$imglg."\n";
  61. echo "\t".'&lt;a xhref="http://family.robersonsoftware.com/'.$imglg.'" mce_href="http://family.robersonsoftware.com/'.$imglg.'" border="0" title="View Pic"&gt;&lt;img xsrc="'.$imgsm.'" mce_src="'.$imgsm.'" border="0" /&gt;&lt;/a&gt;'."\n";
  62. echo "\t".''."\n";
  63. echo "\t ".makeDate($time)."\n";
  64. echo "\n";
  65. }
  66. }
  67. ?>