我已经创建了上传文件,但是当我想验证内容是否是图片时我有堆栈,因为如果只是验证扩展,我们可以使用虚假内容和有效扩展来操作。
在javascript中有类似getimagesize()php的功能吗?
答案 0 :(得分:2)
在一些更现代的浏览器中,您可以将图像加载到画布上下文中(实际上不必在页面上显示)并从中获取尺寸。
我在CMS中做了类似的事情我建立的CMS允许扫描某些颜色的图像,而不必先将其上传到服务器:
$('section.content form input[name=image]').change(function(){
file = this.files[0];
if(file.type.match(/image.*/))
{
var img = document.createElement('img');
// add this into an onload, as it takes time (albeit very little) to load the image into the DOM object
img.onload = function(){
canv = document.getElementById('product_image');
if(!canv || !canv.getContext)
return; // problem getting the canvas element
// get the canvas context that we'll be using and the width of the canvas & image
var ctx = canv.getContext('2d');
var width = img.width;
var height = img.height;
};
// put the file into the img object, triggering the img onload handler above
var reader = new FileReader();
reader.onload = function(e) {img.src = e.target.result};
reader.readAsDataURL(file);
}
});