【发布时间】:2016-01-19 19:01:40
【问题描述】:
在我从事的项目中,我需要在上传图片之前检查图片尺寸(宽度和高度)。
我需要3个检查点
1-> 如果尺寸小于 600 X 600 像素,则不接受上传
1-> 如果尺寸大于 600 x 600 像素且小于 1200 x 1200 像素,则接受警告质量不好和
3-> 如果尺寸大于 1200 X 1200 像素,则接受...
我有你看到的代码.. 但是有两个问题 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);
}
});`
【问题讨论】:
-
我试图纠正您的拼写问题,但有些我无法理解。请仔细阅读文本,看看是否可以修复它,最好是
-
@timoleon 请参考关于 SO 的类似问题。你会找到你的答案。以stackoverflow.com/questions/5633264/… 为例
-
至于你的第一个问题,第二个
else if正在检查宽度和高度是否小于600。将&&更改为||,所以如果宽度或高度是低于600,不予录取。 -
@azeós 谢谢你是对的,它运作良好......
标签: javascript jquery image