如何在javascript中检查高度和宽度图像
如何在javascript中检查高度和宽度图像 我的代码是:如何在javascript中检查高度和宽度图像,javascript,jquery,image,Javascript,Jquery,Image,如何在javascript中检查高度和宽度图像 我的代码是: jQuery.validator.addMethod('allowdimensions', function (value, element, params) { if (element.files.length < 1) { // No files selected return true; } if (!element.files || !element.files[0
jQuery.validator.addMethod('allowdimensions', function (value, element, params) {
if (element.files.length < 1) {
// No files selected
return true;
}
if (!element.files || !element.files[0].size) {
// This browser doesn't support the HTML5 API
return true;
}
var minWidth = parseInt(params['minwidth'], 10);
var maxWidth = parseInt(params['maxwidth'], 10);
var minHeight = parseInt(params['minheight'], 10);
var maxHeight = parseInt(params['maxheight'], 10);
var image = {
width: 0,
height: 0
};
var reader = new FileReader();
var img = new Image();
reader.addEventListener("load", function () {
img.src = reader.result;
}, false);
reader.readAsDataURL(element.files[0]);
return image.width >= minWidth &&
image.width <= maxWidth &&
image.height >= minHeight &&
image.height <= maxHeight;
});
jQuery.validator.addMethod('allowdimensions',函数(值、元素、参数){
if(element.files.length<1){
//未选择任何文件
返回true;
}
如果(!element.files | |!element.files[0].size){
//此浏览器不支持HTML5 API
返回true;
}
var minWidth=parseInt(参数['minWidth'],10);
var maxWidth=parseInt(参数['maxWidth'],10);
var minHeight=parseInt(参数['minHeight'],10);
var maxHeight=parseInt(参数['maxHeight'],10);
变量图像={
宽度:0,
身高:0
};
var reader=new FileReader();
var img=新图像();
reader.addEventListener(“加载”,函数(){
img.src=reader.result;
},假);
reader.readAsDataURL(element.files[0]);
返回image.width>=最小宽度&&
image.width=最小高度&&
image.height像这样试试
reader.onload = function (e)
{
image.src = e.target.result;
image.onload = function ()
{
// access image size here
if (this.width < 100 && this.height < 100)
{
// alert("To Small Image or your message");
}
}
}
reader.onload=函数(e)
{
image.src=e.target.result;
image.onload=函数()
{
//在此处访问图像大小
if(this.width<100和this.height<100)
{
//提醒(“小图像或您的信息”);
}
}
}
我对您在这里要做的事情有点困惑。但请尝试一下:
jQuery:
$("document").ready(function() {
var imageWidth = $("img").width();
var imageHeight = $("img").height();
$("#dims").html("Width: " + imageWidth + " x Height: " + imageHeight)
if(imageWidth >= 400 && imageHeight >= 200) {
$("#test").html("It's equal to or wider than 400px and equal to or higher than 200px");
} else if(imageWidth <= 399 && imageHeight <= 199) {
$("#test").html("It's smaller than or equal to 399px wide and shorter than of equal to 199px");
}
;});
$(“文档”).ready(函数(){
var imageWidth=$(“img”).width();
var imageHeight=$(“img”).height();
$(“#dims”).html(“宽度:+imageWidth+”x高度:+imageHeight)
如果(图像宽度>=400和图像高度>=200){
$(“#test”).html(“它等于或大于400px,等于或大于200px”);
}else if(imageWidthPunit)的答案是正确的。但如果您使用jQuery,则可以使用:
$('#image_id').load(function(){
var Img_Width = $(this).width();
var Img_Height = $(this).height();
});
您得到的宽度和高度为0,因为图像可能尚未加载到浏览器中。您需要为img编写一个小的onload函数,要获得图像的宽度和高度,您可以相应地对其进行操作
var reader = new FileReader();
var img = new Image();
reader.addEventListener("load", function () {
preview.src = reader.result;
img.src=reader.result;
}, false);
reader.readAsDataURL(file);
img.onload = function() {
//alert(this.width + 'x' + this.height);
image.width=this.width;
image.height=this.height;
}
这里这个.width是图像的宽度,这个.height是高度。
获取图像的高度和宽度后,将其存储在图像json中,并将其与maxHeight和maxWidth、minHeight和minWidth进行比较
这是小提琴
希望这能有所帮助。实际上他正在使用var reader=new FileReader();var img=new Image();这就是为什么。
var reader = new FileReader();
var img = new Image();
reader.addEventListener("load", function () {
preview.src = reader.result;
img.src=reader.result;
}, false);
reader.readAsDataURL(file);
img.onload = function() {
//alert(this.width + 'x' + this.height);
image.width=this.width;
image.height=this.height;
}