图像的尺寸,然后检查标准
dimension of an image and then check the critera
我需要一个上传图像并检查最大标准的功能。如果图像大于最大标准,则显示警报消息
alert("Height is bigger then our max criteria pls select image max height and width =etc");
我可以完成所有功能,但当我上传图像时,我会给出一个条件来显示所选图像的实际高度和宽度。我会得到以前的图像高度和宽度
我的围板是:
<!DOCTYPE html>
<html>
<head>
<link href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" rel="stylesheet"></link>
<script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link href="http://jqueryui.com/resources/demos/style.css" rel="stylesheet"></link>
<script type="text/jquery">
function readImage(file) {
var reader = new FileReader();
var image = new Image();
var maxh = 800;
var maxw = 800;
reader.readAsDataURL(file);
reader.onload = function (_file) {
image.src = _file.target.result; // url.createObjectURL(file);
image.onload = function () {
var w = this.width,
h = this.height,
t = file.type, // ext only: // file.type.split('/')[1],
n = file.name,
s = ~~ (file.size / 1024) + 'KB';
if (w > maxw && h > maxh) {
alert("Height is bigger then over max criteria pls select image max height and width =2024X2024");
alert(width);
alert(height);
} else {
$('#uploadPreview').html('<img src="' + this.src + '"> ' + w + 'x' + h + ' ' + s + ' ' + t + ' ' + n + '<br>');
}
};
image.onerror = function () {
alert('Invalid file type: ' + file.type);
};
};
}
$("#choose").change(function (e) {
if (this.disabled) return alert('File upload not supported!');
var F = this.files;
if (F && F[0]) for (var i = 0; i < F.length; i++) readImage(F[i]);
});
</script>
<style>
#uploadPreview img {
height:32px;
}
</style>
</head>
<body >
<input type="file" id="choose" multiple="multiple" />
<br>
<div id="uploadPreview"></div>
</body>
</html>
问题:每次我得到的是以前选择的图像尺寸,而不是当前图像尺寸。
希望你能理解我的问题
请参阅THIS FIDDLE,重要的变化是您的逻辑错误-您正在根据之前上传的图像检查高度和宽度(等等),实际上需要执行上传来检查属性-然后确定要采取的操作。
请注意,下面的/链接的示例采用了SO上的答案,为您的目的进行了编辑。
HTML
<input type="file" id="choose" multiple="multiple" />
<br>
<div id="uploadPreview"></div>
JS-
function readImage(file) {
var reader = new FileReader();
var image = new Image();
var maxh = 800;
var maxw = 800;
reader.readAsDataURL(file);
reader.onload = function (_file) {
image.src = _file.target.result; // url.createObjectURL(file);
image.onload = function () {
var w = this.width,
h = this.height,
t = file.type, // ext only: // file.type.split('/')[1],
n = file.name,
s = ~~ (file.size / 1024) + 'KB';
if (w > maxw && h > maxh) {
alert("Height is bigger then over max criteria pls select image max height and width =2024X2024");
alert(width);
alert(height);
} else {
$('#uploadPreview').html('<img src="' + this.src + '"> ' + w + 'x' + h + ' ' + s + ' ' + t + ' ' + n + '<br>');
}
};
image.onerror = function () {
alert('Invalid file type: ' + file.type);
};
};
}
$("#choose").change(function (e) {
if (this.disabled) return alert('File upload not supported!');
var F = this.files;
if (F && F[0]) for (var i = 0; i < F.length; i++) readImage(F[i]);
});
相关文章:
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 如何利用多链接延迟检查并显示ping结果,然后使用JavaScript选择最快的URL
- 文件上传:检查文件格式,然后调用srvlet
- jQuery检查CSS宽度是否大于0,然后添加CSS属性
- j查询检查复选框是否被选中,然后向输入字段添加值
- 如何检查服务器是否返回未定义然后忽略它
- 如何检查 HTML 标记,然后在 jQuery 验证中添加错误
- 图像的尺寸,然后检查标准
- 上传图片,然后检查最大标准,然后创建缩略图
- jQuery检查URL,然后显示和隐藏元素
- 检查是否有匹配的类名,然后做些什么
- jQuery-用于检查是否有任何字段不为空,然后将所有字段设为必需字段的代码
- 如何检查文本字段为空,然后调用javascript
- 如何检查窗口是否已滚动到href,然后在javascript/jQuery中执行函数
- jQuery/javascript,然后检查其他内容
- 找到行跨度,然后检查第一个单元格是否有文本
- 设置 $(element).style.backgroundColor,然后检查但不起作用
- PHP表单提交,然后检查url以显示相应的内容
- 检查用户是否已连接到facebook,然后检查他是否喜欢某个页面
- setTimeout设置为addClass,然后检查元素是否为hasClass