简体   繁体   English

获取图像尺寸(上传之前)

[英]Get image dimensions (before uploading)

in a project I work on I need to check image dimensions (width and height) before uploading them. 在我从事的项目中,我需要在上传图像之前检查图像尺寸(宽度和高度)。

I need 3 check points 我需要3个检查站

1-> if dimensions are less than 600 X 600 pixels don't accept the uploading 1->如果尺寸小于600 X 600像素,则不接受上传

1-> if dimensions are more than 600 x 600 pixels and less then 1200 X 1200 pixels accept with the warning the the quality is not good and 1->如果尺寸大于600 x 600像素且小于1200 X 1200像素,则显示警告,表示质量不好,并且

3-> if dimensions are hier than 1200 X 1200 pixels, accept... 3->如果尺寸大于1200 X 1200像素,请接受...

I have the code you see.. but there are two issues 1 when an image is 550X700 return acceptable with warning this must be not acceptable... and the second issue when I try to change the image it keeps also the old values.. please check the code in: jsfiddle 我有您看到的代码..但是有两个问题1,当图像为550X700时返回可接受的警告,警告它必须不可接受...;第二个问题,当我尝试更改图像时,它还保留了旧值。请检查以下代码: jsfiddle

$("#file").change(function() {
    var file, img;
    if ((file = this.files[0])) {
        img = new Image();
       /* img.onload = function() {
            alert(this.width + " " + this.height);
        };
        img.onerror = function() {
            alert( "not a valid file: " + file.type);
        };
        img.src = _URL.createObjectURL(file); */
    }
    if ((file = this.files[0])) {
        img = new Image();

        img.onload = function() {
        //green
            if (this.width > 1200 && this.height > 1200){
            $('.text1').css('visibility', 'visible');
            $('.text1').append(this.width + "X & " + this.height+ "Y");
            $(".grey").addClass("green");
            $('.textgreen').css('visibility', 'visible')
            }
        //red
            else if ((this.width < 600) && (this.height < 600)){
            $('.text1').css('visibility', 'visible');
            $('.text1').append(this.width + "X & " + this.height+ "Y");
            $(".btn.btn-success.btn-xs").remove();
            $(".grey").addClass("red");
            $('.textred').css('visibility', 'visible');
            }
         //yellow
            else if (($(this.width > 600) && $(this.width <1200)) && ($(this.height > 600) && $(this.height <1200))){
            $('.text1').css('visibility', 'visible');
            $('.text1').append(this.width + "X & " + this.height+ "Y");
            $(".grey").addClass("yellow");
            $('.textyellow').css('visibility', 'visible')
            }
            else {
            return;
            }


        };
        img.src = _URL.createObjectURL(file);


    }

});`

When you change the image, the old values are still there because you are appending the new text. 更改图像时, old values仍然存在,因为您要appending新文本。 You need to replace the old one. 您需要更换旧的。 So, for that to be easy, I added an empty span and instead of appending the new text, I just replace it. 因此,为简便起见,我添加了一个空span ,而不是附加新文本,而是将其替换。

The same is happening with the background color and the "quality texts". 背景颜色和“质量文本”也是如此。 You need to remove the other classes and add the new one. 您需要删除其他类并添加新的类。

And your first problem is because you are using the && operator in the second if . 第一个问题是因为在第二个if中使用&&运算符。 You need to change it to || 您需要将其更改为|| .

HTML 的HTML

<input type="file" id="file" />
<div class="grey">
  <p class="text1">Image Dimensions : <span></span></p>
  <p class="textred">File quality is very low we can not accept this image
    <br><strong>Please select an other image</strong> </p>
  <p class="textyellow">file quality is accepteble but not high</p>
  <p class="textgreen">file quality is high</p>
  <button type="button" class="btn btn-success btn-xs" ng-click="item.upload()" ng-disabled="item.isReady || item.isUploading || item.isSuccess">
    Upload
  </button>
</div>

jQuery jQuery的

...
img.onload = function() {
    //green
    if (this.width > 1200 && this.height > 1200) {
        $('.text1').css('visibility', 'visible');
        $('.text1 span').text(this.width + "X & " + this.height + "Y");
        $(".grey").removeClass('red yellow').addClass("green");
        $('.textred, .textyellow').css('visibility', 'hidden');
        $('.textgreen').css('visibility', 'visible');
    }
    //red
    else if ((this.width < 600) || (this.height < 600)) {
        $('.text1').css('visibility', 'visible');
        $('.text1 span').text(this.width + "X & " + this.height + "Y");
        $(".btn.btn-success.btn-xs").remove();
        $(".grey").removeClass('green yellow').addClass("red");
        $('.textgreen, .textyellow').css('visibility', 'hidden');
        $('.textred').css('visibility', 'visible');
    }
    //yellow
    else if (($(this.width > 600) && $(this.width < 1200)) && ($(this.height > 600) && $(this.height < 1200))) {
        $('.text1').css('visibility', 'visible');
        $('.text1 span').text(this.width + "X & " + this.height + "Y");
        $(".grey").removeClass('green red').addClass("yellow");
        $('.textgreen, .textred').css('visibility', 'hidden');
        $('.textyellow').css('visibility', 'visible');
    } else {
        return;
    }
};
...

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM