【问题标题】:Drag & Drop a image via jQuery to get image dimensions通过 jQuery 拖放图像以获取图像尺寸
【发布时间】:2013-03-02 13:20:03
【问题描述】:
我使用jquery-filedrop 并且我允许用户仅上传 PNG 和 JPG 文件
但是如何检查图像大小(如像素)?
是否可以在文件对象中获取图像大小?
在我查看了 console.log 中的文件对象后,它与图像大小无关。
或者我必须检查 PHP 或附加图像所以.width() & .height() (REF)?
【问题讨论】:
标签:
javascript
jquery
image
native
dimensions
【解决方案1】:
根据this answer 的Georg Schölly:
// find the element
var img = $('#imageid');
/*
* create an offscreen image that isn't scaled
* but contains the same image.
* Because it's cached it should be instantly here.
*/
var theImage = new Image();
theImage.src = img.attr("src");
// you should check here if the image has finished loading
// this can be done with theImage.complete
alert("Width: " + theImage.width);
alert("Height: " + theImage.height);
【解决方案2】:
好的,我把这样的帖子发到 PHP 上
$size = getimagesize($_FILES['name']['tmp_name']);
if($size[0]===150 && $size[1]===150) {
// done
} else {
// error
}