UNPKG

filepond

Version:

FilePond, where files go to stretch their bits.

105 lines (104 loc) • 3.16 kB
/*! * FilePond v5.0.2 * Copyright (c) 2017-2026 Pqina B.V. * Released under the MIT License * https://filepond.com */ import { blobToFile as S } from "../utils/file.js"; import { getImageSize as A } from "../utils/media.js"; import { rectApply as B, rectFromSize as M } from "../utils/rect.js"; import { sizeIsEmpty as k, sizeFromRect as q } from "../utils/size.js"; import { didAbort as H } from "../utils/abort.js"; import { isFileEntry as W, isImageFile as C } from "../utils/test.js"; import { thread as L, createThreadWorker as O } from "../utils/thread.js"; import { createTransformExtension as U } from "./common/createTransformExtension.js"; import { transformImage as Q } from "../workers/transformImage.js"; const $ = U({ name: "ImageBitmapTransform", props: { actionTransform: "transformImage", width: void 0, height: void 0, upscale: !1, fit: "contain", aspectRatio: void 0, type: void 0, quality: "medium", compression: 0.98, workersURL: void 0, shouldTransform: () => !0 }, factory: ({ props: v, extensionName: z }) => ({ canTransformEntry: (o) => W(o) && C(o.file) && !/svg/i.test(o.file.type), transformEntry: async (o, { signal: c }) => { const { aspectRatio: f, width: d, height: l, upscale: F, fit: p, quality: T, compression: E, type: I, workersURL: R } = v, { file: s } = o, x = [...o.extensionState[z].history ?? []], h = await A(s); if (h === null || k(h)) throw "Failed to read image size"; const e = B(M(h, f), Math.round); let t = q(e); if (d || l) { const n = e.width / e.height, a = f || n; let i = d, r = l; if (r ? i || (i = r * a) : r = i / a, t.width = i, t.height = r, p === "contain" ? i > r ? t.width = r * a : t.height = i / a : p === "cover" && (i > r ? t.height = i / a : t.width = r * a), !F && (t.width > e.width || t.height > e.height)) { const b = Math.min( e.width / t.width, e.height / t.height ); t.width *= b, t.height *= b; } } let m; try { m = await L( O(R, Q), [ s, e, { resizeWidth: Math.round(t.width), resizeHeight: Math.round(t.height), resizeQuality: T, imageOrientation: "from-image" } ], { signal: c } ); } catch (n) { throw H(c, n) ? n : "Failed to create image bitmap"; } const g = new OffscreenCanvas(m.width, m.height), w = g.getContext("bitmaprenderer"); if (w === null) throw "Failed to create bitmap renderer"; w.transferFromImageBitmap(m); let u; try { u = await g.convertToBlob({ type: I || s.type, quality: E }); } catch { throw "Failed to convert canvas to blob"; } const y = S(u, s.name); return { file: y, history: [...x, y] }; } }) }); export { $ as ImageBitmapTransform };