Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: dziąłajaca funkcja do tworzenia miniaturek zdjęć
Forum PHP.pl > Forum > PHP
1q2w3e4r
Witam,

Robię sobie galerię zdjęć. Mam problem z miniaturkami.
Do tej pory gdy wykorzystywałem jakiś skrypt z sieci miniaturki były słabej jakości.
Potrzebuję skrypt, który będzie mógł zrobić miniaturkę która się skaluje do najdłuższego boku.
Oraz druga miniaturka jednego zdjęcia, która będzie konkretnego wymiaru każda, (czyli np. wycina od środka zdjęcia aż napotka koniec krawędzi).

Ma ktoś może taką funkcję?

Z góry dziękuję za pomoc
Pozdrawiam
rivos
Znalazłem coś w necie:
  1. <?php
  2. function rozszerzenie($file) {
  3. $v=explode('.',$file);
  4. $n=count($v);
  5. $extension=$v[$n];
  6. return $extension;
  7. }
  8.  
  9. if (isset($_GET['link']) && isset($_GET['width']) && isset($_GET['height'])) {
  10. switch (rozszerzenie($_GET['link'])) {
  11. case 'gif':
  12. $img=imagecreatefromgif($_GET['link']);
  13. $width=imagesx($img);
  14. $height=imagesy($img);
  15. $edit=false;
  16. $valid[0]=true;
  17. $valid[1]=true;
  18. if ($width<=$_GET['width']) {
  19. $valid[0]=false;
  20. }
  21. if ($height<=$_GET['height']) {
  22. $valid[1]=false;
  23. }
  24. if ($width>$_GET['width']) {
  25. $roznica=$width-$_GET['width'];
  26. $wsp=$width/$roznica;
  27. $nHeight=$height/$wsp;
  28. $rHeight=$height-$nHeight;
  29. $rWidth=$_GET['width'];
  30. $edit=true;
  31. }
  32. if ($rHeight>$_GET['height']) {
  33. $roznica=$rHeight-$_GET['height'];
  34. $wsp=$rHeight/$roznica;
  35. $nWidth=$rWidth/$wsp;
  36. $rHeight=$_GET['height'];
  37. $rWidth=$rWidth-$nWidth;
  38. $edit=true;
  39. }
  40. if (!$edit && !$valid[0] && !$valid[1]) {
  41. $rWidth=$width;
  42. $rHeight=$height;
  43. }
  44. $img_mini=imagecreatetruecolor($rWidth,$rHeight);
  45. imagecopyresampled($img_mini,$img,0,0,0,0,$rWidth,$rHeight,$width,$height);
  46. header("Content-type: image/gif;");
  47. imagegif($img_mini);
  48. break;
  49. case 'png':
  50. $img=imagecreatefrompng($_GET['link']);
  51. $width=imagesx($img);
  52. $height=imagesy($img);
  53. $edit=false;
  54. $valid[0]=true;
  55. $valid[1]=true;
  56. if ($width<=$_GET['width']) {
  57. $valid[0]=false;
  58. }
  59. if ($height<=$_GET['height']) {
  60. $valid[1]=false;
  61. }
  62. if ($width>$_GET['width']) {
  63. $roznica=$width-$_GET['width'];
  64. $wsp=$width/$roznica;
  65. $nHeight=$height/$wsp;
  66. $rHeight=$height-$nHeight;
  67. $rWidth=$_GET['width'];
  68. $edit=true;
  69. }
  70. if ($rHeight>$_GET['height']) {
  71. $roznica=$rHeight-$_GET['height'];
  72. $wsp=$rHeight/$roznica;
  73. $nWidth=$rWidth/$wsp;
  74. $rHeight=$_GET['height'];
  75. $rWidth=$rWidth-$nWidth;
  76. $edit=true;
  77. }
  78. if (!$edit && !$valid[0] && !$valid[1]) {
  79. $rWidth=$width;
  80. $rHeight=$height;
  81. }
  82. $img_mini=imagecreatetruecolor($rWidth,$rHeight);
  83. imagecopyresampled($img_mini,$img,0,0,0,0,$rWidth,$rHeight,$width,$height);
  84. header("Content-type: image/png;");
  85. imagepng($img_mini);
  86. break;
  87. case 'jpg':
  88. $img=imagecreatefromjpeg($_GET['link']);
  89. $width=imagesx($img);
  90. $height=imagesy($img);
  91. $edit=false;
  92. $valid[0]=true;
  93. $valid[1]=true;
  94. if ($width<=$_GET['width']) {
  95. $valid[0]=false;
  96. }
  97. if ($height<=$_GET['height']) {
  98. $valid[1]=false;
  99. }
  100. if ($width>$_GET['width']) {
  101. $roznica=$width-$_GET['width'];
  102. $wsp=$width/$roznica;
  103. $nHeight=$height/$wsp;
  104. $rHeight=$height-$nHeight;
  105. $rWidth=$_GET['width'];
  106. $edit=true;
  107. }
  108. if ($rHeight>$_GET['height']) {
  109. $roznica=$rHeight-$_GET['height'];
  110. $wsp=$rHeight/$roznica;
  111. $nWidth=$rWidth/$wsp;
  112. $rHeight=$_GET['height'];
  113. $rWidth=$rWidth-$nWidth;
  114. $edit=true;
  115. }
  116. if (!$edit && !$valid[0] && !$valid[1]) {
  117. $rWidth=$width;
  118. $rHeight=$height;
  119. }
  120. $img_mini=imagecreatetruecolor($rWidth,$rHeight);
  121. imagecopyresampled($img_mini,$img,0,0,0,0,$rWidth,$rHeight,$width,$height);
  122. header("Content-type: image/jpeg;");
  123. imagejpeg($img_mini);
  124. break;
  125. case 'jpeg':
  126. $img=imagecreatefromjpeg($_GET['link']);
  127. $width=imagesx($img);
  128. $height=imagesy($img);
  129. $edit=false;
  130. $valid[0]=true;
  131. $valid[1]=true;
  132. if ($width<=$_GET['width']) {
  133. $valid[0]=false;
  134. }
  135. if ($height<=$_GET['height']) {
  136. $valid[1]=false;
  137. }
  138. if ($width>$_GET['width']) {
  139. $roznica=$width-$_GET['width'];
  140. $wsp=$width/$roznica;
  141. $nHeight=$height/$wsp;
  142. $rHeight=$height-$nHeight;
  143. $rWidth=$_GET['width'];
  144. $edit=true;
  145. }
  146. if ($rHeight>$_GET['height']) {
  147. $roznica=$rHeight-$_GET['height'];
  148. $wsp=$rHeight/$roznica;
  149. $nWidth=$rWidth/$wsp;
  150. $rHeight=$_GET['height'];
  151. $rWidth=$rWidth-$nWidth;
  152. $edit=true;
  153. }
  154. if (!$edit && !$valid[0] && !$valid[1]) {
  155. $rWidth=$width;
  156. $rHeight=$height;
  157. }
  158. $img_mini=imagecreatetruecolor($rWidth,$rHeight);
  159. imagecopyresampled($img_mini,$img,0,0,0,0,$rWidth,$rHeight,$width,$height);
  160. header("Content-type: image/jpeg;");
  161. imagejpeg($img_mini);
  162. break;
  163. }
  164. }
  165. ?>


