summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/density-size-correction/resources/exify.js
blob: b6c22e196c64a8532d05c1ea5e1453c5534c753b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function createImageWithMetadata({
    width,
    height,
    preferredWidth,
    preferredHeight,
    resolutionX,
    resolutionY,
    resolutionUnit,
    orientation
}) {
    const canvas = document.createElement('canvas')
    canvas.width = width || 100
    canvas.height = height || 100
    const ctx = canvas.getContext('2d')
    ctx.fillColor = 'green'
    ctx.fillRect(0, 0, canvas.width, canvas.height)
    const original = canvas.toDataURL('image/jpeg')
    const root = {}
    const exif = {}
    if (orientation !== undefined)
        root[piexif.ExifIFD.Orientation] = orientation
    if (resolutionX !== undefined)
        root[piexif.ImageIFD.XResolution] = Array.isArray(resolutionX) ? resolutionX : [resolutionX, 1]
    if (resolutionY !== undefined)
        root[piexif.ImageIFD.YResolution] = Array.isArray(resolutionY) ? resolutionY : [resolutionY, 1]
    if (resolutionUnit !== undefined)
        root[piexif.ImageIFD.ResolutionUnit] = resolutionUnit
    if (preferredWidth !== undefined)
        exif[piexif.ExifIFD.PixelXDimension] = preferredWidth
    if (preferredHeight !== undefined)
        exif[piexif.ExifIFD.PixelYDimension] = preferredHeight
    const exifString = piexif.dump({'0th': root, 'Exif': exif})
    const newDataUrl = piexif.insert(exifString, original)
    const image = new Image()
    image.src = newDataUrl
    return new Promise(resolve => {
        image.onload = () => resolve(image);
    })
}