/index.php

https://github.com/einars/tiny-php-gallery · PHP · 229 lines · 172 code · 39 blank · 18 comment · 14 complexity · 4ae8f27aa496aaa4125c096420bef870 MD5 · raw file

  1. <?php
  2. /*
  3. A very tiny PHP gallery intended to be dropped into a web-facing folder full of images.
  4. See the gallery in action: http://spicausis.lv/gallery-demo/ .
  5. Extensions supported: .jpg, haven't had a need for anything else yet.
  6. The gallery needs to have a writable "thumbs" folder (it will attempt to create one).
  7. It uses imagemagick's convert() for the image conversion. All good hosts have it.
  8. You may add a style.css file to the folder if you suddenly have a wish to customize the looks.
  9. Written by Einar Lielmanis, einar@spicausis.lv
  10. */
  11. function main()
  12. {
  13. global $images;
  14. $images = glob('*.jpg') + glob('*.JPG');
  15. if (! file_exists('thumbs')) {
  16. mkdir('thumbs') or die('Cannot make thumbs');
  17. }
  18. is_writable('thumbs') or die('thumbs not writable');
  19. dispatch(@$_REQUEST['a']);
  20. }
  21. function dispatch($action)
  22. {
  23. switch($action) {
  24. case 'make_preview':
  25. return make_preview(@$_REQUEST['img']);
  26. case 'make_thumb':
  27. return make_thumb(@$_REQUEST['img']);
  28. case 'preview':
  29. return print_preview();
  30. default:
  31. return print_index();
  32. }
  33. }
  34. function escapeshellarg_utf8($arg)
  35. {
  36. $current_locale = setlocale(LC_CTYPE, null);
  37. $restore = false;
  38. if (setlocale(LC_CTYPE, 'en_US.UTF-8')) {
  39. $restore = true;
  40. }
  41. $ret = escapeshellarg($arg);
  42. if ($restore) {
  43. setlocale(LC_CTYPE, $current_locale);
  44. }
  45. return $ret;
  46. }
  47. function make_thumb($image)
  48. {
  49. assert_good_image($image);
  50. system(sprintf('convert %s -resize 300x170 %s',
  51. escapeshellarg_utf8($image),
  52. escapeshellarg_utf8(thumb_of($image))));
  53. header('Content-type: image/jpeg');
  54. readfile(thumb_of($image));
  55. }
  56. function make_preview($image)
  57. {
  58. assert_good_image($image);
  59. system(sprintf('convert %s -resize 800x600 %s',
  60. escapeshellarg_utf8($image),
  61. escapeshellarg_utf8(preview_of($image))));
  62. header('Content-type: image/jpeg');
  63. readfile(preview_of($image));
  64. }
  65. function print_index()
  66. {
  67. print_html_crap();
  68. global $images;
  69. foreach($images as $image) {
  70. printf('<a href="?a=preview&img=%s"><img src="%s"></a>'
  71. , $image
  72. , thumb_url($image)
  73. );
  74. }
  75. }
  76. function print_preview()
  77. {
  78. $image = $_REQUEST['img'];
  79. assert_good_image($image);
  80. global $images;
  81. $index = array_search($image, $images);
  82. print_html_crap();
  83. $prevlink = null;
  84. $nextlink = null;
  85. if (isset($images[$index - 1])) {
  86. $prevlink = '?a=preview&img=' . $images[$index - 1];
  87. }
  88. if (isset($images[$index + 1])) {
  89. $nextlink = '?a=preview&img=' . $images[$index + 1];
  90. }
  91. printf('<div id="counter">%d/%d</div>'
  92. , $index + 1
  93. , sizeof($images)
  94. );
  95. echo '<p id="links">';
  96. if ( ! $prevlink) {
  97. echo '<a class="inactive" href="#">« prev</a>';
  98. } else {
  99. printf('<a href="%s">« prev</a>', $prevlink);
  100. }
  101. printf('<a id="prev" href="?">home</a>');
  102. if ( ! $nextlink) {
  103. echo '<a href="#" class="inactive">next »</a>';
  104. } else {
  105. printf('<a href="%s">next »</a>', $nextlink);
  106. }
  107. echo '</p>';
  108. printf('<a href="%s"><img id="preview" src="%s"></a>'
  109. , $image
  110. , preview_url($image)
  111. );
  112. }
  113. function thumb_url($image)
  114. {
  115. if (file_exists(thumb_of($image))) {
  116. return thumb_of($image);
  117. } else {
  118. return '?a=make_thumb&img=' . $image;
  119. }
  120. }
  121. function preview_url($image)
  122. {
  123. if (file_exists(preview_of($image))) {
  124. return preview_of($image);
  125. } else {
  126. return '?a=make_preview&img=' . $image;
  127. }
  128. }
  129. function thumb_of($image)
  130. {
  131. return 'thumbs/thu_' . $image;
  132. }
  133. function preview_of($image)
  134. {
  135. return 'thumbs/prv_' . $image;
  136. }
  137. function assert_good_image($image)
  138. {
  139. global $images;
  140. in_array($image, $images) or die('Bad image');
  141. }
  142. function print_html_crap()
  143. {
  144. header('Content-type: text/html; charset=utf-8');
  145. echo '<style>';
  146. echo <<<STYLE
  147. img {
  148. margin: 2px 2px 0 0;
  149. display: block;
  150. float: left;
  151. }
  152. img#preview {
  153. float: none;
  154. margin: 0 auto;
  155. clear: left;
  156. }
  157. p#links {
  158. text-align: center;
  159. }
  160. p#links a {
  161. padding: 5px 30px;
  162. background: #eee;
  163. margin: 0 1px;
  164. color: #33c;
  165. }
  166. p#links a.inactive {
  167. color: #aaa;
  168. text-decoration: none;
  169. }
  170. #counter {
  171. position: absolute;
  172. right: 10px;
  173. top: 10px;
  174. color: #888;
  175. }
  176. STYLE;
  177. if (file_exists('style.css')) {
  178. readfile('style.css');
  179. }
  180. echo '</style>';
  181. }
  182. main();