2010-09-07 28 views
1

我正在使用Uploadify上傳圖像。我唯一遇到的問題是我想限制圖片尺寸給用戶上傳圖片。 (我不想使用圖像調整大小出於某種原因)。我在php中這樣做的邏輯是我有一個用戶定義的函數,它將首先獲取圖像tempname的三個參數,第二個要限制的圖像的寬度,第三個圖像的高度限制用戶。幫我理解使用Uploadify上傳文件的邏輯

我的功能是這樣的。

function valid_image($image, $width, $height = 0) { 
      /** Get the Dimensions of the Uploaded Image **/ 
      list($image_width, $image_height) = getimagesize($image['tmp_name']); 
      /** If the height parameter in function is skipped then perform this **/ 
     if($height == '0') { 
     if($image_width !== $width) { 
      return false; 
      } 
      } 
      /** If all the parameters in the function has got the value then perform this **/ 
    else if($image_width !== $width || $image_height !== $height) { 
      return false; 
      } 
      return true; 
      } 

我想對uploadify應用相同的規則。

這裏是我的jQuery的Uploadify的腳本。

//Uploadify File1 
    $(document).ready(function() { 
    $('#nstitle').uploadify({ 
     'uploader': 'images/uploadify.swf', 
     'script': 'uploadify.php', 
     'folder': 'upload', 
     'auto' : 'true', 
     'cancelImg': 'images/cancel.png', 
     'fileDesc': 'jpg/jpeg', 
     'displayData': 'percentage', 
     'fileExt': "*.jpg;*.jpeg", 
     'sizeLimit' : '8388608', 
     'fileDataName' : 'nstitle', 
     onComplete: function(event, queueID, fileObj, response, data) 
     { 
    $('#t_fileUploadName').append('<p>'+response+'<p/>'); 
    $("#t_fileUploadField").append("<input type='hidden' name='nstitle' value='" + response + "' />"); 
    $("#t_fileUploadBtn").remove(); 
     } 
     }); }); 

現在我想uploadify生成錯誤,如果它不符合條件,在uploadify.php我做了這樣的事情。

if (!empty($_FILES['nstitle'])) { 
    $tempFile = $_FILES['nstitle']['tmp_name']; 
    if(!valid_image($_FILES['nstitle'],685 , 50)) { 
    echo "Incorrect File Dimension"; 
    die(); 
    } 
    else { 
    $targetPath = 'uploads/news/title/'; 
    move_uploaded_file($tempFile,$targetPath.$ns_title); 
    echo "$targetPath$ns_title"; 
    } 
} 

請注意,我已經包括所有相關的文件與各自的網頁require_once();如果我刪除該valid_image()並運行沒有它的腳本它只是工作得很好,但如果我試圖把條件觸發錯誤,它不起作用,因爲我試圖上傳與確切尺寸的文件。仍然拒絕進入其他條件。

我在這裏空白,這是做錯的方式。如果是,那麼我如何限制用戶到固定的圖像尺寸。高度讚賞任何類型的幫助,請讓我知道使用與觸發錯誤相關的Uplodify所帶來的體驗和體驗。

在此先感謝..

+0

「uploadify」 作爲'www.uploadify.com'? – VolkerK 2010-09-07 07:35:33

+0

@VolkerK是的。 – 2010-09-07 07:45:34

回答

2

變化

if(!valid_image($_FILES['nstitle'],685 , 50)) { 

if(!valid_image($tempFile,685 , 50)) { 

看來你已經工作了太長時間,你沒有抓住這個微小的失誤:)好像我是一個工作太久的人..


編輯:

在你valid_image函數的頂部添加:

var_dump($image, getimagesize($image['tmp_name'])); 

那有什麼輸出?

+0

...並且適用於任何您是否使用uploadify或不。 – VolkerK 2010-09-07 07:37:29

+0

不能:(檢查出我的用戶定義的valid_image函數我已經問過函數綁定參數與['tmp_name']。這是代碼。$ image ['tmp_name'];我甚至嘗試過你的提示,它不起作用。我的問題是uploadify.php能夠這樣嗎? – 2010-09-07 07:44:50

+0

是的,從php的角度來看,這是一個完全正常的文件上傳。 – VolkerK 2010-09-07 07:52:52

0

你能不能改!==來!=在valid_image功能,然後嘗試