拖拽通过jQuery删除图像以获取图像尺寸

时间:2013-03-14 09:10:00

标签: javascript jquery image native dimensions

我使用jquery-filedrop并且我允许用户仅上传文件PNG& JPG

但是如何检查图像尺寸(如像素)?

可以在文件对象中获取图像大小吗?

在我查看console.log中的文件对象后,它与图像大小无关。

enter image description here

enter image description here

或者我必须检查PHP或附加图像.width()& .height() (REF)

2 个答案:

答案 0 :(得分:1)

根据this answerGeorg 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);

答案 1 :(得分:0)

好的,我这样发帖给PHP

$size = getimagesize($_FILES['name']['tmp_name']);

    if($size[0]===150 && $size[1]===150) {
       // done
    } else {
       // error
    }