Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Tworzenie miniatur, proporcjonalnosc ;)
bumfank
post
Post #1





Grupa: Zarejestrowani
Postów: 87
Pomógł: 0
Dołączył: 29.02.2004
Skąd: /dev/null

Ostrzeżenie: (0%)
-----


Witam serdecznie (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) Napisalem skrypta ( prawie ;P ) w oparciu o klase resizeImage, skrypt uploaduje zdjecie, wrzuca na serwer oryginal size i w 2 mniejszych wersjach, ale problem tkwi w tym zeby dopasowywal zdjecie, zeby byly zachowane proporcje czyli np zdefiniowac sama szerokosc na 200 wtedy on dlugosc sam ustawi wedlug wielkosci zdjecia, nie wiem jak to zrobic, szukalem na googlach o tej proporcjonalnosci ale nic nie znalazlem, oczywiscie moglem zle szukac, niech ktos mnie poprawi (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
nizej caly skrypt co i jak (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

prosze o pomoc

  1. <?php
  2.  
  3. if($opcja == 'form')
  4. {
  5. <form enctype="multipart/form-data"
  6.  method="post" action="?send=Wyslij">
  7.  <input name="src" type="file">
  8.  <br><input type="submit" value="Wyslij" name="send"><input type="reset" value="Kasuj" name="B2"></form>
  9. ";
  10.  
  11. }
  12.  
  13. if($send == 'Wyslij') {
  14.  
  15.  
  16. $strUploadDir = 'images/artist/' . $_FILES['src']['name'];
  17. $strUploadDir2 = 'images/artist/th_' . $_FILES['src']['name'];
  18. $strUploadDir3 = 'images/artist/th2_' . $_FILES['src']['name'];
  19. move_uploaded_file( $_FILES['src']['tmp_name'], $strUploadDir );
  20.  
  21. function imgType($name)
  22. {
  23.  if(substr($name, -4, 4) == '.jpg' || substr($name, -4, 4) == 'jpeg')
  24.  {
  25. return "IMAGETYPE_JPEG";
  26.  }
  27.  elseif(substr($name, -4, 4) == '.gif')
  28.  {
  29. return "IMAGETYPE_GIF";
  30.  }
  31.  elseif(substr($name, -4, 4) == '.png')
  32.  {
  33. return "IMAGETYPE_PNG";
  34.  }
  35. }
  36.  
  37. function resizeImage($source, $max_x, $max_y, $save_image, $jpeg_quality = 100)
  38. {
  39.  /*
  40. * source - obrazek jpeg
  41. * max_x - maksymalna szerokosc pomniejszonego obrazka
  42. * max_y - maksymalna dlugosc pomniejszonego obrazka
  43. * save_image - nazwa pliku do ktorego zostanie zapisany nowy obrazek
  44. * jpeg_quality - jakosc powstalego obrazu jpeg - jezeli bedzie inny to argument jest nie wazny (domyslnie 100)
  45. */
  46.  
  47. if(imgType($source) == "IMAGETYPE_JPEG")
  48. {
  49.  $img_src = imagecreatefromjpeg($source);
  50. }
  51. elseif(imgType($source) == "IMAGETYPE_GIF")
  52. {
  53.  $img_src = imagecreatefromgif($source);
  54. }
  55. elseif(imgType($source) == "IMAGETYPE_PNG")
  56. {
  57.  $img_src = imagecreatefrompng($source);
  58. }
  59. else
  60. {
  61.  die('Wrong filetype! Accepted images: JPG/JPEG, GIF, PNG');
  62. }
  63.  
  64.  $image_x = imagesx($img_src);
  65.  $image_y = imagesy($img_src);
  66.  if($image_x > $image_y) // Landscape
  67.  {
  68. $ratio_x = ($image_x > $max_x) ? $max_x/$image_x : 1;
  69. $ratio_y = $ratio_x;
  70. $move = 'y';
  71.  }
  72.  else // Portrait
  73.  {
  74. $ratio_y = ($image_y > $max_y) ? $max_y/$image_y : 1;
  75. $ratio_x = $ratio_y;
  76. $move = 'x';
  77.  }
  78. $new_x = $image_x*$ratio_x;
  79. $new_y = $image_y*$ratio_y;
  80.  
  81. $move_x = ($move == "x") ? ($max_x-$new_x)/: 0;
  82. $move_y = ($move == "y") ? ($max_y-$new_y)/: 0;
  83.  
  84. $new_img = imagecreatetruecolor($max_x, $max_y);
  85. $background = imagecolorallocate($new_img, 255, 255, 255);
  86. imagefill($new_img, 0, 0, $background);
  87. imagecopyresampled($new_img, $img_src, $move_x, $move_y, 0, 0, $new_x, $new_y, $image_x, $image_y);
  88.  
  89. if(imgType($save_image) == "IMAGETYPE_JPEG")
  90. {
  91.  imagejpeg($new_img, $save_image, 100);
  92. }
  93. elseif(imgType($save_image) == "IMAGETYPE_GIF")
  94. {
  95.  imagegif($new_img, $save_image);
  96. }
  97. elseif(imgType($save_image) == "IMAGETYPE_PNG")
  98. {
  99.  imagepng($new_img, $save_image);
  100. }
  101.  
  102. }
  103.  
  104. resizeImage($strUploadDir, 200, 100, $strUploadDir2, 100);
  105. echo'<img src="'.$strUploadDir2.'" alt="">';
  106.  
  107. resizeImage($strUploadDir, 100, 80, $strUploadDir3, 100);
  108. echo'<img src="'.$strUploadDir3.'" alt="">';
  109.  
  110.  
  111.  
  112.  
  113.  
  114. }
  115. ?>



dzieki za pomoc (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

pozdrawiam (IMG:http://forum.php.pl/style_emoticons/default/exclamation.gif) (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

Ten post edytował bumfank 8.08.2005, 23:17:17
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 16:58