Witam. Otóż liznąłem trochę PHP ostatnio i chcąc pozmieniać swój kod strony(zrobiony na moje zlecenie) by skalowal obrazy wrzucane przez panel admina na wymiary 640x200 napotkalem na solidny opór. Próbowalem zmieniac parametr "$scale", "wh", "th" i nic! Obrazki slbo sie skalują tak samo albo obraz jesynie sie rozciaga ale wymiary zostają te same! Już sam nie wiem co zmienić. Oto kod:

  1.  
  2.  
  3. // FUNKCJA SPRAWDZAJACA WYMIARY PLIKU
  4. // JAK ROZDZIELCZOSC PLIKU JEST ZA DUZA WYSKAKUJE BLAD
  5. // TA FUNKCJA PRZERWIE PRACE W TAKIM WYPADKU
  6. function SprawdzWymiary($img_path)
  7. {
  8. $mw=2000; // MAXYMALNY WYMIAR SZEROKOSCI OBRAZKA
  9. $mh=2000; // MAXYMALNY WYMIAR WYSOKOSCI OBRAZKA
  10.  
  11. if(file_exists($img_path))
  12. {
  13. $img_attr = getimagesize($img_path);
  14. // $img_attr[0] - szerokosc, $img_attr[1] - wysoko��
  15.  
  16. if($img_attr[0] > $mw or $img_attr[0] > $mh)
  17. {
  18. echo '<div style="font-weight:bold; color:red;">Wyst�pi� b��d podczas dodawania obrazka ,zbyt du�a rozdzielczo�� !!!</div>
  19. Nazwa pliku: '.$img_path.'
  20. Rozdzielczo�� obrazka - szeroko�� : <b>'.$w.'</b> , wysoko�� <b> '.$h.'</b> <br />
  21. Maksymalna rozdzielczo�� obrazka - szeroko�� : <b>'.$mw.'</b> , wysoko�� <b>'.$mh.'</b> <br />';
  22. return false;
  23. }
  24. }
  25. return true;
  26. }
  27.  
  28.  
  29.  
  30. function MakeMinImg($img_path,$thumb_path,$max_width=100,$max_height=100)
  31. {
  32.  
  33. if(!SprawdzWymiary($img_path)) return false;
  34.  
  35. //Sprawdzamy czy obraz istnieje
  36. if(!file_exists($img_path))die('<p>Plik '.$img_path.' nie istnieje!!</p>');
  37.  
  38. //Pobieramy informacjie o obrazie
  39. $img_attr = getimagesize($img_path);
  40.  
  41. //Sprawdzamy czy obraz jest wiekszy na szeroko�� czy na wysoko��
  42. // i zmniejszamy odpowiednio rozmiar
  43. // $img_attr[0] - szerokosc, $img_attr[1] - wysoko��
  44. if($img_attr[0]>$img_attr[1]){
  45. $scale = $img_attr[0] / $max_width;
  46. }else{
  47. $scale = $img_attr[1] / $max_height;
  48. }
  49.  
  50. //Tworzymy obraz miniaturki
  51. $w = floor($img_attr[0]/$scale);
  52. $h = floor($img_attr[1]/$scale);
  53. $thumb = imagecreatetruecolor($w,$h);
  54.  
  55. //Kopiujemy obraz pomniejszajac go,
  56. imagecopyresampled($thumb, imagecreatefromjpeg($img_path), 0, 0, 0, 0, $w, $h, $img_attr[0], $img_attr[1]);
  57.  
  58. //Zapisujemy do pliku (jako�� 100)
  59. imagejpeg($thumb,$thumb_path,100);
  60. };
  61.  
  62. function miniaturka($o_file, $t_file, $t_wd = 100, $t_ht = 100, $prop= true)
  63. {
  64.  
  65. if(!SprawdzWymiary($t_file)) return false;
  66. $image_info = getImageSize($o_file) ; // let's get MIME Type
  67.  
  68. switch ($image_info['mime']) {
  69. case 'image/gif':
  70. if (imagetypes() & IMG_GIF) {
  71. $o_im = @imageCreateFromGIF($o_file);
  72. imagecolortransparent($o_im, imagecolorallocate ($o_im, 255, 255, 255));
  73. }
  74. else
  75. $ermsg = 'GIF images are not supported<br />';
  76. break;
  77. case 'image/jpeg':
  78. if (imagetypes() & IMG_JPG)
  79. $o_im = @imageCreateFromJPEG($o_file);
  80. else
  81. $ermsg = 'JPEG images are not supported<br />';
  82. break;
  83. default:
  84. $ermsg = $image_info['mime'].' images are not supported<br />';
  85. break;
  86. }
  87.  
  88. if (!isset($ermsg)) {
  89. $o_wd = imagesx($o_im);
  90. $o_ht = imagesy($o_im);
  91. if ($t_wd > $o_wd) $t_wd = $o_wd;
  92. if ($t_ht > $o_ht) $t_ht = $o_ht;
  93. if ($prop == true) {
  94. //check sizes if the width was deciding...
  95. // then thumbnail width = target * original width / original height
  96. $t_wd_n = floor($o_wd * $t_ht / $o_ht) ;
  97. //check sizes if the height was deciding...
  98. // then thumbnail height = target * original width / original height
  99. $t_ht_n = floor($o_ht * $t_wd / $o_wd) ;
  100.  
  101. // if calculated width is ok, we set it as target width
  102. if ($t_wd_n < $t_wd) {
  103. $t_wd = $t_wd_n;
  104. }
  105. // else check if calculated height is ok...
  106. else if ($t_ht_n < $t_ht) {
  107. $t_ht = $t_ht_n;
  108. }
  109. }
  110. $t_im = imageCreateTrueColor($t_wd,$t_ht);
  111.  
  112. $x=imageCopyResampled($t_im, $o_im, 0, 0, 0, 0, $t_wd, $t_ht, $o_wd, $o_ht);
  113. $y=imageJPEG($t_im,$t_file);
  114.  
  115. imageDestroy($o_im);
  116. imageDestroy($t_im);
  117. }
  118. return isset($ermsg)?$ermsg:NULL;
  119. }
  120.  
  121. function Kadruj($o_file, $t_file, $t_wd = 100, $t_ht = 100, $prop= true)
  122. {
  123. if(!SprawdzWymiary($t_file)) return false;
  124.  
  125. $image_info = getImageSize($o_file) ; // let's get MIME Type
  126.  
  127. switch ($image_info['mime']) {
  128. case 'image/gif':
  129. if (imagetypes() & IMG_GIF) {
  130. $o_im = @imageCreateFromGIF($o_file);
  131. imagecolortransparent($o_im, imagecolorallocate ($o_im, 0xff, 0xff, 0xff));
  132. }
  133. else
  134. $ermsg = 'GIF images are not supported<br />';
  135. break;
  136. case 'image/jpeg':
  137. if (imagetypes() & IMG_JPG)
  138. $o_im = @imageCreateFromJPEG($o_file);
  139. else
  140. $ermsg = 'JPEG images are not supported<br />';
  141. break;
  142. default:
  143. $ermsg = $image_info['mime'].' images are not supported<br />';
  144. break;
  145. }
  146.  
  147. if (!isset($ermsg)) {
  148. $o_wd = imagesx($o_im);
  149. $o_ht = imagesy($o_im);
  150.  
  151. /*if ($prop == true) {
  152.   //check sizes if the width was deciding...
  153.   // then thumbnail width = target * original width / original height
  154.   $t_wd_n = floor($o_wd * $t_ht / $o_ht) ;
  155.   //check sizes if the height was deciding...
  156.   // then thumbnail height = target * original width / original height
  157.   $t_ht_n = floor($o_ht * $t_wd / $o_wd) ;
  158.  
  159.   // if calculated width is ok, we set it as target width
  160.   if ($t_wd_n < $t_wd) {
  161.   $t_wd = $t_wd_n;
  162.   }
  163.   // else check if calculated height is ok...
  164.   else if ($t_ht_n < $t_ht) {
  165.   $t_ht = $t_ht_n;
  166.   }
  167.   }*/
  168.  
  169.  
  170. //$x=imageCopyResampled($t_im, $o_im, 0, 0, 0, 0, $t_wd, $t_ht, $o_wd, $o_ht);
  171.  
  172. $w = $t_wd;
  173. $h = $t_ht;
  174. $orig_w = $o_wd;
  175. $orig_h = $o_ht;
  176. $ratio = $w/$h;
  177. //$ratio = 500/375; //na sztywno proporcje ;)
  178. //$h = round($w/$ratio);//na sztywno proporcje ;)
  179.  
  180. $orig_ratio = $orig_w/$orig_h;
  181. //echo "ratio=$ratio, orig_ratio=$orig_ratio <br>";
  182. if($orig_ratio>$ratio) {
  183. $neww = $w;
  184. $newh = $h;
  185. $yp = 0;
  186. $xp = round( $orig_w*($orig_ratio-$ratio)/2 );
  187. //echo "xp=$xp yp=$yp";
  188. $copyw = $orig_w-($orig_w*($orig_ratio-$ratio));
  189. $copyh = $orig_h;
  190. }
  191. else {
  192. $neww = $w;
  193. $newh = $h;
  194. $xp = 0;
  195. $yp = round(($orig_h*(1/$orig_ratio-1/$ratio))/2);
  196. $copyh = $orig_h-($orig_h*(1/$orig_ratio-1/$ratio));
  197. $copyw = $orig_w;
  198. }
  199. $t_im = imageCreateTrueColor($w,$h);
  200. imagecopyresampled($t_im, $o_im, 0, 0, $xp, $yp, $neww, $newh, $copyw, $copyh);
  201. $y=imageJPEG($t_im,$t_file);
  202.  
  203. imageDestroy($o_im);
  204. imageDestroy($t_im);
  205. }
  206. return isset($ermsg)?$ermsg:NULL;
  207. }
  208.  
  209. function SkalujKadruj($img_path,$thumb_path,$max_width=100,$max_height=100)
  210. {
  211. if(!SprawdzWymiary($img_path)) return false;
  212.  
  213. $X = $max_width;
  214. $Y = $max_height;
  215.  
  216. $s = imagecreatefromjpeg($img_path);
  217. if ( !$s ) die ("imagecreatefromjpeg");
  218.  
  219. $sx = imagesx($s);
  220. $sy = imagesy($s);
  221.  
  222. if($X > $sx) $X = $sx;
  223. if ($Y > $sy) $Y = $sy;
  224.  
  225. $d = imagecreatetruecolor($X, $Y);
  226.  
  227.  
  228. $xy = $X / (float) $Y;
  229. if ( $sx/ (float) $sy > $xy)
  230. {
  231. $nx = $xy * $sy;
  232. $ny = $sy;
  233. $px = ($sx - $nx) /2;
  234. $py = 0;
  235. }
  236. else
  237. {
  238. $ny = $sx / $xy;
  239. $nx = $sx;
  240. $py = ($sy - $ny) /2;
  241. $px = 0;
  242. }
  243. imagecopyresampled ( $d, $s, 0, 0, $px, $py, $X, $Y, $nx, $ny );
  244. imagedestroy($s);
  245.  
  246. imagejpeg($d,$thumb_path);
  247. imagedestroy($d);
  248. }
  249.  
  250.  
  251.  
  252.