Skip to content

获取图片原始尺寸 #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
chbro opened this issue Nov 8, 2018 · 7 comments
Open

获取图片原始尺寸 #39

chbro opened this issue Nov 8, 2018 · 7 comments

Comments

@chbro
Copy link

chbro commented Nov 8, 2018

let getImgRawSize = (img, cb) => {
    img = img || document.querySelector(img)
    if (!img instanceof HTMLImageElement) {
        return console.log(`error getting ${img} dom`)
    }

    if (img.naturalWidth) {
        return cb({width: img.naturalWidth, height: img.naturalHeight})
    }
    if (img.complete) {
        return cb({width: img.width, height: img.height})
    }
    let _image = new Image
    _image.src = img.src
    _image.onload = _ => {
        cb({width: _image.width, height: _image.height})
    }
}
@Dcatfly
Copy link

Dcatfly commented Nov 8, 2018

第一行代码就错了吧。。

@iamgqb
Copy link

iamgqb commented Jan 19, 2019

之前也写过一篇,不借助ImageElement的 iamgqb/I-should-know#2

@ghost
Copy link

ghost commented Jan 28, 2019

其实 onload 事件需要设置在 _image.src = img.src 语句之前

@chbro
Copy link
Author

chbro commented Feb 13, 2019

其实 onload 事件需要设置在 _image.src = img.src 语句之前

这里js执行比发起网络请求要快,所以不会影响吧。但放到前面会保险一些

@xg4
Copy link

xg4 commented Feb 28, 2019

如果 image 的width,height被设置,是不准确的。

<img src="path.png" width="100" height="100" alt="image" />

or

const image = new Image()
image.width = 100
image.height = 100
image.src = 'path.png'

应该只能依据img.naturalWidth或者new Image()来判断

export default function(image: HTMLImageElement) {
  return new Promise((resolve, reject) => {
    if (image.naturalWidth) {
      resolve({ width: image.naturalWidth, height: image.naturalHeight })
    }
    const savedImage = new Image()
    savedImage.onload = () => {
      resolve({ width: savedImage.width, height: savedImage.height })
    }
    savedImage.onerror = err => {
      reject(err)
    }
    savedImage.src = image.src
  })
}

@github-linong
Copy link

img = img || document.querySelector(img)

这句应该是不对的吧。看上去像是支持 DOM 和 选择器,但是其实并不会执行选择器部分。

img = img || document.querySelector('img')

或者是直接删掉?或者是应该判断是img是字符串,然后就去querySelector

@chbro
Copy link
Author

chbro commented Nov 26, 2020

@github-linong 是的。
工作两年半后,来更新下代码

function getImgRawSize(img: HTMLImageElement | string): Promise<{ width: number; height: number }> {
  const $img: HTMLImageElement = typeof img === 'string' ? document.querySelector(img) : img;

  return new Promise((resolve, reject) => {
    if ($img.naturalWidth) {
      resolve({ width: $img.naturalWidth, height: $img.naturalHeight });
    }
    if ($img.complete && !$img.getAttribute('width') && !$img.getAttribute('height')) {
      resolve({ width: $img.width, height: $img.height });
    }

    const image = new Image;
    image.onload = () => {
      resolve({ width: image.width, height: image.height })
    }
    image.onerror = reject;
    image.src = $img.src;
  })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants