Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]kwadratowa miniatura z wyśrodkowanym zdjęciem
Forum PHP.pl > Forum > Przedszkole
kubax33
Generuję miniaturki PHPthumb działa, ale problem jest taki, że pomniejsza zdjęcia do różnych wysokości (proporcjonalnie), a chciałbym, aby zdjęcie było wyśrodkowane i zawsze miało np. 150x150 px wkomponowane w białe tło. Czy macie jakieś propozycje jak to zrobić?

  1. <?php
  2. if (!file_exists('thumbs/mini.jpg')) {
  3. require_once 'php/ThumbLib.inc.php';
  4. $obrazek = 'kurs.jpg';
  5. $rozmiar = getimagesize($obrazek);
  6. $wspolczynnik = $rozmiar[0]/$rozmiar[1];
  7.  
  8. if($wspolczynnik>1) {
  9. $h=98;
  10. $w=$h*$wspolczynnik;
  11. }
  12. if($wspolczynnik<=1) {
  13. $w=98;
  14. $h=$w/$wspolczynnik;
  15. }
  16.  
  17. $thumb = PhpThumbFactory::create($obrazek);
  18. $thumb->resize($w, $h);
  19. $thumb->save('thumbs/mini.jpg');
  20. }
  21. ?>


Korzystanie z PHPthumb nie jest dla mnie koniczne. Może jest jakaś klasa, która to obsłuży w taki właśnie sposób.
trueblue
http://funkcje.net/view/2/8/5054/index.html
kubax33
Dzięki znalazłem też coś takiego, ale mam jeden problem...

  1. function set_thumb($file, $photos_dir=photos,$thumbs_dir=thumbs, $square_size=150, $quality=150) {
  2. //check if thumb exists
  3. if (!file_exists($thumbs_dir."/".$file)) {
  4. //get image info
  5. list($width, $height, $type, $attr) = getimagesize($photos_dir."/".$file);
  6.  
  7. //set dimensions
  8. if($width> $height) {
  9. $width_t=$square_size;
  10. //respect the ratio
  11. $height_t=round($height/$width*$square_size);
  12. //set the offset
  13. $off_y=ceil(($width_t-$height_t)/2);
  14. $off_x=0;
  15. } elseif($height> $width) {
  16. $height_t=$square_size;
  17. $width_t=round($width/$height*$square_size);
  18. $off_x=ceil(($height_t-$width_t)/2);
  19. $off_y=0;
  20. }
  21. else {
  22. $width_t=$height_t=$square_size;
  23. $off_x=$off_y=0;
  24. }
  25.  
  26. $thumb=imagecreatefromjpeg($photos_dir."/".$file);
  27. $thumb_p = imagecreatetruecolor($square_size, $square_size);
  28. //default background is black
  29. $bg = imagecolorallocate ( $thumb_p, 255, 255, 255 );
  30. imagefill ( $thumb_p, 0, 0, $bg );
  31. imagecopyresampled($thumb_p, $thumb, $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height);
  32. imagejpeg($thumb_p,$thumbs_dir."/".$file,$quality);
  33. }
  34. }
  35. $file = 'kurs.jpg';
  36. echo set_thumb ($file);


  1. function set_thumb($file, $photos_dir=photos,$thumbs_dir=thumbs, $square_size=150, $quality=150) {


$photos_dir=photos,$thumbs_dir=thumbs

jeśli zrobię:
  1. $photos_dir=$folder1,$thumbs_dir=$folder2

wyrzuca mi błąd więc pytanie jakie apostrofy zastosować, aby było dobrze? chcę mieć foldery w zmiennych

Ok mam wychodzi coś takiego:

  1. <?php
  2.  
  3. function set_thumb($file, $photos_dir, $thumbs_dir, $square_size=150, $quality=150) {
  4. //check if thumb exists
  5. if (!file_exists($thumbs_dir.'/'.$file)) {
  6. //get image info
  7. list($width, $height, $type, $attr) = getimagesize($photos_dir.'/'.$file);
  8.  
  9. //set dimensions
  10. if($width> $height) {
  11. $width_t=$square_size;
  12. //respect the ratio
  13. $height_t=round($height/$width*$square_size);
  14. //set the offset
  15. $off_y=ceil(($width_t-$height_t)/2);
  16. $off_x=0;
  17. } elseif($height> $width) {
  18. $height_t=$square_size;
  19. $width_t=round($width/$height*$square_size);
  20. $off_x=ceil(($height_t-$width_t)/2);
  21. $off_y=0;
  22. }
  23. else {
  24. $width_t=$height_t=$square_size;
  25. $off_x=$off_y=0;
  26. }
  27.  
  28. $thumb=imagecreatefromjpeg($photos_dir.'/'.$file);
  29. $thumb_p = imagecreatetruecolor($square_size, $square_size);
  30. //default background is black
  31. $bg = imagecolorallocate ( $thumb_p, 255, 255, 255 );
  32. imagefill ( $thumb_p, 0, 0, $bg );
  33. imagecopyresampled($thumb_p, $thumb, $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height);
  34. imagejpeg($thumb_p,$thumbs_dir.'/'.$file,$quality);
  35. }
  36. }
  37. $photos_dir = 'photos';
  38. $thumbs_dir = 'thumbs';
  39. $file = 'kurs.jpg';
  40. echo set_thumb ($file, $photos_dir, $thumbs_dir);
  41. ?>


A odnośnie PHPthumb to generuje do kwadratu, ale ucina zdjęcia chyba nie ma tam takiego rozwiązania jak to. Zapewne kiedyś komuś się przyda.

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.