2011-11-30 208 views
0

我正在使用Uploadifyclass.upload.php上傳和操作圖像。 這裏是我的代碼。PHP圖像調整大小

require_once('/style/scripts/crop/class.upload.php'); 
    if (!empty($_FILES)) { 
     $uc = $this->input->post('username'); 
     $_REQUEST['folder'] = "/project/user/".$uc."/pages".$_REQUEST['folder']."/images/gallery"; 
     $targetPath = $_SERVER['DOCUMENT_ROOT'] .$_REQUEST['folder']. '/'; 
     $pic_temp = random_string('alnum',10); 
     $handle = new Upload($_FILES['Filedata']); 
      if ($handle->uploaded) { 
       $handle->file_src_name_body  = $pic_temp; // hard name 
       $handle->file_overwrite   = true; 
       $handle->file_auto_rename  = false; 
       $handle->image_resize   = false; 
       $handle->image_ratio_y   = false; 
       $handle->image_x     = ($handle->image_src_x < 400)?$handle->image_src_x:400; 
       $handle->file_max_size   = '999999'; // max size 
       $handle->Process($targetPath); 
       $handle->clean(); 
       if ($handle->processed) 
        $json = array("result"  => 1, 
            "file"  => $_REQUEST['folder'].'/'.$handle->file_dst_name.'?'.time(), 
            "imagewidth" => $handle->image_dst_x, 
            "imageheight" => $handle->image_dst_y 
           ); 
       else 
        $json = array("result" => 0); 

       $encoded = json_encode($json); 
       echo $encoded; 
       unset($encoded);  
      } 
      else { 
       $json = array("result" => 0); 
       $encoded = json_encode($json); 
       echo $encoded; 
       unset($encoded); 
      } 

現在我想上傳前檢查文件的大小,如果 在大小超過1MB一個圖像,然後應該重新調整大小[不裁剪或其他東西...]以節省磁盤空間。 如果小於1MB則應直接上傳。

我該如何做到這一點?

該項目正在使用Codeigniter框架。

回答

0

當一個文件被上傳後,您可以訪問它:

$_FILES['some_name']['tmp_name'] 

所以要檢查它的大小,你可以這樣做:

$size = filesize($_FILES['some_name']['tmp_name']); 

參見filesize文檔的詳細信息。

+0

使用class.upload.php完成,但只能在上傳之後完成。 – Red

0
$(document).ready(function() { 
    $('#id_image').bind('change', function() { 
     if(this.files[0].size > 1000141){ 
      $('#formerror').html('File is too big'); 
      $('#myformbutton').hide(); 
     }else{ 
      $('#formerror').html(' '); 
      $('#myformbutton').show('slow'); 
     } 
    }); 
}); 

    <div id="formerror"></div> 
    <form action="myaction" method="post" id="myform" enctype="multipart/form-data"> 
     <label for="id_title">Title</label> 
     <input id="id_title" type="text" name="title" maxlength="255" /> 
     <label for="id_image">Image</label> 
     <input type="file" name="image" id="id_image" /> 
     <input type="button" id="myformbutton" value="Add!" /> 
    </form> 
+0

我正在使用'uploadify'及其基於Flash的上傳器,我不知道如何訪問像我們的答案這樣的值,也是一個客戶端驗證。 – Red

0

Uploadify有一個參數來限制文件大小:sizeLimit。如果設置了,它將阻止提交文件。

http://www.uploadify.com/documentation/options/sizelimit/

大部分(如果不是全部)的基於Flash的上傳像SWF,solmetra等允許你檢查在上傳之前文件的大小。另外,您可以從技術上將文件自己調整到服務器上,但這會浪費您的帶寬。