2017-04-07 121 views
0

下面是我的代碼,我試過,但有證據顯示一些警告錯誤:如何在使用php上傳圖像時壓縮多個圖像大小?

警告:和getimagesize(C:\ XAMPP \ tmp目錄\ php2F0B.tmp):未能打開流:在沒有這樣的文件或目錄C:\ xampp \ htdocs \ maahima \ admin \ uploadBulkImages.php on line 55

警告:imagejpeg()需要參數1爲資源,字符串在C:\ xampp \ htdocs \ maahima \ admin \ uploadBulkImages.php中給出在線66

我想要代碼插入壓縮圖像批量文件夾中的圖像。

<?php 
if(isset($_FILES['file']['tmp_name'])){ 
    $pro_image = $_FILES['file']['tmp_name']; 
    $no_of_file = count($_FILES['file']['tmp_name']); 
    $name = ''; $type = ''; $size = ''; $error = ''; 

    for($i=0;$i < $no_of_file;$i++){ 
     if(! is_uploaded_file($_FILES['file']['tmp_name'][$i])){ 
      header("location:add_products.php?error=er8r5r"); 
     } 
     else{ 
      if(move_uploaded_file($_FILES['file']['tmp_name'][$i],"../products/".$_FILES['file']['name'][$i])){ 
       if ($_FILES["file"]["error"][$i] > 0) { 
        $error = $_FILES["file"]["error"]; 
       } 
       else { 
        $image=""; 
        $error = "Uploaded image should be jpg or gif or png"; 
        $url = '../smallSize-Image/'.$_FILES['file']['name'][$i]; 
        echo $url; 
        $source_url=$_FILES["file"]["tmp_name"][$i]; 
        $destination_url=$url; 
        $quality=12; 
        $info = getimagesize($source_url); 

        if ($info['mime'] == 'image/jpeg') 
         $image = imagecreatefromjpeg($source_url); 
        elseif ($info['mime'] == 'image/gif') 
         $image = imagecreatefromgif($source_url); 
        elseif ($info['mime'] == 'image/png') 
         $image = imagecreatefrompng($source_url); 

        imagejpeg($image, $destination_url, $quality); 
        return $destination_url; 

        $buffer = file_get_contents($url); 
        /* Force download dialog... */ 
        //header("Content-Type: application/force-download"); 
        //header("Content-Type: application/octet-stream"); 
        //header("Content-Type: application/download"); 
        // header("location:add_products.php?succcess=s8sc5s"); 
        /* Don't allow caching... */ 
        // header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
        /* Set data type, size and filename */ 
        //header("Content-Type: application/octet-stream"); 
        //header("Content-Length: " . strlen($buffer)); 
        //header("Content-Disposition: attachment; filename=$url"); 
        /* Send our file... */ 
        echo $buffer; 
       } 
      } 
      else{ 
       header("location:add_products.php?error=er8r5r"); 
      } 
     } 
    } 
} 
else{ 
} 
?> 
+0

你在HTML表單中是否有適當的表單標籤?它是否具有enctype = multipart/form-data? –

回答

1

你的腳本有很多不合邏輯的特徵。如果你要重定向,你需要exit。在腳本中間不應該有return,這有些隨意(可能這是未完成的腳本,它在另一個函數/方法內?)。如果我要這樣做,我會做一些事情。

如果您還沒有一個具有絕對定義的配置,我會創建一個並將其包含在頂部的每個初始加載頁面上。

這是測試和工作,只要它正確實施和演示。使用它的一些,它的位,或沒有它,但它的工作:

/config.php

<?php 
define('DS',DIRECTORY_SEPARATOR); 
define('ROOT_DIR',__DIR__); 
define('UPLOAD_DIR',ROOT_DIR.DS.'uploads'.DS.'images'); 
define('THUMB_DIR',ROOT_DIR.DS.'uploads'.DS.'thumbs'); 
define('VENDOR_DIR',ROOT_DIR.DS.'vendors'); 

我會考慮做一些快速的函數,在這種情況下,一類/方法系統來完成一些簡單的任務。

/vendors/Files.php

class Files 
    { 
     /* 
     ** @description This will turn a numbered $_FILES array to a normal one 
     */ 
     public static function normalize($key = 'file') 
      { 
       foreach($_FILES[$key]['tmp_name'] as $fKey => $value) { 
        $FILES[] = array(
         'tmp_name'=>$_FILES[$key]['tmp_name'][$fKey], 
         'name'=>$_FILES[$key]['name'][$fKey], 
         'error'=>$_FILES[$key]['error'][$fKey], 
         'size'=>$_FILES[$key]['size'][$fKey], 
         'type'=>$_FILES[$key]['type'][$fKey] 
        ); 
       } 

       return $FILES; 
      } 
     /* 
     ** @description I would contain this step into an editable method for 
     **    ease of use and reuse. 
     */ 
     public static function compress($path, $dest, $quality = 12) 
      { 
       $info = getimagesize($path); 

       if($info['mime'] == 'image/jpeg') 
        $image = imagecreatefromjpeg($path); 
       elseif($info['mime'] == 'image/gif') 
        $image = imagecreatefromgif($path); 
       elseif($info['mime'] == 'image/png') 
        $image = imagecreatefrompng($path); 

       imagejpeg($image,$dest,$quality); 
      } 
    } 

把它放在一起,在這種情況下,我可能會拋出異常和錯誤保存到一個會話。在add_products.php頁面上循環瀏覽它們。

# Add our config 
include(__DIR__.DIRECTORY_SEPARATOR.'config.php'); 
# Add our file handler class (spl_autoload_register() is a better option) 
include(VENDOR_DIR.DS.'Files.php'); 
# Observe for file upload 
if(!empty($_FILES['file']['tmp_name'])){ 
    # Use our handy convert method 
    $FILES  = Files::normalize(); 
    # Try here 
    try { 
     # Loop through our normalized file array 
     foreach($FILES as $i => $file){ 
      # Throw error exception here 
      if(!is_uploaded_file($file['tmp_name'])) 
       throw new Exception('File is not an uploaded document.'); 
      # I like to add a create directory line or two to cover all bases 
      if(!is_dir(UPLOAD_DIR)) { 
       if(!mkdir(UPLOAD_DIR,0755,true)) 
        throw new Exception('Upload folder could not be created.'); 
      } 
      # Ditto 
      if(!is_dir(THUMB_DIR)) { 
       if(!mkdir(THUMB_DIR,0755,true)) 
        throw new Exception('Thumb folder could not be created.'); 
      } 
      # Create an absolute path for the full size upload 
      $path = UPLOAD_DIR.DS.$file['name']; 
      # If no errors and file is moved properly, compress and save thumb 
      if(($file["error"]) == 0 && move_uploaded_file($file['tmp_name'],$path)) 
       # Create a thumbnail from/after the file you moved, not the temp file 
       Files::compress($path,THUMB_DIR.DS.$file['name']); 
      # Throw error on fail 
      else 
       throw new Exception('An unknown upload error occurred.'); 
     } 
    } 
    # Catch, assign, redirect 
    catch (Exception $e) { 
     $_SESSION['errors'][] = $e->getMessage(); 
     header("location: add_products.php?error=er8r5r"); 
     exit; 
    } 
}