Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Problem z zapisaniem zdjęcia ze zmienioną wielkością
goofynow
post
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 10.03.2012

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


Witam mam problem. Chciałem aby dodawane zdjęcia zmieniało sie na przeliczona wielkość ,a i tak sie zapisuje jako orginalna wielkość. Więc co robie źle? Manuala przglądałem i nadal nie mam pojęcia o co biega.

  1. if (move_uploaded_file($_FILES['image_filename']['tmp_name'],
  2. $ImageName)) {
  3.  
  4. // pobranie informacji na temat umieszczonego obrazu
  5. list($width, $height, $type, $attr) = getimagesize($ImageName);
  6.  
  7.  
  8. if ($type > 3) {
  9. echo "Przykro nam, ale przesłany obraz nie jest w formacie GIF, JPG lub " .
  10. "PNG.<br>";
  11. echo "Kliknij przycisk 'Wstecz' w przeglądarce i spróbuj ponownie.";
  12. } else {
  13.  
  14.  
  15. // wstawienie informacji w tabeli images
  16.  
  17. $insert = "INSERT INTO img
  18. (img_nazwa, img_date, img_fot, img_min,img_kat)
  19. VALUES
  20. ('$img_nazwa', '$today', '$img_fot', '$img_min', '$img_kat')";
  21. $insertresults = mysql_query($insert)
  22.  
  23. $lastpicid = mysql_insert_id();
  24.  
  25. $newfilename = $ImageDir . $lastpicid . ".jpg";
  26.  
  27. // przeliczenie wielkości zdjęcia
  28. $max_width = 1024;
  29. $max_height = 1024;
  30.  
  31. $new_width = $max_width;
  32. $ratio = $max_width / $width;
  33. $new_height = $height * $ratio;
  34.  
  35.  
  36. if ($type == 2) {
  37. rename($ImageName, $newfilename);
  38. } else {
  39. if ($type == 1) {
  40. $image_old = imagecreatefromgif($ImageName);
  41. } elseif ($type == 3) {
  42. $image_old = imagecreatefrompng($ImageName);
  43. }
  44. // skonwertuj obraz do formatu JPG
  45. $image_jpg = imagecreatetruecolor($new_width, $new_height);
  46. imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  47. imagejpeg($image_jpg, $newfilename);
  48. imagedestroy($image_old);
  49. imagedestroy($image_jpg);
  50. }
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
goofynow
post
Post #2





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 10.03.2012

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


W sumie nie zauważyłem że mi odpisałeś ale sam też doszedłem dzięki Twoim podpowiedziom i coś takiego mi wyszło.

  1. // udostępnienie zmiennych
  2. $img_nazwa = $_POST['img_nazwa'];
  3. $img_fot = "fot/";
  4. $img_min = "fot/min/";
  5. $image_tempname = $_FILES['image_filename']['name'];
  6. $today = date("Y-m-d");
  7. $img_kat = $_POST['kategorie'];
  8.  
  9.  
  10.  
  11. // umieszczenie obrazu i sprawdzenie jego formatu
  12. $ImageDir = "fot/";
  13.  
  14.  
  15. // folder minaturek
  16. $ImageThumb = $ImageDir . "min/";
  17.  
  18.  
  19. $ImageName = $ImageDir . $image_tempname;
  20.  
  21. if (move_uploaded_file($_FILES['image_filename']['tmp_name'],
  22. $ImageName)) {
  23.  
  24. // pobranie informacji na temat umieszczonego obrazu
  25. list($width, $height, $type, $attr) = getimagesize($ImageName);
  26.  
  27.  
  28. if ($type > 3) {
  29. echo "Przykro nam, ale przesłany obraz nie jest w formacie GIF, JPG lub " .
  30. "PNG.<br>";
  31. echo "Kliknij przycisk 'Wstecz' w przeglądarce i spróbuj ponownie.";
  32. } else {
  33.  
  34.  
  35. // wstawienie informacji w tabeli images
  36.  
  37. $insert = "INSERT INTO img
  38. (img_nazwa, img_date, img_fot, img_min,img_kat)
  39. VALUES
  40. ('$img_nazwa', '$today', '$img_fot', '$img_min', '$img_kat')";
  41. $insertresults = mysql_query($insert)
  42.  
  43. $lastpicid = mysql_insert_id();
  44.  
  45. $newfilename = $ImageDir . $lastpicid . ".jpg";
  46.  
  47.  
  48.  
  49. if ($type == 2) {
  50.  
  51. $max_width = 1024;
  52. $max_height = 1024;
  53.  
  54. $new_width = $max_width;
  55. $ratio = $max_width / $width;
  56. $new_height = $height * $ratio;
  57.  
  58. rename($ImageName, $newfilename);
  59. $image = imagecreatefromjpeg($newfilename);
  60. $imagenew = imagecreatetruecolor($new_width, $new_height);
  61. imagecopyresampled($imagenew, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  62. imagejpeg($imagenew, $newfilename);
  63. imagedestroy($image);
  64. imagedestroy($imagenew);
  65. } else {
  66. if ($type == 1) {
  67. $image_old = imagecreatefromgif($ImageName);
  68. } elseif ($type == 3) {
  69. $image_old = imagecreatefrompng($ImageName);
  70. }
  71. $max_width = 1024;
  72. $max_height = 1024;
  73.  
  74. $new_width = $max_width;
  75. $ratio = $max_width / $width;
  76. $new_height = $height * $ratio;
  77.  
  78. $image_jpg = imagecreatetruecolor($new_width, $new_height);
  79. imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  80. imagejpeg($image_jpg, $newfilename);
  81. imagedestroy($image_old);
  82. imagedestroy($image_jpg);
  83.  
  84. }
  85.  
  86. $newthumbname = $ImageThumb . $lastpicid . ".jpg";
  87.  
  88.  
  89. $max_width = 1024;
  90. $max_height = 1024;
  91.  
  92. $new_width = $max_width;
  93. $ratio = $max_width / $width;
  94. $new_height = $height * $ratio;
  95.  
  96. // pobierz wymiary miniaturki
  97. $thumb_width = $new_width * 0.10;
  98. $thumb_height = $new_height * 0.10;
  99.  
  100. // utwórz miniaturkę
  101. $largeimage = imagecreatefromjpeg($newfilename);
  102. $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
  103. imagecopyresampled($thumb, $largeimage, 0, 0, 0, 0,
  104. $thumb_width, $thumb_height, $new_width, $new_height);
  105. imagejpeg($thumb, $newthumbname);
  106. imagedestroy($largeimage);
  107. imagedestroy($thumb);
  108.  
  109. }
  110.  
  111. }
  112. ?>


Wielkie dzieki Za pomoc

Ten post edytował goofynow 30.03.2012, 13:12:28
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: 8.10.2025 - 20:52