Möchte man eine Bilder-Galerie auf seine Website einbinden, so soll am Anfang meist eine Übersicht über alle Bilder angezeigt werden.

Um die Ladezeit der Übersicht und damit der Seite zu minimieren macht es Sinn, sog. Thumbnails - kleinere Versionen (bzgl. Dimension und Dateigröße) der eigentlichen Bilder - anzuzeigen.

Wer diese nicht immer manuell erstellen möchte, der kann mit dem folgenden PHP-Skript diese Aufgabe automatisch erledigen lassen. 🙂 

Die wichtigsten Aspekte des Schnipsels sind in den Kommentaren erklärt.

Anzumerken ist vielleicht noch, dass die letzten beiden Parameter optional sind (die Standardwerte stehen in der Methodensignatur). Außerdem werden nur die Formate JPEG, GIF und PNG unterstützt.

Bei Fehlern, Verbesserungsvorschlägen und co. einfach melden, ich erweitere den Schnipsel gerne. 😉

Werbung

Werbeblocker aktiv?

Vermutlich aufgrund eines Werbeblockers kann an dieser Stelle leider keine Werbung angezeigt werden. Als Blog ist diese Website jedoch darauf angewiesen, um diverse Kosten abdecken zu können. Vielleicht möchtest du Servaholics ja in deinem Werbeblocker als Ausnahme hinzufügen und so unterstützen? 🙂

  1. <?php
  2. /**
  3.  * Creates thumbnails for all images (jpeg, png, gif) in a specific folder.
  4.  * @param pathToImages the path to the folder containing the images to be parsed to thumbnails
  5.  * @param pathToThumbs the path to the folder where the thumbnails should be stored
  6.  * @param thumbWidth the width of the thumbnails
  7.  * @param forceWidth whether every thumbnail should have exactly that width or - if set to false - smaller original image stay their original
  8.  * width and are only copied
  9.  * @param refresh if there is already a thumbnail for an image (decided by equal filename), should it be refreshed/recreated?
  10.  * @author servaholics
  11.  */
  12. function createThumbs($pathToImages, $pathToThumbs, $thumbWidth, $forceWidth = true, $refresh = false) {
  13. 	// If path to images does not end with slash, add it
  14. 	if (substr($pathToImages, -1) != "/") {
  15. 		$pathToImages .= "/";
  16. 	}
  17. 	// If path to thumbs does not end with slash, add it
  18. 	if (substr($pathToThumbs, -1) != "/") {
  19. 		$pathToThumbs .= "/";
  20. 	}	
  21.  
  22. 	// Open path to images
  23. 	$dir = opendir($pathToImages);
  24. 	if ($dir === false) {
  25. 		echo "The specified path to the original image is not valid: ".$pathToImages;
  26. 		return;
  27. 	}
  28.  
  29. 	// Create folder for thumbs if necessary
  30. 	if (!is_dir($pathToThumbs)) {
  31. 		mkdir($pathToThumbs);
  32. 	}
  33.  
  34. 	// Read all the file in the images folder
  35. 	while (false !== ($fname = readdir($dir))) {
  36. 		// Get extension of current file
  37. 		$extension = pathinfo($pathToImages.$fname, PATHINFO_EXTENSION);
  38.  
  39. 		// If there is already a thumbnail for that file, skip it if there is no request to refresh it
  40. 		if (!$refresh && file_exists($pathToThumbs.$fname)) {
  41. 			continue;
  42. 		}
  43.  
  44. 		// Create image object of current file
  45. 		if (strtolower($extension) == 'jpg' || strtolower($extension) == 'jpeg') {
  46. 			$img = imagecreatefromjpeg($pathToImages.$fname);
  47. 		} else if (strtolower($extension) == 'png') {
  48. 			$img = imagecreatefrompng($pathToImages.$fname);
  49. 		} else if (strtolower($extension) == 'gif') {
  50. 			$img = imagecreatefromgif($pathToImages.$fname);
  51. 		} else { // or if it is no image, skip the file
  52. 			continue;
  53. 		}
  54.  
  55. 		// Get data of image
  56. 		$height = imagesy($img);
  57. 		$width = imagesx($img);
  58.  
  59. 		if (!$forceWidth && $width <= $thumbWidth) { // If we allow image widths lower than the 
  60. 																			// specified thumb width, only copy the original image
  61. 			$tmp_img = $img;
  62. 		} else { // Resize and copy the image
  63. 			// Get new dimension data
  64. 			$new_width = $thumbWidth;
  65. 			$new_height = floor($height * ($thumbWidth / $width));
  66.  
  67. 			// Create a new temporary image object
  68. 			$tmp_img = imagecreatetruecolor($new_width, $new_height);
  69.  
  70. 			// Copy and resize old image to new one
  71. 			imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  72. 		}
  73.  
  74. 		// Store file
  75. 		if (strtolower($extension) == 'jpg' || strtolower($extension) == 'jpeg') {
  76. 			imagejpeg($tmp_img, $pathToThumbs.$fname);
  77. 		} else if (strtolower($extension) == 'png') {
  78. 			imagepng($tmp_img, $pathToThumbs.$fname);
  79. 		} else if (strtolower($extension) == 'gif') {
  80. 			imagegif($tmp_img, $pathToThumbs.$fname);
  81. 		}
  82.  
  83. 		// Destroy the objects (don't longer need them)
  84. 		imagedestroy($img);
  85. 		if ($img != $tmp_img) {
  86. 			imagedestroy($tmp_img);
  87. 		}
  88. 	}
  89.  
  90. 	// Close folder
  91. 	closedir($dir);
  92. }
  93.  
  94. // Example: Call the creation process. Original images are in the folder "original", thumbnails will be stored in the folder "thumbs" - both relative to the script's path.
  95. // The width of all the thumbnails will be 330 pixels.
  96. createThumbs("original/", "thumbs/", 330);
  97. ?>

Deine Meinung zu diesem Artikel?