简体   繁体   English

如何在上传前检查图像宽度和高度

[英]How to check image width and height before upload

for picture upload i wrote following html code : 对于图片上传我写了以下HTML代码:

<input type="file" id="upload" class="im" onchange="fileSelectHandlerProfile('defaultProfile','true')" style="cursor:pointer"/>

when we done with select image, 当我们完成选择图像时,

this code will be fired : 此代码将被触发:

function fileSelectHandlerProfile(title, defalutProfile) {
    var oFile;
    oFile = $('#thumbUploadProfile')[0].files[0];
    var rFilter = /^(image\/jpeg|image\/png|image\/gif|image\/bmp|image\/jpg|image\/pjpeg)$/i;
    if (!rFilter.test(oFile.type)) {
        $('.errorProfile').html('Please select a valid image file (jpg and png are allowed)').show();
    }
}

i want to check the oFile width and height, how can it is possible like oFile.type? 我想检查oFile的宽度和高度,怎么可能像oFile.type一样?

A solutions is to load it clienside and check the height and width. 解决方案是将其装入clienside并检查高度和宽度。

function fileSelectHandlerProfile(title, defalutProfile) {
var oFile;
oFile = $('#thumbUploadProfile')[0].files[0];
var rFilter = /^(image\/jpeg|image\/png|image\/gif|image\/bmp|image\/jpg|image\/pjpeg)$/i;
if (!rFilter.test(oFile.type)) {
    $('.errorProfile').html('Please select a valid image file (jpg and png are allowed)').show();
}

var picReader = new FileReader();

picReader.addEventListener("load", function (event) {

    var imageObj = new Image();
    imageObj.src = event.target.result;
    imageObj.onload = function () {
        //TEST IMAGE SIZE
        if (imageObj.height < 100 || imageObj.width < 100) {
            $('.errorProfile').html('Please select an image with correct dimensions').show();
        }
    };
});

//Read the image
picReader.readAsDataURL(oFile);

} }

You can do it in 2 steps: 您可以分两步完成:

  1. Read the image file content as data url using html5 file api: 使用html5文件api将图像文件内容作为数据URL读取:
var reader  = new FileReader();
reader.onloadend = function () {
    //var dataUrl = reader.result;
    //code as per step 2 here...
}
reader.readAsDataURL(oFile);

//ref: https://developer.mozilla.org/en-US/docs/Web/API/FileReader.readAsDataURL
  1. Now read the image size by creating Image object with the dataUrl as src. 现在通过使用dataUrl作为src创建Image对象来读取图像大小。

    Details: JS - get image width and height from the base64 code 详细信息: JS-从base64代码获取图像的宽度和高度

The FileReader API allows you to use FileReader.readAsDataURL to read the file as a data: url. FileReader API允许您使用FileReader.readAsDataURL将文件作为data: url读取。 Use that url as the src attribute of a <img /> tag and read the width and height attribute of that image. 使用该URL作为<img />标记的src属性,并读取该图像的widthheight属性。

    var width;
    var height;
    var oFile;
    oFile = $('#thumbUploadProfile')[0].files[0];

    var reader = new FileReader();
    reader.onload = function(e){
        var image = new Image();
        image.onload  = function(){            
            width = img.width;
            height = img.height;
        }
        image.src   = e.target.result;
    };
    reader.readAsDataURL(oFile);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在uploadify中上传图片之前如何检查图片的宽度和高度? - How to check image width and height before upload image in uploadify? 在使用 Javascript 上传之前检查图像的宽度和高度 - Check image width and height before upload with Javascript Reactjs上传前如何检查图片高宽 - How to check image height and width before upload in Reactjs blueimp fileupload-上传之前检查图像的宽度和高度 - blueimp fileupload - check the image width and height before upload 上传前如何设置图像的最大宽度和最大高度 - How To Set Max Width And Max Height Of Image Before Upload 如何在使用[Package CollectionFS]插入图像之前检查宽度和高度 - How to check width and height before insert image by using [Package CollectionFS] 在节点上传之前验证图像宽度和高度,使用 Multer 表达 - Validate Image width & height before upload in Node, Express With Multer 上传前获取文件大小,图像宽度和高度 - Get file size, image width and height before upload 在上传之前从Phonegap获取图像宽度和高度 - Getting an image width and height from Phonegap before upload 上传前如何获取文件大小,图像,高度和宽度? 在剔除文件绑定中 - How to get file size, image ,height and width before upload? in knockout file binding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM