PHP - Image Browser script


SUBMITTED BY: efbee

DATE: Oct. 5, 2016, 7:02 p.m.

FORMAT: PHP

SIZE: 9.4 kB

HITS: 409

  1. Image browser
  2. <?
  3. // Configuration Variables
  4. // ======================
  5. //
  6. // scale : weather or not to scale the large img to the max height/width if it
  7. // does not exceed the max height/width limits
  8. // 0=don't scale 1=scale
  9. // maxwidth : the maximum allowed width of the large picture
  10. // maxheight : the maximum allowed height of the large picture
  11. // thumbmaxw : the maximum allowed width of the thumbnail
  12. // thumbmaxh : the maximum allowed height of the thumbnail
  13. // imgperpage : the number of thumbnail images displayed
  14. // its best to give this a value that is a multiple of imgperrow
  15. // imgperrow : the number of thumbnail images per row
  16. // pgperrow : the number of page links per row
  17. // typelist : array that contains the imagetypes shown by the browser
  18. // currentdir : default the directory where this php file resides,
  19. // can be replaced by any directory of your choice
  20. // title : enter the title of your page here
  21. // home : enter path to your home directory or any other desired directory
  22. // (where the home link goes to)
  23. // this_page : the name of this file, $_SERVER['PHP_SELF'] should work, but if it dosen't
  24. // just use the file name; this is where all the links to this page go
  25. // captionext : filename extension placed on caption file with same name
  26. // as image. (ie - <image>.<ext>.<captionext>)
  27. // example: image file sunrise.jpg would use the caption
  28. // file sunrise.jpg.txt with the default captionext
  29. // caption : default caption to place under files if no caption file
  30. // exists
  31. // stylesheet : enter the path to your stylesheet here
  32. // you may enter just '' to use the default(will embed it in the html)
  33. // you may also enter 'none' to have no style sheet
  34. // the stylesheet should have these classes:
  35. //
  36. // .imag { border-style : solid;
  37. // border-color: blue;
  38. // border-width : 1px;}
  39. // .thumb { border-style : solid;
  40. // border-color: #999999;
  41. // border-width : 2px;}
  42. // A:link { color: #999999;
  43. // text-decoration : none; }
  44. // A:visited { color: #999999;
  45. // text-decoration : none; }
  46. // A:hover { color:blue; }
  47. // any of these classses can be adjusted to your needs
  48. //
  49. //
  50. // USAGE:
  51. // to browse through the images use the back and forward images
  52. // click on one of the thumbnails
  53. // or use one of the pagelinks to go directly to another set of images
  54. // clicking on the large image will give you the full image
  55. //---Variables---
  56. $scale = 0;
  57. $maxwidth = 640;
  58. $maxheight = 480;
  59. $thumbmaxw = 50;
  60. $thumbmaxh = 50;
  61. $imgperpage = 10;
  62. $imgperrow = 5;
  63. $pgperrow = 10;
  64. $currentdir = getcwd ();
  65. $typelist = array("jpg","jpeg","gif","png","JPG");
  66. $imagelist = array();
  67. $title = "Pics";
  68. $stylesheet = '' ;
  69. $home = "{$_SERVER['PHP_SELF']}";
  70. $this_page = "{$_SERVER['PHP_SELF']}";
  71. $caption = "";
  72. $captionext = "txt";
  73. //--- ind is put to zero when the script is first called uppon---
  74. if(!isset($_GET['ind']))
  75. $_GET['ind'] = 0;
  76. $index = $_GET['ind'];
  77. //---the following code iterates through the directory and puts any image found in the imagelist array---
  78. $dp=opendir($currentdir);
  79. while ( false != ( $file=readdir($dp) ) ) {
  80. if (is_file($file) && $file!="." && $file!=".."){
  81. $extention = explode(".",$file);
  82. $extfield = count($extention)-1;
  83. $extention = $extention[$extfield];
  84. if( in_array($extention,$typelist) ){
  85. array_push ($imagelist,$file);
  86. }
  87. }
  88. }
  89. ?>
  90. <html >
  91. <head>
  92. <title><?= $title ?></title>
  93. <?
  94. if ($stylesheet == ''){
  95. //--insert the default style sheet into html if none specified
  96. echo '<style type="text/css">'
  97. .'.imag { border-style : solid;'
  98. .'border-color: blue;'
  99. .'border-width : 1px;}'
  100. .'.thumb { border-style : solid;'
  101. .'border-color: #999999;'
  102. .'border-width : 2px;}'
  103. .'A:link { color: #999999;'
  104. .'text-decoration : none; }'
  105. .'A:visited { color: #999999;'
  106. .'text-decoration : none; }'
  107. .'A:hover { color:blue; }'
  108. .'</style>';
  109. } elseif ($stylesheet == 'none') {
  110. //--no style sheet if that is what you want
  111. } else {
  112. echo "<link rel=\"STYLESHEET\" href=\"$stylesheet\" />";
  113. }
  114. ?>
  115. </head>
  116. <body>
  117. <table align="center" border="0">
  118. <tr>
  119. <td>
  120. <? if($index-1 >= 0) {?>
  121. <a href='<?= $this_page ?>?ind=<?= $index-1 ?>'>[ prev ]</a>
  122. <? } ?>
  123. </td>
  124. <td>
  125. <?
  126. //--- This is where the large pictures are resized so that they maintain ratio---
  127. $sizeee = getimagesize ("$imagelist[$index]");
  128. $imgwidth = $sizeee[0];
  129. $imgheight = $sizeee[1];
  130. if ($scale == 1 || $imgwidth > $maxwidth || $imgheight > $maxheight) { // decide if img needs to be scaled
  131. $newwidth = $imgwidth/($imgheight/$maxheight);
  132. $newheight = $imgheight/($imgwidth/$maxwidth);
  133. if ($imgwidth < $imgheight) {
  134. if ($newwidth > $maxwidth)
  135. {
  136. ?>
  137. <a href="<?= $imagelist[$index] ?>" target="_blank">
  138. <img src="<?= $imagelist[$index] ?>" width="<?= $maxwidth ?>" height="<?= $newheight ?>" alt="" />
  139. </a>
  140. <?
  141. } else {
  142. ?>
  143. <a href="<?= $imagelist[$index] ?>" target="_blank">
  144. <img src="<?= $imagelist[$index] ?>" width="<?= $newwidth ?>" height="<?= $maxheight ?>" alt="" />
  145. </a>
  146. <?
  147. }
  148. } else {
  149. if ($newhight > $maxheight)
  150. {
  151. ?>
  152. <a href="<?= $imagelist[$index] ?>" target="_blank">
  153. <img src="<?= $imagelist[$index] ?>" width="<?= $newwidth ?>" height="<?= $maxheight ?>" alt="" />
  154. </a>
  155. <?
  156. } else {
  157. ?>
  158. <a href="<?= $imagelist[$index] ?>" target="_blank">
  159. <img src="<?= $imagelist[$index] ?>" width="<?= $maxwidth ?>" height="<?= $newheight ?>" alt="" />
  160. </a>
  161. <?
  162. }
  163. }
  164. } else { ?>
  165. <a href="<?= $imagelist[$index] ?>" target="_blank">
  166. <img src="<?= $imagelist[$index] ?>" width="<?= $imgwidth ?>" height="<?= $imgheight ?>" alt="" />
  167. </a>
  168. <? }
  169. ?>
  170. </td>
  171. <td>
  172. <? if($index+1 < count($imagelist) ) {?>
  173. <a href="<?= $this_page ?>?ind=<?= $index+1 ?>">[ next ]</a>
  174. <? } ?>
  175. </td>
  176. </tr>
  177. <tr>
  178. <td>
  179. </td>
  180. <td>
  181. <center><?
  182. if (file_exists ($imagelist[$index]. "." . $captionext) &&
  183. is_file ($imagelist[$index]. "." . $captionext) &&
  184. !is_dir ($imagelist[$index]. "." . $captionext))
  185. include $imagelist[$index]. "." . $captionext;
  186. else
  187. echo $caption; ?>
  188. </center>
  189. </td>
  190. <td>
  191. </td>
  192. </tr>
  193. </table>
  194. <table align="center">
  195. <tr><td></td>
  196. <?
  197. //---this code generates the thumbnails based on the configuration settings---
  198. $nrpages = ceil( count($imagelist)/$imgperpage );
  199. for($j=0;$j<$nrpages;$j++)
  200. {
  201. if( $index >= ($j*$imgperpage) && ($index < (($j+1) * $imgperpage)) ) {
  202. for($i=($j*$imgperpage);$i<(($j+1) * $imgperpage);$i++) {
  203. if(($i%$imgperrow == 0) && ($i > 0)) { ?>
  204. </tr>
  205. <tr>
  206. <? }
  207. if($i <count($imagelist) ) {
  208. $path = "$this_page?ind=".$i; ?>
  209. <td>
  210. <?
  211. //--- This is where the thumbnails are resized so that they maintain ratio---
  212. $sizeee = getimagesize ("$imagelist[$i]");
  213. $imgwidth = $sizeee[0];
  214. $imgheight = $sizeee[1];
  215. $newthumbw = $imgwidth/($imgheight/$thumbmaxh);
  216. $newthumbh = $imgheight/($imgwidth/$thumbmaxw);
  217. if ($imgwidth < $imgheight) {
  218. if ($newthumbw > $thumbmaxw)
  219. {
  220. ?>
  221. <a href="<?= $path ?>">
  222. <img src="<?= $imagelist[$i] ?>" width="<?= $thumbmaxw ?>" height="<?= $newthumbh ?>" alt="" />
  223. </a>
  224. <?
  225. } else {
  226. ?>
  227. <a href="<?= $path ?>">
  228. <img src="<?= $imagelist[$i] ?>" width="<?= $newthumbw ?>" height="<?= $thumbmaxh ?>" alt="" />
  229. </a>
  230. <?
  231. }
  232. } else {
  233. if ($newthumbh > $thumbmaxh)
  234. {
  235. ?>
  236. <a href="<?= $path ?>">
  237. <img src="<?= $imagelist[$i] ?>" width="<?= $newthumbw ?>" height="<?= $thumbmaxh ?>" />
  238. </a>
  239. <?
  240. } else {
  241. ?>
  242. <a href="<?= $path ?>">
  243. <img src="<?= $imagelist[$i] ?>" width="<?= $thumbmaxw ?>" height="<?= $newthumbh ?>" alt="" />
  244. </a>
  245. <?
  246. }
  247. }
  248. ?>
  249. </td>
  250. <? }
  251. }
  252. }
  253. }
  254. ?>
  255. </tr>
  256. </table>
  257. <br />
  258. <center>
  259. <?
  260. //---this code generates links based on the configuration settings---
  261. //---only puts $pgperrow page links per row to make cleaner---
  262. for($j=0;$j<$nrpages;$j++) {
  263. if(($j%$pgperrow == 0) && ($j > 0)) { ?>
  264. <br />
  265. <? } ?>
  266. <a href="<?= $this_page ?>?ind=<?= ($j*$imgperpage) ?>">[page <?= ($j+1) ?>]</a>
  267. <? } ?>
  268. </center>
  269. <center><a href="<?= $home ?>">[ home ]</a></center>
  270. </body>
  271. </html>

comments powered by Disqus