Nie wiem czy działa, nie testowałem.
adbacz
Z tego co wiem, to nikt Ci takiego skryptu sam z siebie nie da. Najlepiej gdybyś sam poczytał o funkcjach manipulowania obrazem (biblioteka GD w PHP). Nie dosyć, że się dowiesz nowych rzeczy i czegoś nauczysz to jeszcze Ci to zostanie na przyszłość i będziesz umiał napisac i sam ulepszyć ta funkcję (czy funkcje) o których mowa wyżej.
1q2w3e4r
Dziękuję za fatygę. Jednak ten skrypt skaluje tylko obrazek.
Nikt nie ma takich skryptów? Jeśli ktoś nie chce publicznie udostępniać proszę pisać na PW.
tehaha
co jak co, ale akurat gotowych klas do podstawowej obróbki zdjęć to w necie nie brakuje http://www.google.pl/search?q=php+thumbnai...lient=firefox-a, wypróbuj kilka i zobacz, która Ci najlepiej służy
kadlub
ja mam taką funkcję
1q2w3e4r
znalazłem funkcje, która wykonuje wszystko to co jest mi potrzebne:
http://asido.info/about/features/

Jednak przy zdjęciach jest duża strata jakości.

Gdy porównamy zdjęcia przerabiane np. tą funkcją:
  1. passthru("convert -size {$size[0]}x{$size[1]} $file -thumbnail $proportions $destination_dir");


