Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Skalowanie obrazka i jego zapis
wgrzelak
post
Post #1





Grupa: Zarejestrowani
Postów: 22
Pomógł: 0
Dołączył: 22.12.2007

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


No więc mój problem polega na tym że mam upload obrazka i potrzebuje żeby obrazek się skalował i zapisywał proporcjonalnie i żeby była funkcja utraty jakosci.

  1. <?php
  2. $userfile = $_FILES["userfile"];
  3. $uploadfile = $GLOBALS["uploaddir"] . "".$hash.".jpg";
  4. $image_types = Array ("image/bmp","image/jpeg", "image/pjpeg","image/gif","image/x-png");
  5. $file_name = "".$hash.".jpg";
  6. $file_size = $_FILES["userfile"]["size"];
  7. $file_type = $_FILES["userfile"]["type"];
  8. $file_size = makesize1($file_size);
  9.  
  10. if (isset($_FILES["userfile"]))
  11. {
  12. if (!$file_name)
  13. {
  14. // do nothing...
  15. }
  16. else
  17. {
  18. if ($file_size > $GLOBALS["file_limit"])
  19. {
  20. err_msg (ERROR,FILE_UPLOAD_TO_BIG."$file_limit KB, ".IMAGE_WAS." $file_size");
  21. blocksx_end();
  22. stdfoot();
  23. }
  24. if (in_array (strtolower ($file_type), $image_types, TRUE))
  25. {
  26. if (@move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
  27. {
  28. echo"<center>Upload obrazka pomyślny</center><br />";
  29. }
  30. else
  31. {
  32. err_msg (ERROR,MOVE_IMAGE_TO.$GLOBALS["uploaddir"].$file_name.CHECK_FOLDERS_PERM);
  33. blocksx_end();
  34. stdfoot();
  35. }
  36. }
  37. else
  38. {
  39. err_msg (ERROR,ILEGAL_UPLOAD);
  40. blocksx_end();
  41. stdfoot();
  42. }
  43. }
  44. }
  45. ?>


Ten post edytował wgrzelak 26.12.2007, 21:49:43
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
andrew1985
post
Post #2





Grupa: Zarejestrowani
Postów: 48
Pomógł: 4
Dołączył: 28.12.2007

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


Tu masz moją funkcję która to robi:
Opis paramterów:
$img_name = nazwa pliku (np. obraz.jpg)
$photo_dir = katalog z plikiem
$thumb_dir = katalog so którego zostanie zapisany przekonwertowany plik
$max_width = maksymalna szerokosc
$max_height = maksymalna wysokosc
$proportion=false = czy ma zachowac proporcje czy nie - w twoim przypadku true

W przypadku prawidłowego skonwertowania obrazka zwraca ściezke do niego.

Kod
function ImageConvert($img_name,$photo_dir,$thumb_dir, $max_width,$max_height,$proportion=false){
    $img_path = $photo_dir.$img_name;
    $thumb_path = $thumb_dir.$img_name;
    
    if(file_exists($img_path)) {
        $img_attr = getimagesize($img_path);
        
        if($proportion){
            if($img_attr[0]>$img_attr[1])
                $scale = $img_attr[0] / $max_width;
            else $scale = $img_attr[1] / $max_height;
            
            $w = floor($img_attr[0]/$scale);
            $h = floor($img_attr[1]/$scale);
        }else{
            $w = $max_width;
            $h = $max_height;
        }
        
        $thumb = imagecreatetruecolor($w,$h);
        
        $result = @imagecopyresampled($thumb, imagecreatefromjpeg($img_path), 0, 0, 0, 0, $w, $h, $img_attr[0], $img_attr[1]);        
        if($result)
            @imagejpeg($thumb,$thumb_path,85);

        if($result) return $thumb_path; else return $result;
    } else return -1;
}
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: 12.10.2025 - 22:40