1. <?php
  2. $pathToImages = "images/";   //folder duze zdjecia
  3. $pathToThumbs = "thumbs/";   //folder na miniatury
  4.  
  5. $thumbWidth = 150;     //szerokosc miniatury
  6. $thumbHeight = 0;     //wysokosc miniatury, gdy "0" zachowana jest proporcja szerokosc x wysokosc
  7.  
  8. function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth, $thumbHeight)
  9. {
  10. //otwiera katalog
  11. $dir = opendir( $pathToImages );
  12.  
  13. //szukanie obrazow JPG
  14. while (false !== ($fname = readdir( $dir ))) {
  15.  
  16.   $info = pathinfo($pathToImages . $fname);
  17.   // dalej idzie gdy rozszezenie JPG
  18.   if ( strtolower($info['extension']) == 'jpg' )
  19.   {
  20.     echo "Utworzono miniature pliku: {$fname} <br />";
  21.     // wczytanie rozmiaru obrazu
  22.     $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
  23.     $width = imagesx( $img );
  24.     $height = imagesy( $img );
  25.  
  26.     // obliczanie rozmiaru miniaturki
  27.     $new_width = $thumbWidth;
  28.        //  $new_height = floor( $height * ( $thumbWidth / $width ) );
  29.        if ( $thumbHeight == 0 )
  30.             {
  31.  $new_height = floor( $height * ( $thumbWidth / $width ) );
  32.     }
  33.  else {
  34.   $new_height = $thumbHeight;
  35.     }
  36.  
  37.     // tworzenie nowych miniatur
  38.     $tmp_img = imagecreatetruecolor( $new_width, $new_height );
  39.  
  40.     // kopiowanie i zmniejszanie starych plikow w nowe
  41.     imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
  42.  
  43.     // utworzenie miniatur
  44.     imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
  45.   }
  46. }
  47. // zamykanie katalogu
  48. closedir( $dir );
  49. }
  50. createThumbs( $pathToImages, $pathToThumbs, $thumbWidth, $thumbHeight);
  51. ?>