Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Modyfikacja skryptu] Skrypt do miniaturek
Forum PHP.pl > Forum > Przedszkole
NetJaro
Witam :-)

Mam skrypt własnej roboty, skrypt do miniaturek, lecz nie wiem jak go zmodyfikować, zeby w zmiennej $obrazek przechowywać cały obrazek np. $obrazek=zdjecie.jpg.

Aktualnie skryptu używa się tak:
<img border="0" src="skrypt.php?id=$id&type=$roz_pliku">

Gdzie $id = id pliku (nazwa) a $type = rozszerzenie pliku.

<?php
  1. <?php
  2.  
  3. // Katalog z obrazkami (nazwa obrazku to $id.$type, $id - liczba, $type
  4. // - jeden z [gif, png, jpg, jpeg])
  5. $img_dir = '/img/';
  6.  
  7. // Prefiks miniatur
  8. $thumb_prefix = 'mini_';
  9.  
  10. // Katalog z miniaturami
  11. $thumb_dir = 'mini/';
  12.  
  13. // Automatyczne wykrywanie zmiany oryginalnego pliku, nie zawsze działa
  14. // Żeby sprawdzić, czy działa, sprawdĽ czy daty utworzenia oryginalnego pliku
  15. // i jego miniatury s&plusmn; takie same.
  16. // Je&para;li wył&plusmn;czone, nie zapomnij po zmianie oryginalnego pliku usun&plusmn;ć
  17. // miniatury!!!
  18. $auto_detect = true;
  19.  
  20. function generate_thumbnail($srcFile, $destFile, $maxWidth = 98, $maxHeight = 98, $quality = 75, $forceJPEG = false)
  21. {
  22. $info = getimagesize($srcFile);
  23. $width = $info[0];
  24. $height = $info[1];
  25. $imgType = $info[2];
  26.  
  27. $xRatio = $maxWidth / $width;
  28. $yRatio = $maxHeight / $height;
  29.  
  30. if( ($width <= $maxWidth) && ($height <= $maxHeight) ) {
  31. $newWidth = $width;
  32. $newHeight = $height;
  33. }
  34. elseif( ($xRatio * $height) < $maxHeight ) {
  35. $newHeight = ceil($xRatio * $height);
  36. $newWidth = $maxWidth;
  37. }
  38. else {
  39. $newWidth = ceil($yRatio * $width);
  40. $newHeight = $maxHeight;
  41. }
  42.  
  43. if( file_exists($destFile) )
  44. {
  45. @unlink($destFile);
  46. }
  47.  
  48. if( (!$forceJPEG || ($forceJPEG && $imgType == IMAGETYPE_JPEG)) && $newWidth == $width && $newHeight == $height )
  49. {
  50. if( copy($srcFile, $destFile) )
  51. {
  52. @chmod($destFile, 0666);
  53. return true;
  54. }
  55. else
  56. {
  57. return false;
  58. }
  59. }
  60.  
  61. switch( $imgType )
  62. {
  63. case IMAGETYPE_GIF:
  64. $img = ImageCreateFromGif($srcFile);
  65. break;
  66. case IMAGETYPE_JPEG:
  67. $img = ImageCreateFromJpeg($srcFile);
  68. break;
  69. case IMAGETYPE_PNG:
  70. $img = ImageCreateFromPng($srcFile);
  71. break;
  72. default:
  73. die(&#092;"Sorry, this image type ($img_type) is not supported\");
  74. }
  75.  
  76. $thumb = ImageCreateTrueColor($newWidth, $newHeight);
  77. ImageCopyResampled($thumb, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
  78. ImageDestroy($img);
  79.  
  80. if( $forceJPEG || $imgType == IMAGETYPE_JPEG )
  81. {
  82. $return_val = ImageJPEG($thumb, $destFile, $quality);
  83. }
  84.  
  85. switch( $imgType )
  86. {
  87. case IMAGETYPE_GIF:
  88. $return_val = ImageGIF($thumb, $destFile);
  89. break;
  90. case IMAGETYPE_PNG:
  91. $return_val = ImagePNG($thumb, $destFile);
  92. break;
  93. }
  94. @chmod($destFile, 0666);
  95. @touch($destFile, filemtime($srcFile));
  96. ImageDestroy($thumb);
  97.  
  98. return true;
  99. }
  100.  
  101. $img_file = isset($_GET['id']) ? $_GET['id'] : false;
  102. if( !$img_file || !preg_match('/[a-z0-9]+/', $img_file) )
  103. {
  104. die('Incorrect id');
  105. }
  106.  
  107. $img_type = isset($_GET['type']) ? $_GET['type'] : '';
  108. if( !in_array($img_type, array('gif', 'png', 'jpg', 'jpeg', 'jpe', 'JPG', 'GIF' , 'PNG', 'BMP')) )
  109. {
  110. die('Incorrect type');
  111. }
  112.  
  113. $filename = $img_file . '.' . $img_type;
  114. $img_file = $img_dir . $filename;
  115. $thumb_file = $thumb_dir . $thumb_prefix . $filename;
  116. if( !file_exists($img_file) )
  117. {
  118. die('Given file does not exists');
  119. }
  120.  
  121. if( !file_exists($thumb_file) || ($auto_detect && filemtime($img_file) != filemtime($thumb_file)) )
  122. {
  123. generate_thumbnail($img_file, $thumb_file);
  124. }
  125.  
  126. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($thumb_file)) . ' GMT');
  127.  
  128. switch( $img_type )
  129. {
  130. case 'gif':
  131. Header(&#092;"Content-type: image/gif\");
  132. break;
  133. case 'png':
  134. header(&#092;"Content-type: image/png\");
  135. break;
  136. case 'jpg':
  137. case 'jpeg':
  138. header(&#092;"Content-type: image/jpeg\");
  139. break;
  140. }
  141.  
  142. readfile($thumb_file);
  143. ?>


Dzięki wielkie :-)
Bakus
  1. <?php
  2. $rozszerzenie = substr($nazwa_pliku, -3);
  3. $nazwa = substr($nazwa_pliku, 0, -4);
  4.  
  5. // dla
  6. $nazwa_pliku = 'plik.png';
  7. // zwroci:
  8. $rozszerzenie = 'png';
  9. $nazwa = 'plik';
  10. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.