Reputation: 34
in a project I work on I need to check image dimensions (width and height) before uploading them.
I need 3 check points
1-> if dimensions are less than 600 X 600 pixels don't accept the uploading
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
3-> if dimensions are hier than 1200 X 1200 pixels, accept...
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
$("#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);
}
});`
Upvotes: 0
Views: 2866
Reputation: 4901
When you change the image, the old values
are still there because you are appending
the new text. 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.
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
. You need to change it to ||
.
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
...
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;
}
};
...
Upvotes: 1