Free PHP Scripts for Gallery Site

Mar 23, 2012, by admin

The site is coded in php, comes with full source code. It’s 100% FREE and needs no database for the images. Simply upload your images and the script does the rest!

entirely FREE!,Written in php.All pages are created using Apaches mod-revise so it looks like flat html pages to the search engines.

Automatic creation of the thumbnail images.Ability to categorise into albums.

No database is needed. Simply upload your images into their own folders (albums) and the script will do the rest.Images are automatically titled with the original filename.JPG, PNG and static GIF images supported .Single config file permits for modifying thumbnail size, how many per page, site title, colours and more. Automatic page titles/meta information based on the image,filename/directory.

  1. <?php
  2. function createhtmlname($name) {
  3. $replace_values = array(” “, “‘”, “””, “”, “/”, “?”, “|”, “@”, “#”, “~”, “!”, “£”, “$”, “%”, “^”, “&”, “*”, “(“, “)”, “[“, “]”, “{“, “}”, “+”, “=”, “-“);
  4. $name = str_replace($replace_values, “_”, $name);
  5. $name = str_replace(“.”, “”, $name);
  6. $name = str_replace(“,”, “”, $name);
  7. return strtolower($name);
  8. }
  9. function randomfilename() {
  10. $salt = “0123456789abcdefghijklmnopqrstuvwxyz”;
  11. srand((double)microtime()*1000000);
  12. $i = 0;
  13. while ($i <= 25) {
  14. $num = rand() % 33;
  15. $tmp = substr($salt, $num, 1);
  16. $nname = $nname . $tmp;
  17. $i++;
  18. }
  19. return $nname;
  20. }
  21. function count_photos($folder) {
  22. global $storage_location;
  23. $dir_handle = @opendir($storage_location.$folder);
  24. $total = 0;
  25. while ($file = readdir($dir_handle)) {
  26. if(($file != “.”) && ($file != “..”)) {
  27. $exp   = explode(“.”, $file);
  28. $where = COUNT($exp)-1;
  29. $ext   = $exp[$where];
  30. if(($ext == “jpg”) || ($ext == “jpeg”) || ($ext == “pjpg”) || ($ext == “png”) || ($ext == “gif”)) $total++;
  31. }
  32. }
  33. closedir($dir_handle);
  34. return $total;
  35. }
  36. function load_photos($folder) {
  37. global $storage_location;
  38. $file_listing = array();
  39. $dir_handle = @opendir($storage_location.$folder);
  40. while ($file = readdir($dir_handle)) {
  41. if(($file != “.”) && ($file != “..”)) {
  42. $exp   = explode(“.”, $file);
  43. $where = COUNT($exp)-1;
  44. $ext   = $exp[$where];
  45. $title = $exp[0];
  46. if(($ext == “jpg”) || ($ext == “jpeg”) || ($ext == “pjpg”) || ($ext == “png”) || ($ext == “gif”)) $file_listing[] = array(‘path’=>$storage_location.$folder.”/”.$file, ‘filename’=>$file, ‘title’=>$title, ‘ext’=>$ext);
  47. }
  48. }
  49. closedir($dir_handle);
  50. return $file_listing;
  51. }
  52. function galleryImage ($path, $max_x, $justurl = FALSE) {
  53. include_once(“config.inc.php”);
  54. $sourceImagePath = $path;
  55. $targetImagePath = “cache/”.MD5($path.$max_x).”.jpg”;
  56. $outputImageQuality = 80;
  57. /* Check if file is already cached, if so just deliver existing image */
  58. if(!file_exists($targetImagePath)) {
  59. /* MAIN RESIZING SCRIPT */
  60. /* Load Dimensions of Original Image */
  61. $originalImageSize = getimagesize($sourceImagePath);
  62. $original_x = $originalImageSize[0];
  63. $original_y = $originalImageSize[1];
  64. if($original_x > $original_y) {
  65. $max_y = 0;
  66. $max_x = $max_x;
  67. }
  68. else if($original_x < $original_y) {
  69. $max_y = $max_x;
  70. $max_x = 0;
  71. }
  72. else {
  73. $max_y = $max_x;
  74. $max_x = $max_x;
  75. }
  76. /* Work out ratios and which way to crop */
  77. $state = 0;
  78. if($square == 1) {
  79. if($max_x == 0) $max_x = $max_y;
  80. elseif($max_y == 0) $max_y = $max_x;
  81. }
  82. if($max_x == 0) $state = 1;
  83. elseif($max_y == 0) $state = 2;
  84. if($state == 0) {
  85. $testratio = $max_x / $max_y;
  86. $origratio = $original_x / $original_y;
  87. if($origratio > $testratio) $state = 1;
  88. elseif($origratio < $testratio) $state = 2;
  89. else $state = 3;
  90. }
  91. /* With ratios sorted, plot co-ordinates */
  92. if($state == 1) {
  93. /* make new-y = max-y OR crop sides */
  94. if($square == 0) {
  95. if(($original_y > $max_y) || ($enlarge == 1)) $new_y = $max_y;
  96. else $new_y = $original_y;
  97. $new_x = round(($original_x / ($original_y / $new_y)), 0);
  98. $srcx = 0;
  99. $srcy = 0;
  100. $srcw = $original_x;
  101. $srch = $original_y;
  102. }
  103. else {
  104. if(($original_y > $max_y) || ($enlarge == 1)) $new_y = $max_y;
  105. else $new_y = $original_y;
  106. $new_x = $new_y;
  107. $tempratio = ($original_y / $new_y);
  108. $sectionwidth = $new_y * $tempratio;
  109. $srcy = 0;
  110. $srch = $original_y;
  111. $srcx = floor(($original_x – $sectionwidth) / 2);
  112. $srcw = floor($sectionwidth);
  113. }
  114. }
  115. elseif($state == 2) {
  116. /* make new-x = max-x OR crop top & bottom */
  117. if($square == 0) {
  118. if(($original_x > $max_x) || ($enlarge == 1)) $new_x = $max_x;
  119. else $new_x = $original_x;
  120. $new_y = round(($original_y / ($original_x / $new_x)), 0);
  121. $srcx = 0;
  122. $srcy = 0;
  123. $srcw = $original_x;
  124. $srch = $original_y;
  125. }
  126. else {
  127. if(($original_x > $max_x) || ($enlarge == 1)) $new_x = $max_x;
  128. else $new_x = $original_x;
  129. $new_y = $new_x;
  130. $tempratio = ($original_x / $new_x);
  131. $sectionheight = $new_x * $tempratio;
  132. $srcx = 0;
  133. $srcw = $original_x;
  134. $srcy = floor(($original_y – $sectionheight) / 2);
  135. $srch = floor($sectionheight);
  136. }
  137. }
  138. elseif($state == 3) {
  139. /* original image ratio = new image ratio – use all of image */
  140. if($square == 0) {
  141. if(($original_x > $max_x) || ($enlarge == 1)) $new_x = $max_x;
  142. else $new_x = $original_x;
  143. $new_y = round(($original_y / ($original_x / $new_x)), 0);
  144. $srcx = 0;
  145. $srcy = 0;
  146. $srcw = $original_x;
  147. $srch = $original_y;
  148. }
  149. else {
  150. if(($original_x > $max_x) || ($enlarge == 1)) $new_x = $max_x;
  151. else $new_x = $original_x;
  152. $new_y = $new_x;
  153. $srcx = 0;
  154. $srcy = 0;
  155. $srcw = $original_x;
  156. $srch = $original_y;
  157. }
  158. }
  159. /* Do Conversion */
  160. if(strstr(strtolower($path), “.jpg”)) $originalImage = ImageCreateFromJPEG($sourceImagePath);
  161. elseif(strstr(strtolower($path), “.jpeg”)) $originalImage = ImageCreateFromJPEG($sourceImagePath);
  162. elseif(strstr(strtolower($path), “.pjpg”)) $originalImage = ImageCreateFromJPEG($sourceImagePath);
  163. elseif(strstr(strtolower($path), “.png”)) $originalImage = ImageCreateFromPNG($sourceImagePath);
  164. elseif(strstr(strtolower($path), “.gif”)) $originalImage = ImageCreateFromGIF($sourceImagePath);
  165. $newImage = ImageCreateTrueColor($new_x, $new_y);
  166. ImageCopyResampled ($newImage, $originalImage, 0, 0, $srcx, $srcy, $new_x, $new_y, $srcw, $srch);
  167. ImageJPEG ($newImage, $targetImagePath, $outputImageQuality);
  168. ImageDestroy($newImage);
  169. ImageDestroy($originalImage);
  170. }
  171. /* Output Image */
  172. $imageSize = getimagesize($targetImagePath);
  173. if($justurl == TRUE) return $targetImagePath;
  174. else return “<img src=”$targetImagePath” width=”$imageSize[0]” height=”$imageSize[1]” border=”0″ style=”border:1px solid #000000;”>”;
  175. }
  176. ?>
  177. &nbsp;