mkpicidx.pl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/perl
  2. # Scans a directory with JPEG files, and generates PNG
  3. # thumbnails for all JPEG files and an index.html listing
  4. # the JPEGs and displaying the thumbnails
  5. #
  6. # Steinar Bang, 940916
  7. #
  8. $gallery="/home/steinarb/wwwhome/dod/gallery/steinarb" ;
  9. $cur_dir="." ;
  10. $icon_dir="$cur_dir/.icons";
  11. $prefix_file="$cur_dir/prefix.html" ;
  12. $suffix_file="$cur_dir/suffix.html" ;
  13. #
  14. $thumbnail_height=66 ; # Height in pixels
  15. opendir(IMAGEDIR, ".");
  16. @allfiles = sort(readdir(IMAGEDIR));
  17. closedir(IMAGEDIR) ;
  18. @jpgfiles = grep(/.*\.jpg/i, @allfiles) ;
  19. @giffiles = grep(/.*\.gif/, @allfiles) ;
  20. @rootnames = () ;
  21. @rootpaths = () ;
  22. @pnmfiles = ();
  23. @descfiles = () ;
  24. # Make ./.icons if it doesn't exist
  25. if (!-d $icon_dir) { mkdir($icon_dir,0777) ; }
  26. print STDERR "Lager PNM-filer og reduserer hoeyden til $thumbnail_height piksler:\n" ;
  27. foreach $f (@jpgfiles) {
  28. local ($rootname) = (&rootname($f)) ;
  29. local ($rootpath) = (&rootpath($f)) ;
  30. local($pnm) = ("$icon_dir/$rootname.pnm") ;
  31. print STDERR "Lager og skalerer $pnm\n" ;
  32. system("djpeg $f | pnmscale -height $thumbnail_height >$pnm") ;
  33. push (@pnmfiles,$pnm) ;
  34. print STDERR "rootname: $rootname rootpath: $rootpath\n" ;
  35. push(@rootnames, $rootname) ;
  36. push(@rootpaths, $rootpath) ;
  37. }
  38. foreach $f (@giffiles) {
  39. local ($rootname) = (&rootname($f)) ;
  40. local ($rootpath) = (&rootpath($f)) ;
  41. local($pnm) = ("$icon_dir/$rootname.pnm") ;
  42. print STDERR "Lager og skalerer $pnm\n" ;
  43. system("giftopnm $f | pnmscale -height $thumbnail_height >$pnm") ;
  44. push (@pnmfiles,$pnm) ;
  45. print STDERR "rootname: $rootname rootpath: $rootpath\n" ;
  46. push(@rootnames, $rootname) ;
  47. push(@rootpaths, $rootpath) ;
  48. }
  49. print STDERR "Sletter gamle ikoner..." ;
  50. system("rm $icon_dir/*.png") ;
  51. print STDERR "ferdig\n" ;
  52. print STDERR "Lager PNG-ikoner:\n" ;
  53. foreach $f (@pnmfiles) {
  54. local ($rootname) = &rootname($f) ;
  55. local ($png) = ("$rootname.png") ;
  56. print STDERR "Lager $png\n" ;
  57. system ("pnmcrop $f | pnmtopng >$png") ;
  58. }
  59. print STDERR "Sletter PNM-filer..." ;
  60. system("rm $icon_dir/*.pnm") ;
  61. print STDERR "ferdig\n" ;
  62. print STDERR "Lager \"index.html\":\n" ;
  63. open(INDX, ">index.html") ;
  64. if (-r $prefix_file) {
  65. open(DESC, $prefix_file);
  66. while(<DESC>) { print INDX ; }
  67. close(DESC) ;
  68. } else {
  69. $pwdnam = $ENV{'PWD'} ;
  70. $pwdnam =~ s#/(\w*/)*(\w+)$#$2# ;
  71. print INDX "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n\n" ;
  72. print INDX "<html>\n" ;
  73. print INDX "<head>\n" ;
  74. print INDX "<title>\n\"$pwdnam\" picture archive\n</title>\n" ;
  75. print INDX "</head>\n\n<body>\n" ;
  76. print INDX "<h1>\n\"$pwdnam\" picture archive\n</h1>\n" ;
  77. }
  78. $cur_counter = 1;
  79. foreach $f (@jpgfiles) {
  80. local ($rootname, $ext) = split(/\./, $f) ;
  81. local ($txt) = ("$rootname.txt") ;
  82. local ($icon) = ("$icon_dir/$rootname.png") ;
  83. local ($jpg) = ($f) ;
  84. print INDX "<p><a name=\"jpeg$cur_counter\" href=\"$jpg\"><img src=\"$icon\" alt=\"[$jpg]\"></a><br>\n" ;
  85. $cur_counter++ ;
  86. $desc = `rdjpgcom $jpg` ; # Get JPEG comment, if any
  87. if ($desc) {
  88. # unpack any quoted octal values from the JPEG comment
  89. $desc =~ s/\\(\d\d\d)/pack("C",oct($1))/ge ;
  90. print INDX "<em>$desc</em>\n" ;
  91. } elsif (-r $txt) {
  92. open(DESC, "$txt");
  93. while(<DESC>) { print INDX ; }
  94. close(DESC) ;
  95. }
  96. $jpg_size = (stat($jpg))[7] ;
  97. printf(INDX "(JPEG %1.0fK).", ($jpg_size / 1024)) ;
  98. print INDX "</p>\n" ;
  99. }
  100. $cur_counter = 1;
  101. foreach $f (@giffiles) {
  102. local ($rootname, $ext) = split(/\./, $f) ;
  103. local ($txt) = ("$rootname.txt") ;
  104. local ($icon) = ("$icon_dir/$rootname.gif") ;
  105. local ($gif) = ($f) ;
  106. print INDX "<p><a name=\"gif$cur_counter\" href=\"$gif\"><img src=\"$icon\" alt=\"[$gif]\"></a><br>\n" ;
  107. $cur_counter++ ;
  108. if (-r $txt) {
  109. open(DESC, "$txt");
  110. while(<DESC>) { print INDX ; }
  111. close(DESC) ;
  112. }
  113. $gif_size = (stat($gif))[7] ;
  114. printf(INDX "(GIF %1.0fK).", ($gif_size / 1024)) ;
  115. print INDX "</p>\n" ;
  116. }
  117. if (-r $suffix_file) {
  118. open(DESC, $suffix_file);
  119. while(<DESC>) { print INDX ; }
  120. close(DESC) ;
  121. } else {
  122. # Print some blank lines at the end, to allow jumping directly to
  123. # name'd <a> elements down in the index
  124. $no_of_blank_lines = 40 ;
  125. for($i=0; $i<$no_of_blank_lines; $i++) {
  126. print INDX "<br>" ;
  127. }
  128. print INDX "\n" ;
  129. print INDX "</body>\n</html>\n" ;
  130. }
  131. close(INDX) ;
  132. print STDERR "Ferdig\n" ;
  133. #
  134. # Steinar Bang, Falch Hurtigtrykk, Oslo
  135. #
  136. # Return the argument filename without the last '.ext' sequence (the
  137. # root name of the file).
  138. #
  139. sub rootname {
  140. local($n) = @_ ;
  141. $n =~ s/\.\w{0,3}$// ;
  142. $n ;
  143. }
  144. sub rootpath {
  145. local($n) = @_ ;
  146. $n =~ s/^(.*)\/[^\/]+$/\1/ ;
  147. $n ;
  148. }