[英]how to clear image variable in javascript
我在更改每張要上傳的圖像時檢查圖像分辨率。
//preview image
$(document).on('change', '#product_image', function(event) {
var output = document.getElementById('preview_product_image');
output.src = URL.createObjectURL(event.target.files[0]);
image_check(output,event);
});
//image1
$(document).on('change', '#product_image1', function(event) {
var output = document.getElementById('preview_product_image1');
output.src = URL.createObjectURL(event.target.files[0]);
image_check(output,event);
});
//下面的分辨率檢查代碼
var loaded = false;
function loadHandler(path,width,height) {
if (loaded) {
return;
}
loaded = true;
alert("The image you have selected is low resloution image.Your image width="+width+",Heigh="+height+". Please select image greater or equal to 600x600,Thanks!");
var output = document.getElementById(path);
output.src = "http://localhost/uploads/no-photo.jpg";
}
function image_check(output,event) {
//alert("Image size");
var output = document.getElementById("preview_product_image");
output.src = URL.createObjectURL(event.target.files[0]);
var img = new Image();
img = output;
img.onload = function() {
var width = img.naturalWidth,
height = img.naturalHeight;
//window.URL.revokeObjectURL( img.src );
if( width < 600 || height < 600 ) {
// alert("The image you have selected is low resloution image.Your image width="+width+",Heigh="+height+". Please select image greater or equal to 255x255,Thanks!");
if(img.complete) {
loadHandler("preview_product_image",width,height);
}
}
};
}
問題在於,第一次加載時僅檢查其中之一,以后將無法用於其他圖像預覽框。 請幫忙 ?
這已經解決了情況,謝謝大家!
var output = document.getElementById('preview_product_image');
if(this.height < 600 || this.width < 600) {
output.src = "http://localhost/danieladenew/uploads/no-photo.jpg";
alert("The image you have selected is low resloution image.Your image width="+this.width+",Heigh="+this.height+". Please select image greater or equal to 600x600,Thanks!");
}
else{
output.src = URL.createObjectURL(event.target.files[0]);
}
return;
}
img.src = URL.createObjectURL(event.target.files[0]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.