MCPcopy Create free account
hub / github.com/docusealco/docuseal / convertImage

Function convertImage

app/javascript/elements/convert_upload.js:1–29  ·  view source on GitHub ↗
(sourceFile, targetType, quality)

Source from the content-addressed store, hash-verified

1export function convertImage (sourceFile, targetType, quality) {
2 return new Promise((resolve, reject) => {
3 const reader = new FileReader()
4
5 reader.onload = function (event) {
6 const img = new Image()
7
8 img.onload = function () {
9 const canvas = document.createElement('canvas')
10 const ctx = canvas.getContext('2d')
11
12 canvas.width = img.width
13 canvas.height = img.height
14 ctx.drawImage(img, 0, 0)
15 canvas.toBlob(function (blob) {
16 const ext = targetType === 'image/jpeg' ? '.jpg' : '.png'
17 const newFile = new File([blob], sourceFile.name.replace(/\.\w+$/, ext), { type: targetType })
18 resolve(newFile)
19 }, targetType, quality)
20 }
21
22 img.onerror = () => reject(new Error(`browser cannot decode ${sourceFile.type || sourceFile.name}`))
23
24 img.src = event.target.result
25 }
26 reader.onerror = reject
27 reader.readAsDataURL(sourceFile)
28 })
29}
30
31export async function convertImagesInInput (input) {
32 if (!input.files || input.files.length === 0) return

Callers 1

convertImagesInInputFunction · 0.85

Calls 1

replaceMethod · 0.80

Tested by

no test coverage detected