温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javascript - Image.width gives 0

javascript - Image.width给出0

发布于 2020-03-27 15:44:56

我正在使用一个函数来验证上传图像的大小。效果很好,现在我还想检查图像是否比某些像素(例如1280像素)宽。但是var宽度仍为零。我已经尝试使用externalwith,innerwidth和file.files [0] .width;。

function ValidateSize(file) {
    var FileSize = file.files[0].size / 1024 / 1024; // in MB
    var NewFilze = FileSize.toFixed(2);

    var height = file.height;
    var width = file.width;

    if (FileSize > 13) {
        alert("The file size of your picture (" + NewFilze + " MB) exceeds the maximum allowed filesize (12,5 MB). Still problems with resizing your pictures? Use for example www.imageresize.org.");
    }else if(width < 1280){
        alert("The width of your image  (" + width + " pixels) is lower than the required minimum width (1280 pixels)");
    }else{

    }
}

提前致谢

查看更多

查看更多

提问者
Glenn S
被浏览
8
FujiRoyale 2020-01-31 16:45

如果无法自己进行测试,建议您尝试以下操作:

function ValidateSize(file) {
  var FileSize = file.files[0].size / 1024 / 1024; // in MB
  var NewFilze = FileSize.toFixed(2);
  var reader = new FileReader();

  reader.readAsDataURL(fileUpload.files[0]);
  reader.onload = function (e) {
    var image = new Image();
    image.src = e.target.result;
    image.onload = function () {
      var height = this.height;
      var width = this.width;

    if (FileSize > 13) {
       alert("The file size of your picture (" + NewFilze + " MB) exceeds the maximum allowed filesize (12,5 MB). Still problems with resizing your pictures? Use for example www.imageresize.org.");
    }else if(width < 1280){
       alert("The width of your image  (" + width + " pixels) is lower than the required minimum width (1280 pixels)");
    }else{

    }
} 

我应该注意,这未经测试,并且取决于您的js在某些浏览器(最好是chrome或FF)上运行。如果是服务器端,我认为visibleman被链接为最佳答案。

祝你好运,黑客愉快!

归功于我应该获得图像代码的适当位置

发布
问题

分享
好友

手机
浏览

扫码手机浏览