Skip to content
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

fix(h5): getImageInfo missing path value #11598

Merged
merged 1 commit into from
Apr 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/taro-h5/src/api/media/image/getImageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,29 @@ export const getImageInfo: typeof Taro.getImageInfo = (options) => {
return Promise.reject(res)
}

const getBase64Image = (image: HTMLImageElement) => {
try {
const canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
const ctx = canvas.getContext('2d')
ctx?.drawImage(image, 0, 0, image.width, image.height)
return canvas.toDataURL('image/png')
} catch (e) {
console.error('getImageInfo:get base64 fail', e)
}
}

const { src, success, fail, complete } = options
const handle = new MethodHandler({ name: 'getImageInfo', success, fail, complete })
return new Promise((resolve, reject) => {
const image = new Image()
image.crossOrigin = ''
image.onload = () => {
handle.success({
width: image.naturalWidth,
height: image.naturalHeight
height: image.naturalHeight,
path: getBase64Image(image) || src
}, resolve)
}
image.onerror = (e: any) => {
Expand Down