Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Zmniejszanie fotek w PHP do określonego rozmiaru
Forum PHP.pl > Forum > Przedszkole
northwest
Witam serdecznie,
Mam problem ze zmniejszaniem zdjęć w PHP sad.gif
Mam następujący kod:
  1. class Image
  2. {
  3. const JPG = 'jpg';
  4. const JPEG = 'jpeg';
  5. const GIF = 'gif';
  6. const PNG = 'png';
  7.  
  8. private $handle;
  9. private $width;
  10. private $height;
  11. private $imageType;
  12.  
  13. function __construct($fileName = '')
  14. {
  15. if ($fileName)
  16. {
  17. $this->open($fileName);
  18. }
  19. }
  20.  
  21. function __destruct()
  22. {
  23. if ($this->handle)
  24. {
  25. $this->close();
  26. }
  27. }
  28.  
  29. public function setImageType($imageType)
  30. {
  31. $this->imageType = $imageType;
  32. return $this;
  33. }
  34.  
  35. public function getImageType()
  36. {
  37. return $this->imageType;
  38. }
  39.  
  40. public function create($imageType, $width, $height)
  41. {
  42. $this->imageType = $imageType;
  43. $this->handle = imagecreatetruecolor($width, $height);
  44.  
  45. $this->width = $width;
  46. $this->height = $height;
  47. return $this;
  48. }
  49.  
  50. /**
  51.   * Otwarcie obrazu
  52.   * @param string $fileName Sciezka/nazwa pliku
  53.   */
  54. public function open($fileName)
  55. {
  56. if (!file_exists($fileName))
  57. {
  58. throw new Exception("$fileName does not exist");
  59. }
  60.  
  61. $this->imageType = strtolower(end(explode('.', $fileName)));
  62. list($this->width, $this->height, , ,) = @getimagesize($fileName);
  63.  
  64. switch ($this->imageType)
  65. {
  66. case 'jpeg':
  67. case 'jpg':
  68. $this->handle = @imagecreatefromjpeg($fileName);
  69. break;
  70.  
  71. case 'gif':
  72. $this->handle = @imagecreatefromgif($fileName);
  73. break;
  74.  
  75. case 'png':
  76. $this->handle = @imagecreatefrompng($fileName);
  77. break;
  78.  
  79. default:
  80. throw new Exception('Unsupported image format');
  81. }
  82. }
  83.  
  84. public function getHandle()
  85. {
  86. return $this->handle;
  87. }
  88.  
  89. /**
  90.   * Zamyka uchwyt i konczy prace z obrazem
  91.   */
  92. public function close()
  93. {
  94. @imagedestroy($this->handle);
  95. }
  96.  
  97. /**
  98.   * Zwraca szerokosc obrazu
  99.   * @return int
  100.   */
  101. public function getWidth()
  102. {
  103. return $this->width;
  104. }
  105.  
  106. /**
  107.   * Zwraca wysokosc obrazu
  108.   * @return int
  109.   */
  110. public function getHeight()
  111. {
  112. return $this->height;
  113. }
  114.  
  115. /**
  116.   * Zmiara rozmiaru obrazu do wartosci podanych w parametrach
  117.   * @param int $width
  118.   * @param int $height
  119.   */
  120. public function resize($width, $height)
  121. {
  122. $image = @imagecreatetruecolor($width, $height);
  123. //$this->handle = imagealphablending ($image, true);
  124. @imagecopyresampled($image, $this->handle, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
  125.  
  126. $this->handle = $image;
  127. $this->width = $width;
  128. $this->height = $height;
  129. }
  130.  
  131. /**
  132.   * Konwersja do systemu szestnastkowego
  133.   * @param int $red
  134.   * @param int $green
  135.   * @param int $blue
  136.   * @return string
  137.   */
  138. public function rgb2hex($red, $green, $blue)
  139. {
  140. return sprintf('#%02s%02s%02s', dechex($red), dechex($green), dechex($blue));
  141. }
  142.  
  143. /**
  144.   * Konwersja liczby szestnastkowej okreslajacej kolor, do tablicy liczb RGB
  145.   * @param string $hex np. #ccc lub #f8f8f8
  146.   * @return array
  147.   */
  148. public function hex2rgb($hex)
  149. {
  150. if ($hex{0} == '#')
  151. {
  152. $hex = substr($hex, 1);
  153. }
  154. if (strlen($hex) == 6)
  155. {
  156. list($red, $green, $blue) = array(substr($hex, 0, 2), substr($hex, 2, 2), substr($hex, 4, 2));
  157. }
  158. elseif (strlen($hex) == 3)
  159. {
  160. list($red, $green, $blue) = array($hex[0] . $hex[0], $hex[1] . $hex[1], $hex[2] . $hex[2]);
  161. }
  162. else
  163. {
  164. return false;
  165. }
  166.  
  167. return array(hexdec($red), hexdec($green), hexdec($blue));
  168. }
  169.  
  170. /**
  171.   * Metoda generuje miniature o podanych rozmiarach
  172.   * @param int $width Szerokosc w px
  173.   * @param int $height Wysokosc w px
  174.   * @param strig $color Kolor tla dla miniatury
  175.   */
  176. public function thumbnail($width, $height, $color = '#FFF')
  177. {
  178. // jezeli wysokosc jest MNIEJSZA niz szerokosc
  179. // obraz poziomy
  180. if ($this->height < $this->width)
  181. {
  182. $ratio = $this->getHeight() / $this->getWidth();
  183. $cHeight = $width * $ratio;
  184.  
  185. $this->resize($width, $cHeight);
  186. }
  187. // obraz pionowy
  188. else
  189. {
  190. $ratio = $this->getWidth() / $this->getHeight();
  191. $cWidth = $height * $ratio;
  192.  
  193. $this->resize($cWidth, $height);
  194. }
  195.  
  196. list($red, $green, $blue) = $this->hex2rgb($color);
  197. $output = imagecreatetruecolor($width, $height);
  198.  
  199. $color = imagecolorallocate($output, $red, $green, $blue);
  200. imagefill($output, 0, 0, $color);
  201.  
  202. imagecopy($output, $this->handle, round(($width - $this->getWidth()) / 2), round(($height - $this->getHeight()) / 2), 0, 0, $this->getWidth(), $this->getHeight());
  203.  
  204. $this->handle = $output;
  205. }
  206.  
  207. /**
  208.   * Zapis obrazu do pliku
  209.   * @param $fileName Nazwa (sciezka) pliku
  210.   */
  211. public function save($fileName)
  212. {
  213. switch ($this->imageType)
  214. {
  215. case 'jpg':
  216. case 'jpeg':
  217. @imagejpeg($this->handle, $fileName, 80);
  218. break;
  219.  
  220. case 'gif':
  221. @imagegif($this->handle, $fileName);
  222. break;
  223.  
  224. case 'png':
  225. @imagepng($this->handle, $fileName);
  226. break;
  227. }
  228. }
  229. }
  230.  
  231. $image = new Image("pliki/" . "" . $f1a);
  232. $image->thumbnail(160, 240, '#FFFFFF');
  233. $image->save("pliki/" . "mini222_" . $f1a); // miniatura
  234. $image->close();


Zdjęcie źródłowe wygląda następująco:


skrypt obrabia mi fotkę do czegoś takiego:



a Chciałbym żeby to wyglądało następująco:


Czyli całe zdjęcie, sformatowane tak ażeby się mieściło proporcjonalnie... smile.gif

Generalnie mam różnego rodzaju fotki i chciałbym żeby miniaturka tak operowała tymi białymi paskami ażeby zawsze całe fotki były widoczne...

Macie pomysł co mam źle w moim skrypcie?


North
northwest
to chyba troszkę nie to sad.gif
Szymciosek
http://www.white-hat-web-design.co.uk/blog...mages-with-php/

a takie coś ?
northwest
to też nie to.... nie ma białego tła pod nie wymiarowym zdjęciem sad.gif
thek
Problem jest taki, ponieważ wrzucasz zdjęcie po zmniejszeniu bez sprawdzania proporcji boków. To co chcesz, przewija się przez to forum średnio co 1-2 tygodnie, więc używając szukajki tutaj powinno znaleźc naprawdę wiele tematów z rozwiązaniem. Podstawa to wiedzieć który bok po przeskalowaniu zostaje niezmieniony, a który trafia na nieprawidłową wartośc i ulegnie przesunięciu względem wynikowego. Twój kod niby powinien to robić, ale funkcja imagecopy nie przelicza tego prawidłowo, więc lepiej sobie zdebuguj wartości jakie tam wchodzą i jakie wychodzą.
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.