如何在javascript中清除图像变量

时间:2016-02-09 14:39:20

标签: javascript

我有以下用户界面: enter image description here

我检查每个要上传的图像时的图像分辨率。

 //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);
                }
            }
        };   
}

问题是它只在第一次加载时检查其中一个,之后它不会验证其他图像预览框。 请帮帮忙?

1 个答案:

答案 0 :(得分:0)

这解决了这个问题,谢谢大家!!

 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]);