Nikt na tym forum nie może mi podrzucić jakiejś dobrej funkcji, która by wykonywała to co asido ale bez strat jakosci?
rocktech.pl
Witam.

Może zainteresuje Cię rozszerzenie Imagick a funkcja to Imagick::thumbnailImage albo Imagick::cropThumbnailImage.
tehaha
Cytat
znalazłem funkcje, która wykonuje wszystko to co jest mi potrzebne:
http://asido.info/about/features/

Jednak przy zdjęciach jest duża strata jakości.


Poszukaj w dokumentacji czy da się ustawić parametr jakości, ponieważ Twój problem zapewne spowodowany jest tym, że ta funkcja http://php.net/manual/en/function.imagejpeg.php ma domyślnie parametr quality na 75, a Ty chcesz mieć 100. Jeżeli się nie da to musisz sobie to ręcznie ustawić
Dragas
Ja używam chyba od zawsze takiego skryptu:
  1. <?
  2. $imgname = $_GET["pic"];
  3.  
  4. $x_scale = $_GET["width"];
  5. list($width_img1, $height_img1) = getimagesize($imgname);
  6. if($height_img1 > $width_img1)
  7. {
  8. $hg = 640;
  9. $skala1 = $height_img1 / $hg;
  10. $x_scale = $width_img1 / $skala1;
  11. }
  12.  
  13. $check = @GetImageSize($imgname);
  14.  
  15. $width = $check[0];
  16. $height = $check[1];
  17. $mime = $check[2];
  18.  
  19. if ($height > 0) {
  20. $factor = $width/$height;
  21. $y_scale= @floor($x_scale/$factor);
  22. } else {
  23.  
  24. $y_scale=30;
  25. $width=$x_scale;
  26. $height=30;
  27. }
  28. function LoadImage($imgname) {
  29. global $mime, $width, $height;
  30. switch($mime){
  31. case 1:
  32. $im = @ImageCreateFromGif($imgname);
  33. break;
  34.  
  35. case 2:
  36. $im = @ImageCreateFromJpeg($imgname);
  37. break;
  38.  
  39. case 3:
  40. $im = @ImageCreateFromPng($imgname);
  41. break;
  42.  
  43. default:
  44. $im = false;
  45. break;
  46. }
  47. if (!$im) {
  48. $im = ImageCreate($width, $height);
  49. $bgc = ImageColorAllocate ($im, 255, 255, 255);
  50. $tc = ImageColorAllocate ($im, 0, 0, 0);
  51. ImageString($im, 1, 2, 2, "Bład: $imgname", $tc);
  52. ImageString($im, 1, 2, 15, "Brak pliku!", $tc);
  53. }
  54. return $im;
  55. }
  56. function ShowThumb($imgname) {
  57. global $x_scale, $y_scale, $width, $height;
  58.  
  59. $thumb = ImageCreateTrueColor($x_scale,$y_scale);
  60.  
  61. ImageCopyResampled($thumb, LoadImage($imgname), 0, 0, 0, 0,
  62. $x_scale, $y_scale, $width, $height);
  63.  
  64. return $thumb;
  65. }
  66.  
  67. if ($mime=='2') {
  68. Header("Content-type: image/jpeg");
  69. ImageJPEG(ShowThumb($imgname), "", 80);
  70. }
  71. elseif ($mime=='1') {
  72. Header("Content-type: image/gif");
  73. ImageGIF(ShowThumb($imgname));
  74. }
  75.  
  76. elseif ($mime=='3') {
  77. Header("Content-type: image/png");
  78. ImagePNG(ShowThumb($imgname));
  79. }
  80. else
  81. die("Ten serwer nie obsługuje funkcji graficznych w PHP");
  82. ?>


Skaluje on obrazki w locie, czyli wywołujesz mini.php?width=300&pic=obraz.png .
obsługuje pliki jpg, jpeg, gif (bez animacji) i png.

Pozdrawiam Dragas
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.