2015-09-07 52 views
1

圖像尺寸邏輯我製成,其是用於檢查圖像大小之前提交的功能。它的工作原理沒有錯誤,但我發現如果圖像只包含一個圖像是正確的大小,允許圖像上傳的例外。發生這種情況是因爲布爾值不正確。我知道我該如何檢測這種骯髒的方式 我知道我的代碼是愚蠢的,但你們可以檢查我的代碼嗎?檢查使用jQuery

function check_scr_img_size() { 
var img_size; 
var ss = $('.tmp_screenshot').length; 
console.log("업로드할 스크린샷 갯수 : "+ ss); 

var scr = []; 
for (var i = 0 ; i < ss ; i ++) { 
    console.log("check_scr_img_size"); 
    scr[i] = document.getElementById('tmp_scr'+i); 
    console.log(scr[i].naturalWidth + " , " + scr[i].naturalHeight); 

    if(scr[i].naturalWidth == 1440 && scr[i].naturalHeight == 810) { 
     img_size = true; 
    }else { 
     img_size = false; 
    } 
} 

if (img_size) { 
    console.log("check_scr_img_size : true"); 
    return true; 
}else { 
    console.log("check_scr_img_size : false"); 
    if (lang == "ko") { 
     popup("스크린샷 이미지를 확인해 주세요. (사이즈 : 1440 X 810"); 
    }else { 
     popup("Please check your screenshots images (size : 1440 X 810"); 
    } 
    return false; 
} 
} 

我該如何解決這個問題?請讓我知道我可以修改我的代碼更加乾淨和優化的方式。


我試過了,日誌是這樣的。

스크린샷 존재 업로드할 스크린샷 존재 업로드할 스크린샷 갯수 : 2 // The 2files have to be uploaded. check_scr_img_size 518, 346 // The first image size check_scr_img_size 1440, 810 // The second image size check_scr_img_size : true // It turns to true 스크린샷 존재 && 업로드할 스크린샷 존재 && 업로드할 스크린샷 사이즈 일치

的布爾只是改變,如果最後的圖像是真實的。

我改變了這樣的代碼,你說。

function check_scr_img_size() { 
var img_size=true; 
var ss = $('.tmp_screenshot').length; 
console.log("업로드할 스크린샷 갯수 : "+ ss); 

var scr = []; 
for (var i = 0 ; i < ss ; i ++) { 
    console.log("check_scr_img_size "); 
    scr[i] = document.getElementById('tmp_scr'+i); 
    console.log(scr[i].naturalWidth + " , " + scr[i].naturalHeight); 

    if(!scr[i].naturalWidth == 1440 && !scr[i].naturalHeight == 810) { 
     //img_size = true; 
    //}else { 
     img_size = false; 
    } 
} 

if (img_size) { 
    console.log("check_scr_img_size : true"); 
    return true; 
}else { 
    console.log("check_scr_img_size : false"); 
    if (lang == "ko") { 
     popup("스크린샷 이미지를 확인해 주세요. (사이즈 : 1440 X 810"); 
    }else { 
     popup("Please check your screenshots images (size : 1440 X 810"); 
    } 
    return false; 
} 
} 

謝謝它工作正常。我改變了對(!scr[i].naturaWidth == 1440 && !scr[i].naturalHeight == 810)scr[i].naturalWidth != 1440 && scr[i].naturalHeight != 810我也img_size = false;後加入break;。我真的很感謝你的幫助:-)

回答

3

只是刪除設置img_sizetrue。相反img_sizetrue INIT:

var img_size = true; 
// in your loop: 
// ... 
if(scr[i].naturalWidth != 1440 && scr[i].naturalHeight != 810) { 
    img_size = false; 
} 

所以,你總是會得到false如果收到了一些不好的形象。

+0

我申請你的代碼,並在錯誤的方式做工作。你能再次檢查我的問題嗎?我更新了我的問題。謝謝 –

+1

@홍의숙哇'實際上(!SCR [I] .naturaWidth == 1440 &&!SCR [I] .naturalHeight == 810)'總會返回false,這意味着如果'(假== 1440 &&假== 810 )'我們都錯過了,我不知道爲什麼u_mulder使這個......應該是'如果(SCR [I] .naturalWidth!== 1440 || SCR [I] .naturalHeight!== 810)' – Kaiido

+0

是的,我的錯誤。更新。謝謝@Kaiido –

1

你可以試試這個:

function check_scr_img_size() { 
    var img_size = true, 
     ss = $('.tmp_screenshot').length, 
     scr = []; 
    console.log("업로드할 스크린샷 갯수 : " + ss); 
    for (var i = 0; i < ss; i++) { 
     if (img_size === true) { 
      console.log("check_scr_img_size"); 
      scr[i] = document.getElementById('tmp_scr' + i); 
      console.log(scr[i].naturalWidth + " , " + scr[i].naturalHeight); 
      if (scr[i].naturalWidth != 1440 || scr[i].naturalHeight != 810) { 
       img_size = false; 
      } 
     } 
    } 
    if (img_size) { 
     console.log("check_scr_img_size : true"); 
     return true; 
    } else { 
     console.log("check_scr_img_size : false"); 
     if (lang == "ko") { 
      popup("스크린샷 이미지를 확인해 주세요. (사이즈 : 1440 X 810"); 
     } else { 
      popup("Please check your screenshots images (size : 1440 X 810"); 
     } 
     return false; 
    } 
} 
+1

這確實是一個好方法,但是不要檢查for循環中的'img_size'是否爲true,如果它是false,則可以'break';而不是。 – Kaiido

+0

這是真的。我認爲一次迭代和一次分配會更快。 –

+0

@홍의숙這是另一種做法,類似於u_mulder。 –