/sorting-images/sorting.php

https://bitbucket.org/dbogicevic/daki-projects · PHP · 107 lines · 72 code · 25 blank · 10 comment · 19 complexity · 80a86898b21f5953af65c0f3643885af MD5 · raw file

  1. <?php
  2. $dirFiles = array();
  3. // opens images folder
  4. if ($handle = opendir('Images')) {
  5. while (false !== ($file = readdir($handle))) {
  6. // strips files extensions
  7. $crap = array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-");
  8. $newstring = str_replace($crap, " ", $file );
  9. // hides folders, writes out ul of images and thumbnails from two folders
  10. if ($file != "." && $file != ".." && $file != "index.php" && $file != "Thumbnails") {
  11. $dirFiles[] = $file;
  12. }
  13. }
  14. closedir($handle);
  15. }
  16. sort($dirFiles);
  17. foreach($dirFiles as $file)
  18. {
  19. echo "<li><a href=\"Images/$file\" class=\"thickbox\" rel=\"gallery\" title=\"$newstring\"><img src=\"Images/Thumbnails/$file\" alt=\"$newstring\" width=\"300\" </a></li>\n";
  20. }
  21. ?>
  22. <?php
  23. /* function: generates thumbnail */
  24. function make_thumb($src,$dest,$desired_width) {
  25. /* read the source image */
  26. $source_image = imagecreatefromjpeg($src);
  27. $width = imagesx($source_image);
  28. $height = imagesy($source_image);
  29. /* find the "desired height" of this thumbnail, relative to the desired width */
  30. $desired_height = floor($height*($desired_width/$width));
  31. /* create a new, "virtual" image */
  32. $virtual_image = imagecreatetruecolor($desired_width,$desired_height);
  33. /* copy source image at a resized size */
  34. imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
  35. /* create the physical thumbnail image to its destination */
  36. imagejpeg($virtual_image,$dest);
  37. }
  38. function get_files($images_dir,$exts = array('jpg')) {
  39. $files = array();
  40. if($handle = opendir($images_dir)) {
  41. while(false !== ($file = readdir($handle))) {
  42. $extension = strtolower(get_file_extension($file));
  43. if($extension && in_array($extension,$exts)) {
  44. $files[] = $file;
  45. }
  46. }
  47. closedir($handle);
  48. }
  49. return $files;
  50. }
  51. function get_file_extension($file_name) {
  52. return substr(strrchr($file_name,'.'),1);
  53. }
  54. ?>
  55. <?php
  56. /** settings **/
  57. $images_dir = '/img/hersteller/';
  58. $thumbs_dir = '/img/hersteller/thumb/';
  59. $thumbs_width = 200;
  60. $images_per_row = 3;
  61. $image_files = get_files($images_dir);
  62. if(count($image_files)) {
  63. $index = 0;
  64. sort($thumbnail_image);
  65. foreach($image_files as $index=>$file) {
  66. $index++;
  67. $thumbnail_image = $thumbs_dir.$file;
  68. if(!file_exists($thumbnail_image)) {
  69. $extension = get_file_extension($thumbnail_image);
  70. if($extension) {
  71. make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);
  72. }
  73. }
  74. echo '<a href="',$images_dir.$file,'" class="unterseitenbild"><img src="',$thumbnail_image,'" /></a>';
  75. if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
  76. }
  77. echo '<div class="clear"></div>';
  78. }
  79. else {
  80. echo '<p>Keine Bilder vorhanden!</p>';
  81. }
  82. ?>