Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Upload
Forum PHP.pl > Forum > Przedszkole
dave666
Mam o to skrypt uploadu zdjęć który ma mozliwość zmniejszania zdjęć ale zmniejsza je o ileś procent no tyle ile ustalimy a chcialbym aby zmniejszał do danej wielkości czy mgłby mnie ktos nakierowac jak to mogę zrobic ?
  1. <?php
  2. # Variable
  3. $new_height = 0.5; // new height(1 is 100%, 0.1 is 10%)
  4. $new_width = 0.5; // new width
  5. $quality = 50; // quality(100 = 100%)
  6. $folder = 'zdjecia/'; // folder with images
  7.  
  8. # Functions
  9. //function give new random name for file
  10. function unid(){
  11.    while(substr($result, 0, 1) == 0){
  12.        $number = md5 (uniqid (rand()));
  13.        $vowels = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "o", "u", "A", "E", "I", "O", "U");
  14.        $onlyconsonants = str_replace($vowels, "", $number);
  15.        $result = substr($onlyconsonants, 0, 7);
  16.    }
  17.    return $result;
  18. }
  19.  
  20. function LoadJpeg($imgname, $new_height, $new_width)
  21. {
  22.    /* Attempt to open */
  23.    $img = @imagecreatefromjpeg($imgname);
  24.    
  25.    /* See if it failed */
  26.    if(!$img)
  27.    {
  28.    
  29.        /* Create a black image */
  30.        $img_mini  = imagecreatetruecolor(150, 30);
  31.        $bgc = imagecolorallocate($img_mini, 255, 255, 255);
  32.        $tc  = imagecolorallocate($img_mini, 0, 0, 0);
  33.  
  34.        imagefilledrectangle($img_mini, 0, 0, 150, 30, $bgc);
  35.  
  36.        /* Output an error message */
  37.        imagestring($img_mini, 1, 5, 5, 'Error loading ' . $imgname, $tc);
  38.    }
  39.    else{
  40.        $width  = imagesx($img);
  41.        $height = imagesy($img);
  42.        
  43.        $width_mini = $width * $new_width;
  44.        $height_mini = $height * $new_height;
  45.        $img_mini = imagecreatetruecolor($width_mini, $height_mini);
  46.        Imagecopyresampled($img_mini, $img, 0, 0, 0, 0, $width_mini , $height_mini, $width  , $height);
  47.    }
  48.  
  49.    return $img_mini;
  50. }
  51.  
  52. # MAIN
  53. if($_FILES['photo']){
  54.    # uploading file to server
  55.    if (is_uploaded_file($_FILES['photo']['tmp_name'])) {
  56.        echo "Plik ". $_FILES['photo']['name'] ." zaladowany Poprawnie.\n";
  57.        move_uploaded_file($_FILES['photo']['tmp_name'], $folder.$_FILES['photo']['name']);
  58.    } else {
  59.        echo "Upload Niewykonany Error: ";
  60.        echo "Plik '". $_FILES['photo']['tmp_name'] . "'.";
  61.    }
  62.  
  63.    # resizing image and saving new file on server
  64.    $new_name = $folder.unid().'.jpg';
  65.    while(file_exists($new_name)){
  66.        $new_name = $folder.unid().'.jpg';
  67.    }
  68.    $img = LoadJpeg($folder.$_FILES['photo']['name'], $new_height, $new_width);
  69.    imagejpeg($img, $new_name, $quality);
  70.    
  71.  
  72.  
  73.    # deleting old file
  74.    imagedestroy($img);
  75.    unlink($folder.$_FILES['photo']['name']);
  76.  
  77. }
  78. else{
  79. ?>
  80. Wgraj zdjecia:
  81.  
  82.    <form action="index.php?admin=upload" method="post" enctype="multipart/form-data">
  83.    <input type="file" name="photo" />
  84.    <input type="submit" value="wgraj" />
  85.    </form>
  86. <?php
  87. }
  88. ?>
nexis
Zamiast
  1. <?php
  2. $width_mini = $width * $new_width;
  3. $height_mini = $height * $new_height;
  4. ?>

wstaw
  1. <?php
  2. if ($new_width/$new_height > $width/$height) {
  3.    $width_mini  = floor( $width/($height/$new_height) );
  4.    $height_mini = $new_height;
  5. } else {
  6.    $width_mini  = $new_width;
  7.    $height_mini = floor( $height/($width/$new_width) );
  8. }
  9. ?>

I oczywiście na początku nadaj zmiennym $new_height oraz $new_width odpowiednie wartości.
dave666
Oczywiście "nexis" Pomógł za co bardzo dziękuję
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.