PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/ck-gallery.php

https://github.com/UberGallery/ck-gallery
PHP | 277 lines | 191 code | 52 blank | 34 comment | 53 complexity | b4b4316e6f1446791c2ed1f34a460bdd MD5 | raw file
  1. <?php // CK-Gallery by Chris Kankiewicz <http://www.chriskankiewicz.com>
  2. // Customize your gallery by changing the following variables. If a variable
  3. // is contained within quotes make sure you don't delete the quotes.
  4. $galleryDir = "gallery"; // Original images directory (No trailing slash!)
  5. $thumbsDir = "$galleryDir/thumbs"; // Thumbnails directory (No trailing slash!)
  6. $logFile = "gallery-log.txt"; // Directory/Name of log file
  7. $thumbSize = 100; // Thumbnail width/height in pixels
  8. $imgPerPage = 0; // Images per page (0 disables pagination)
  9. $cacheExpire = 0; // Frequency (in minutes) of cache refresh
  10. $verCheck = 0; // Set to 1 to enable update notifications
  11. // *** DO NOT EDIT ANYTHING BELOW HERE UNLESS YOU ARE A PHP NINJA ***
  12. $version = "1.2.0"; // File version
  13. isset($_GET['page']) ? $currentPage = $_GET['page'] : $currentPage = 1;
  14. // *** START PAGE CACHING ***
  15. // Create cache directory if it doesn't exist
  16. $cacheDir = "gallery-cache";
  17. if (!file_exists($cacheDir)) {
  18. mkdir($cacheDir);
  19. }
  20. $cacheFile = "$cacheDir/page$currentPage-cached.html";
  21. $cacheTime = $cacheExpire * 60;
  22. // Serve from the cache if it is younger than $cacheTime
  23. if (file_exists($cacheFile) && time() - $cacheTime < filemtime($cacheFile) && $cacheExpire >= 1) {
  24. include($cacheFile);
  25. echo "<!-- Cached page: created ".date('H:i:s', filemtime($cacheFile))." / expires ".date('H:i:s', (filemtime($cacheFile)) + $cacheTime)." -->\n";
  26. } else {
  27. ob_start();
  28. // Create log file if it does not exist, otherwise open log for writing
  29. if (!file_exists($logFile)) {
  30. $log = fopen($logFile, "a");
  31. fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $logFile\r\n\r\n");
  32. } else {
  33. $log = fopen($logFile, "a");
  34. }
  35. // Create image directory if it doesn't exist
  36. if (!file_exists($galleryDir)) {
  37. mkdir($galleryDir);
  38. fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $galleryDir\r\n");
  39. }
  40. // Create thumbnail directory if it doesn't exist
  41. if (!file_exists($thumbsDir)) {
  42. mkdir($thumbsDir);
  43. fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsDir\r\n");
  44. }
  45. // Clean up thumbnail directory
  46. if ($dirHandle = opendir($thumbsDir)) {
  47. while (($file = readdir($dirHandle)) !== false) {
  48. if (isImage("$thumbsDir/$file")) {
  49. $size = getimagesize("$thumbsDir/$file");
  50. if (!file_exists("$galleryDir/$file") || $size[0] !== $thumbSize) {
  51. unlink("$thumbsDir/$file");
  52. fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." REMOVED: $thumbsDir/$file\r\n");
  53. }
  54. }
  55. }
  56. closedir($dirHandle);
  57. }
  58. // Alcohol! The cause of, and solution to, all of life's problems!
  59. // Create array from gallery directory
  60. if ($dirHandle = opendir($galleryDir)) {
  61. while (($file = readdir($dirHandle)) !== false) {
  62. if (isImage("$galleryDir/$file")) {
  63. $images[] = $file;
  64. }
  65. }
  66. closedir($dirHandle);
  67. }
  68. // Page varriables
  69. $totalImages = count($images);
  70. if ($imgPerPage <= 0 || $imgPerPage >= $totalImages) {
  71. $imgStart = 0;
  72. $imgEnd = $totalImages;
  73. } elseif ($imgPerPage > 0 && $imgPerPage < $totalImages) {
  74. $totalPages = ceil($totalImages / $imgPerPage);
  75. if ($_GET['page'] < 1) {
  76. $currentPage = 1;
  77. } elseif ($_GET['page'] > $totalPages) {
  78. $currentPage = $totalPages;
  79. } else {
  80. $currentPage = $_GET['page'];
  81. }
  82. $imgStart = ($currentPage - 1) * $imgPerPage;
  83. $currentPage * $imgPerPage > $totalImages ? $imgEnd = $totalImages : $imgEnd = $currentPage * $imgPerPage;
  84. }
  85. // Opening markup
  86. echo("<!-- Start CK-Gallery v$version - Created by, Chris Kankiewicz <http://www.ChrisKankiewicz.com> -->\r\n");
  87. echo("<div id=\"gallery-wrapper\">\r\n <div id=\"ck-gallery\">\r\n");
  88. for ($x = $imgStart; $x < $imgEnd; $x++) {
  89. $filePath = "$galleryDir/$images[$x]";
  90. // Convert file name and extension for processing
  91. if (ctype_upper(pathinfo($filePath,PATHINFO_EXTENSION))
  92. || strpos(basename($filePath),' ') !== false
  93. || strpos($filePath,'.jpeg') !== false) {
  94. $source = "$filePath";
  95. $fileParts = pathinfo($filePath); // Create array of file parts
  96. $ext = $fileParts['extension']; // Original extension
  97. $name = basename($filePath, ".$ext"); // Original file name without extension
  98. $dir = $fileParts['dirname']; // Directory path
  99. // Change extension to all lowercase
  100. if (ctype_upper($ext)) {
  101. $ext = strtolower($ext);
  102. }
  103. // Convert .jpeg to .jpg
  104. if ($ext == 'jpeg') {
  105. $ext = 'jpg';
  106. }
  107. // Replace spaces with underscores
  108. if (strpos($name,' ') !== false) {
  109. $extOld = $fileParts['extension'];
  110. $name = str_replace(' ','_',basename($filePath, ".$extOld"));
  111. }
  112. $destination = "$dir/$name.$ext";
  113. // Rename file and array element
  114. if (rename($source,"$dir/$name.tmp")) {
  115. if (rename("$dir/$name.tmp",$destination)) {
  116. $images[$x] = "$name.$ext";
  117. fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." RENAMED: $source to $destination\r\n");
  118. }
  119. }
  120. }
  121. $filePath = "$galleryDir/$images[$x]";
  122. $thumbPath = "$thumbsDir/$images[$x]";
  123. // Create thumbnail if it doesn't already exist
  124. if (!file_exists("$thumbPath")) {
  125. createThumb("$filePath","$thumbPath",$thumbSize);
  126. fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsDir/$images[$x]\r\n");
  127. }
  128. // Create XHTML compliant markup
  129. $noExt = substr($images[$x],0,strrpos($images[$x],'.'));
  130. $altText = str_replace("_"," ",$noExt);
  131. echo " <a href=\"$filePath\" title=\"$altText\" class=\"thickbox\" rel=\"photo-gallery\"><img src=\"$thumbPath\" alt=\"$altText\"/></a>\r\n";
  132. }
  133. // Clear float, create horizontal rule
  134. echo(" <div class=\"clear\"></div><div class=\"hr\"><hr /></div>\r\n");
  135. // If pagination enabled, create page navigation
  136. if ($imgPerPage > 0 && $imgPerPage < $totalImages) {
  137. $pageName = basename($_SERVER["PHP_SELF"]); // Get current page file name
  138. echo(" <ul id=\"ck-pagination\" style=\"margin: 0 !important; padding: 0 !important;\">\r\n");
  139. // Previous arrow
  140. $previousPage = $currentPage - 1;
  141. echo(" <li".($currentPage > 1 ? "><a href=\"$pageName?page=$previousPage\" title=\"Previous Page\">&lt;</a>" : " class=\"inactive\">&lt;")."</li>\r\n");
  142. // Page links
  143. for ($x = 1; $x <= $totalPages; $x++) {
  144. echo(" <li".($x == $currentPage ? " class=\"current-page\">$x" : "><a href=\"$pageName?page=$x\" title=\"Page $x\">$x</a>")."</li>\r\n");
  145. }
  146. // Next arrow
  147. $nextPage = $currentPage + 1;
  148. echo(" <li".($currentPage < $totalPages ? "><a href=\"$pageName?page=$nextPage\" title=\"Next Page\">&gt;</a>" : " class=\"inactive\">&gt;")."</li>\r\n");
  149. echo(" </ul>\r\n");
  150. }
  151. // Closing markup
  152. echo(" <div id=\"credit\">Powered by <a href=\"http://code.web-geek.net/ck-gallery\" target=\"_blank\">CK-Gallery</a>");
  153. // Display Thickbox link if site is using Thickbox
  154. if(file_exists("thickbox.js") || file_exists("thickbox/thickbox.js")) {
  155. echo(" &amp; <a href=\"http://jquery.com/demo/thickbox\" target=\"_blank\">Thickbox</a></div>\r\n");
  156. } else {
  157. echo("</div>\r\n");
  158. }
  159. // Version check and notification
  160. if ($verCheck == "1") {
  161. $verInfo = @file("http://code.web-geek.net/ck-gallery/version-check.php?ver=$version");
  162. $verInfo = @implode($verInfo);
  163. if ($verInfo == "upgrade") {
  164. echo(" <div class=\"clear\"></div>\r\n");
  165. echo(" <div id=\"ck-notice\">A new version of CK-Gallery is availabe. <a href=\"http://code.web-geek.net/ck-gallery\" target=\"_blank\">Get the latest version here</a>.</div>");
  166. } elseif ($verInfo == "development") {
  167. echo(" <div class=\"clear\"></div>\r\n");
  168. echo(" <div id=\"ck-notice\">This is a development version of CK-Gallery.</div>\r\n");
  169. }
  170. }
  171. echo(" <div class=\"clear\"></div>\r\n </div>\r\n</div>\r\n");
  172. echo("<!-- Page $currentPage of $totalPages -->\r\n");
  173. echo("<!-- End CK-Gallery - Licensed under the GNU Public License version 3.0 -->\r\n");
  174. fclose($log); // Close log
  175. // Cache the output to a file
  176. $fp = fopen($cacheFile, 'w');
  177. fwrite($fp, ob_get_contents());
  178. fclose($fp);
  179. ob_end_flush(); // Send the output to the browser
  180. }
  181. // *** START FUNCTIONS ***
  182. function createThumb($source,$dest,$thumb_size) {
  183. // Create thumbnail, modified from function found on http://www.findmotive.com/tag/php/
  184. $size = getimagesize($source);
  185. $width = $size[0];
  186. $height = $size[1];
  187. if ($width > $height) {
  188. $x = ceil(($width - $height) / 2 );
  189. $width = $height;
  190. } elseif($height > $width) {
  191. $y = ceil(($height - $width) / 2);
  192. $height = $width;
  193. }
  194. $new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
  195. @$imgInfo = getimagesize($source);
  196. if ($imgInfo[2] == IMAGETYPE_JPEG) {
  197. $im = imagecreatefromjpeg($source);
  198. imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
  199. imagejpeg($new_im,$dest,75); // Thumbnail quality (Value from 1 to 100)
  200. } elseif ($imgInfo[2] == IMAGETYPE_GIF) {
  201. $im = imagecreatefromgif($source);
  202. imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
  203. imagegif($new_im,$dest);
  204. } elseif ($imgInfo[2] == IMAGETYPE_PNG) {
  205. $im = imagecreatefrompng($source);
  206. imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
  207. imagepng($new_im,$dest);
  208. }
  209. }
  210. function isImage($fileName) {
  211. // Verifies that a file is an image
  212. if ($fileName !== '.' && $fileName !== '..') {
  213. @$imgInfo = getimagesize($fileName);
  214. $imgType = array(
  215. IMAGETYPE_JPEG,
  216. IMAGETYPE_GIF,
  217. IMAGETYPE_PNG,
  218. );
  219. if (in_array($imgInfo[2],$imgType))
  220. return true;
  221. return false;
  222. }
  223. }
  224. // EOF
  225. ?>