2013-03-05 67 views
0

我想要一個用戶可以一次上傳多張圖片的網站,如何縮小單個用戶上傳的多張圖片的大小。
我的代碼中的DS是在初始化文件中定義的DIRECTORY_SEPARATOR。減少多次上傳的圖片大小

if(isset($_POST['submitImage'])){  
     $file = $_FILES['upload']; 
     $errors = array(); 

     if(empty($file)){ 
      $errors[] = ' The file could not be empty '; 
     }else{ 

      foreach($file['name'] as $key => $value){ 
       $extensions = array("jpeg","jpg","png"); 
       $file_ext=explode('.',$file['name'][$key]) ; 
       $file_ext=end($file_ext); 

       if(in_array($file_ext,$extensions) === false){ 
        $errors[]="extension not allowed"; 
       }else{ 

        $filetmp = $file['tmp_name'][$key]; 
        $terget_path = SITE_ROOT.'j2reimage'.DS; 
        $dat = strftime("%Y-%m-%d %H:%M:%S", time()); 

        if(file_exists($terget_path)){ 
         move_uploaded_file($filetmp, $terget_path.$file['name'][$key]);     
         mysql_query("insert into 4j2memberimage values ('', $memmem->id,'{$file['name'][$key]}', '$dat')"); 

        //end of if file_exists  
        }else{ 
         $errors[] = ' Upload extention could not be locate';    
        } 
       } 
        //redirect_to('inb.php');     
      } 
     } 





} 
+1

http://php.net/manual/en/function.imagecopyresampled.php – 2013-03-05 17:46:17

+0

感謝它按我的預期工作。 – Adesanwo 2013-03-05 18:38:35

+0

我將添加此作爲答案,以便當谷歌拿起它時,別人可以很容易地看到答案是正確的。如果你不介意,我會很感激,如果你把問題標記爲回答:) – 2013-03-05 20:46:25

回答

0

從PHP.net發表評論,但移動到答案,因爲它修復了OPs問題。

<?php 
// The file 
$filename = 'test.jpg'; 
$percent = 0.5; 

// Content type 
header('Content-Type: image/jpeg'); 

// Get new dimensions 
list($width, $height) = getimagesize($filename); 
$new_width = $width * $percent; 
$new_height = $height * $percent; 

// Resample 
$image_p = imagecreatetruecolor($new_width, $new_height); 
$image = imagecreatefromjpeg($filename); 
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// Output 
imagejpeg($image_p, null, 100); 
?>