vue3-image-compressor 1.0.4 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
- import { ref as b, onUnmounted as Q, computed as q, defineComponent as H, watch as K, openBlock as _, createElementBlock as k, createElementVNode as g, withModifiers as N, renderSlot as S, toDisplayString as T, unref as m, createCommentVNode as z, withDirectives as X, isRef as Y, Fragment as Z, renderList as ee, vModelSelect as te, createTextVNode as ne } from "vue";
2
- const L = {
1
+ import { ref as y, onUnmounted as Y, computed as R, defineComponent as Z, openBlock as z, createElementBlock as k, createElementVNode as E, withModifiers as J, renderSlot as S, toDisplayString as D, unref as v, createCommentVNode as I, withDirectives as ee, isRef as te, Fragment as oe, renderList as re, vModelSelect as ne, createTextVNode as se } from "vue";
2
+ const G = {
3
3
  mozJPEG: {
4
4
  label: "MozJPEG",
5
5
  mimeType: "image/jpeg",
@@ -50,7 +50,7 @@ const L = {
50
50
  mimeType: "image/webp2",
51
51
  extension: "wp2"
52
52
  }
53
- }, W = {
53
+ }, N = {
54
54
  mozJPEG: {
55
55
  quality: 75,
56
56
  baseline: !1,
@@ -124,537 +124,663 @@ const L = {
124
124
  wp2: {
125
125
  quality: 75
126
126
  }
127
- }, re = Object.keys(L);
128
- async function oe(t) {
129
- const e = await createImageBitmap(t), r = document.createElement("canvas");
130
- r.width = e.width, r.height = e.height;
131
- const n = r.getContext("2d");
132
- return n.drawImage(e, 0, 0), n.getImageData(0, 0, r.width, r.height);
127
+ }, ae = Object.keys(G);
128
+ async function Q(r) {
129
+ const o = await createImageBitmap(r), n = document.createElement("canvas");
130
+ n.width = o.width, n.height = o.height;
131
+ const t = n.getContext("2d");
132
+ return t.drawImage(o, 0, 0), t.getImageData(0, 0, n.width, n.height);
133
133
  }
134
- async function J(t, e, r) {
135
- const n = document.createElement("canvas");
136
- return n.width = t.width, n.height = t.height, n.getContext("2d").putImageData(t, 0, 0), new Promise((p) => {
137
- n.toBlob(
138
- (c) => p(c),
139
- e,
140
- r
134
+ async function B(r, o, n) {
135
+ const t = document.createElement("canvas");
136
+ return t.width = r.width, t.height = r.height, t.getContext("2d").putImageData(r, 0, 0), new Promise((s) => {
137
+ t.toBlob(
138
+ (a) => s(a),
139
+ o,
140
+ n
141
141
  );
142
142
  });
143
143
  }
144
- function Ge(t) {
145
- return new Promise((e, r) => {
146
- const n = new Image();
147
- n.onload = () => {
148
- e({ width: n.width, height: n.height }), URL.revokeObjectURL(n.src);
149
- }, n.onerror = r, n.src = URL.createObjectURL(t);
144
+ function Le(r) {
145
+ return new Promise((o, n) => {
146
+ const t = new Image();
147
+ t.onload = () => {
148
+ o({ width: t.width, height: t.height }), URL.revokeObjectURL(t.src);
149
+ }, t.onerror = n, t.src = URL.createObjectURL(r);
150
150
  });
151
151
  }
152
- async function se(t) {
153
- const e = new Uint8Array(await t.slice(0, 4).arrayBuffer());
154
- return e[0] === 137 && e[1] === 80 ? "image/png" : e[0] === 255 && e[1] === 216 ? "image/jpeg" : e[0] === 71 && e[1] === 73 ? "image/gif" : e[0] === 82 && e[1] === 73 ? "image/webp" : e[0] === 0 && e[1] === 0 ? "image/avif" : t.type || "image/jpeg";
152
+ async function ie(r) {
153
+ const o = new Uint8Array(await r.slice(0, 4).arrayBuffer());
154
+ return o[0] === 137 && o[1] === 80 ? "image/png" : o[0] === 255 && o[1] === 216 ? "image/jpeg" : o[0] === 71 && o[1] === 73 ? "image/gif" : o[0] === 82 && o[1] === 73 ? "image/webp" : o[0] === 0 && o[1] === 0 ? "image/avif" : r.type || "image/jpeg";
155
155
  }
156
- function ae(t) {
157
- return new Promise((e) => {
158
- const r = new Image();
159
- r.onload = () => e(!0), r.onerror = () => e(!1), r.src = `data:${t};base64,`;
156
+ function le(r) {
157
+ return new Promise((o) => {
158
+ const n = new Image();
159
+ n.onload = () => o(!0), n.onerror = () => o(!1), n.src = `data:${r};base64,`;
160
160
  });
161
161
  }
162
- function ie(t, e, r) {
163
- return new File([t], e, { type: r });
162
+ function H(r, o, n) {
163
+ return new File([r], o, { type: n });
164
164
  }
165
- function $(t, e = 2) {
166
- if (t === 0) return "0 Bytes";
167
- const r = 1024, n = e < 0 ? 0 : e, u = ["Bytes", "KB", "MB", "GB"], p = Math.floor(Math.log(t) / Math.log(r));
168
- return parseFloat((t / Math.pow(r, p)).toFixed(n)) + " " + u[p];
165
+ function $(r, o = 2) {
166
+ if (r === 0) return "0 Bytes";
167
+ const n = 1024, t = o < 0 ? 0 : o, e = ["Bytes", "KB", "MB", "GB"], s = Math.floor(Math.log(r) / Math.log(n));
168
+ return parseFloat((r / Math.pow(n, s)).toFixed(t)) + " " + e[s];
169
169
  }
170
- function le(t, e) {
171
- return t === 0 ? 0 : Math.round((t - e) / t * 100);
170
+ function K(r, o) {
171
+ if (r === 0) return 0;
172
+ const n = Math.round((r - o) / r * 100);
173
+ return Math.max(0, n);
172
174
  }
173
- function ce(t, e) {
174
- return `${t.replace(/\.[^.]+$/, "")}_compressed.${e}`;
175
+ function X(r, o) {
176
+ return `${r.replace(/\.[^.]+$/, "")}_compressed.${o}`;
175
177
  }
176
- function V(t) {
177
- return URL.createObjectURL(t);
178
+ function V(r) {
179
+ return URL.createObjectURL(r);
178
180
  }
179
- function ue(t) {
180
- URL.revokeObjectURL(t);
181
+ function ce(r) {
182
+ URL.revokeObjectURL(r);
181
183
  }
182
- const de = 1e4;
183
- function me() {
184
- const t = b(!1), e = b(!1), r = b(null);
185
- let n = null, u = null;
186
- function p() {
187
- if (!n)
184
+ const ue = 1e4;
185
+ function de(r) {
186
+ let o = 0;
187
+ const n = /* @__PURE__ */ new Map();
188
+ r.addEventListener("message", (e) => {
189
+ const { id: s, result: a, error: p } = e.data, u = n.get(s);
190
+ u && (n.delete(s), p ? u.reject(new Error(p)) : u.resolve(a));
191
+ }), r.addEventListener("error", (e) => {
192
+ console.error("Worker error:", {
193
+ message: e.message,
194
+ filename: e.filename,
195
+ lineno: e.lineno,
196
+ colno: e.colno,
197
+ error: e.error
198
+ }), n.forEach((s) => s.reject(e)), n.clear();
199
+ });
200
+ function t(e, ...s) {
201
+ return new Promise((a, p) => {
202
+ const u = ++o;
203
+ n.set(u, { resolve: a, reject: p }), r.postMessage({ id: u, method: e, args: s });
204
+ });
205
+ }
206
+ return {
207
+ avifDecode: (e) => t("avifDecode", e),
208
+ jxlDecode: (e) => t("jxlDecode", e),
209
+ qoiDecode: (e) => t("qoiDecode", e),
210
+ webpDecode: (e) => t("webpDecode", e),
211
+ wp2Decode: (e) => t("wp2Decode", e),
212
+ avifEncode: (e, s) => t("avifEncode", e, s),
213
+ jxlEncode: (e, s) => t("jxlEncode", e, s),
214
+ mozjpegEncode: (e, s) => t("mozjpegEncode", e, s),
215
+ oxipngEncode: (e, s) => t("oxipngEncode", e, s),
216
+ qoiEncode: (e, s) => t("qoiEncode", e, s),
217
+ webpEncode: (e, s) => t("webpEncode", e, s),
218
+ wp2Encode: (e, s) => t("wp2Encode", e, s),
219
+ rotate: (e, s) => t("rotate", e, s),
220
+ quantize: (e, s) => t("quantize", e, s),
221
+ resize: (e, s) => t("resize", e, s)
222
+ };
223
+ }
224
+ function pe() {
225
+ const r = y(!1), o = y(!1), n = y(null);
226
+ let t = null, e = null;
227
+ function s() {
228
+ if (!t)
188
229
  try {
189
- e.value = !0, n = new Worker(
230
+ o.value = !0, t = new Worker(
190
231
  new URL(
191
232
  /* @vite-ignore */
192
- "/assets/imageWorker-DyeUTFOy.js",
233
+ "/assets/imageWorker-Qv3pCULy.js",
193
234
  import.meta.url
194
235
  ),
195
236
  { type: "module" }
196
- ), t.value = !0, r.value = null;
197
- } catch (x) {
198
- r.value = x instanceof Error ? x : new Error("Failed to start worker"), console.error("Worker start failed:", x);
237
+ ), r.value = !0, n.value = null;
238
+ } catch (b) {
239
+ n.value = b instanceof Error ? b : new Error("Failed to start worker"), console.error("Worker start failed:", b);
199
240
  } finally {
200
- e.value = !1;
241
+ o.value = !1;
201
242
  }
202
243
  }
203
- function c() {
204
- u && (clearTimeout(u), u = null), n && (n.terminate(), n = null, t.value = !1);
244
+ function a() {
245
+ e && (clearTimeout(e), e = null), t && (t.terminate(), t = null, r.value = !1);
205
246
  }
206
- function h() {
207
- u && clearTimeout(u), u = window.setTimeout(() => {
208
- c();
209
- }, de);
247
+ function p() {
248
+ e && clearTimeout(e), e = window.setTimeout(() => {
249
+ a();
250
+ }, ue);
210
251
  }
211
- async function w() {
212
- if (n || p(), !n)
252
+ async function u() {
253
+ if (t || s(), !t)
213
254
  throw new Error("Worker failed to initialize");
214
- return n;
255
+ return de(t);
215
256
  }
216
- async function P(x, C) {
217
- if (x.aborted)
257
+ async function h(b, O) {
258
+ if (b.aborted)
218
259
  throw new DOMException("AbortError", "AbortError");
219
- return clearTimeout(u), new Promise((G, R) => {
220
- const O = () => {
221
- c(), R(new DOMException("AbortError", "AbortError"));
260
+ return clearTimeout(e), new Promise((q, P) => {
261
+ const T = () => {
262
+ a(), P(new DOMException("AbortError", "AbortError"));
222
263
  };
223
- x.addEventListener("abort", O), C().then(G).catch(R).finally(() => {
224
- x.removeEventListener("abort", O), h();
264
+ b.addEventListener("abort", T), O().then(q).catch(P).finally(() => {
265
+ b.removeEventListener("abort", T), p();
225
266
  });
226
267
  });
227
268
  }
228
- return Q(() => {
229
- c();
269
+ return Y(() => {
270
+ a();
230
271
  }), {
231
- isReady: t,
232
- isLoading: e,
233
- error: r,
234
- worker: n,
235
- getWorkerApi: w,
236
- executeTask: P,
237
- terminateWorker: c
272
+ isReady: r,
273
+ isLoading: o,
274
+ error: n,
275
+ worker: t,
276
+ getWorkerApi: u,
277
+ executeTask: h,
278
+ terminateWorker: a
238
279
  };
239
280
  }
240
- function pe() {
241
- const t = b(!1), e = b(0), r = b(null), n = b(null), u = b(null), { getWorkerApi: p, executeTask: c } = me(), h = q(() => t.value && !!u.value);
242
- async function w(d, o) {
243
- const a = await se(o), i = await ae(a), f = await p();
244
- if (!i) {
245
- if (a === "image/avif")
246
- return c(d, () => f.avifDecode(o));
247
- if (a === "image/webp")
248
- return c(d, () => f.webpDecode(o));
249
- if (a === "image/jxl")
250
- return c(d, () => f.jxlDecode(o));
251
- if (a === "image/webp2")
252
- return c(d, () => f.wp2Decode(o));
253
- if (a === "image/qoi")
254
- return c(d, () => f.qoiDecode(o));
281
+ function me() {
282
+ const r = y(!1), o = y(0), n = y(null), t = y(null), e = y(null), { getWorkerApi: s, executeTask: a } = pe(), p = R(() => r.value && !!e.value);
283
+ async function u(f, i) {
284
+ const l = await ie(i), d = await le(l), w = await s();
285
+ if (!d) {
286
+ if (l === "image/avif")
287
+ return a(f, () => w.avifDecode(i));
288
+ if (l === "image/webp")
289
+ return a(f, () => w.webpDecode(i));
290
+ if (l === "image/jxl")
291
+ return a(f, () => w.jxlDecode(i));
292
+ if (l === "image/webp2")
293
+ return a(f, () => w.wp2Decode(i));
294
+ if (l === "image/qoi")
295
+ return a(f, () => w.qoiDecode(i));
255
296
  }
256
- return oe(o);
297
+ return Q(i);
257
298
  }
258
- async function P(d, o, a) {
259
- if (a === 0) return o;
260
- const i = await p();
261
- return c(
262
- d,
263
- () => i.rotate(o, { rotate: a })
299
+ async function h(f, i, l) {
300
+ if (l === 0) return i;
301
+ const d = await s();
302
+ return a(
303
+ f,
304
+ () => d.rotate(i, { rotate: l })
264
305
  );
265
306
  }
266
- async function x(d, o, a) {
267
- var v, y;
268
- let i = o;
269
- const f = await p();
270
- return (v = a.resize) != null && v.enabled && (i = await c(
271
- d,
272
- () => f.resize(i, {
273
- width: a.resize.width || i.width,
274
- height: a.resize.height || i.height,
275
- method: a.resize.method || "lanczos3",
276
- fitMethod: a.resize.fitMethod || "stretch",
307
+ async function b(f, i, l) {
308
+ var g, x;
309
+ let d = i;
310
+ const w = await s();
311
+ return (g = l.resize) != null && g.enabled && (d = await a(
312
+ f,
313
+ () => w.resize(d, {
314
+ width: l.resize.width || d.width,
315
+ height: l.resize.height || d.height,
316
+ method: l.resize.method || "lanczos3",
317
+ fitMethod: l.resize.fitMethod || "stretch",
277
318
  premultiply: !0,
278
319
  linearRGB: !0
279
320
  })
280
- )), (y = a.quantize) != null && y.enabled && (i = await c(
281
- d,
282
- () => f.quantize(i, {
283
- numColors: a.quantize.numColors || 256,
284
- dither: a.quantize.dither || 1
321
+ )), (x = l.quantize) != null && x.enabled && (d = await a(
322
+ f,
323
+ () => w.quantize(d, {
324
+ numColors: l.quantize.numColors || 256,
325
+ dither: l.quantize.dither || 1
285
326
  })
286
- )), i;
327
+ )), d;
287
328
  }
288
- async function C(d, o, a) {
289
- const i = await p(), f = a.encoder;
290
- if (f === "browserJPEG")
291
- return (await J(
292
- o,
329
+ async function O(f, i, l) {
330
+ const d = await s(), w = l.encoder;
331
+ if (w === "browserJPEG")
332
+ return (await B(
333
+ i,
293
334
  "image/jpeg",
294
- a.encoderOptions.quality
335
+ l.encoderOptions.quality
295
336
  )).arrayBuffer();
296
- if (f === "browserPNG")
297
- return (await J(o, "image/png")).arrayBuffer();
298
- if (f === "browserGIF")
337
+ if (w === "browserPNG")
338
+ return (await B(i, "image/png")).arrayBuffer();
339
+ if (w === "browserGIF")
299
340
  throw new Error("Browser GIF encoding not supported directly");
300
- const y = {
301
- mozJPEG: i.mozjpegEncode,
302
- webP: i.webpEncode,
303
- avif: i.avifEncode,
304
- jxl: i.jxlEncode,
305
- oxiPNG: i.oxipngEncode,
306
- qoi: i.qoiEncode,
307
- wp2: i.wp2Encode
308
- }[f];
309
- if (!y)
310
- throw new Error(`Unknown encoder: ${f}`);
311
- return (await c(
312
- d,
313
- () => y.call(i, o, a.encoderOptions)
341
+ const x = {
342
+ mozJPEG: d.mozjpegEncode,
343
+ webP: d.webpEncode,
344
+ avif: d.avifEncode,
345
+ jxl: d.jxlEncode,
346
+ oxiPNG: d.oxipngEncode,
347
+ qoi: d.qoiEncode,
348
+ wp2: d.wp2Encode
349
+ }[w];
350
+ if (!x)
351
+ throw new Error(`Unknown encoder: ${w}`);
352
+ return (await a(
353
+ f,
354
+ () => x.call(d, i, l.encoderOptions)
314
355
  )).buffer;
315
356
  }
316
- async function G(d, o) {
317
- var f;
318
- u.value = new AbortController();
319
- const a = u.value.signal;
320
- t.value = !0, e.value = 0, r.value = null;
321
- const i = n.value;
322
- n.value = null, (f = i == null ? void 0 : i.compressed) != null && f.blobUrl && ue(i.compressed.blobUrl);
357
+ async function q(f, i) {
358
+ var w;
359
+ e.value = new AbortController();
360
+ const l = e.value.signal;
361
+ r.value = !0, o.value = 0, n.value = null;
362
+ const d = t.value;
363
+ t.value = null, (w = d == null ? void 0 : d.compressed) != null && w.blobUrl && ce(d.compressed.blobUrl);
323
364
  try {
324
- e.value = 10;
325
- const v = {
326
- file: d,
327
- size: d.size,
365
+ o.value = 10;
366
+ const g = {
367
+ file: f,
368
+ size: f.size,
328
369
  width: 0,
329
370
  height: 0,
330
- blobUrl: V(d)
331
- }, y = await w(a, d);
332
- v.width = y.width, v.height = y.height, e.value = 25;
333
- const j = await P(
334
- a,
335
- y,
336
- o.rotate || 0
371
+ blobUrl: V(f)
372
+ }, x = await u(l, f);
373
+ g.width = x.width, g.height = x.height, o.value = 25;
374
+ const U = await h(
375
+ l,
376
+ x,
377
+ i.rotate || 0
337
378
  );
338
- e.value = 35;
339
- const U = await x(a, j, o);
340
- e.value = 50;
341
- const F = await C(a, U, o);
342
- e.value = 80;
343
- const I = L[o.encoder], M = ce(
344
- d.name,
345
- (I == null ? void 0 : I.extension) || "bin"
346
- ), D = ie(
379
+ o.value = 35;
380
+ const j = await b(l, U, i);
381
+ o.value = 50;
382
+ const F = await O(l, j, i);
383
+ o.value = 80;
384
+ const C = G[i.encoder], A = X(
385
+ f.name,
386
+ (C == null ? void 0 : C.extension) || "bin"
387
+ ), L = H(
347
388
  F,
348
- M,
349
- (I == null ? void 0 : I.mimeType) || "application/octet-stream"
350
- ), B = {
351
- file: D,
352
- size: D.size,
353
- width: U.width,
354
- height: U.height,
355
- blobUrl: V(D)
356
- }, l = {
357
- original: v,
358
- compressed: B,
359
- savingsBytes: v.size - B.size,
360
- savingsPercent: le(v.size, B.size),
361
- encoderType: o.encoder,
362
- encoderOptions: o.encoderOptions
389
+ A,
390
+ (C == null ? void 0 : C.mimeType) || "application/octet-stream"
391
+ ), M = {
392
+ file: L,
393
+ size: L.size,
394
+ width: j.width,
395
+ height: j.height,
396
+ blobUrl: V(L)
397
+ }, m = {
398
+ original: g,
399
+ compressed: M,
400
+ savingsBytes: g.size - M.size,
401
+ savingsPercent: K(g.size, M.size),
402
+ encoderType: i.encoder,
403
+ encoderOptions: i.encoderOptions
363
404
  };
364
- return n.value = l, e.value = 100, l;
365
- } catch (v) {
366
- if (v instanceof DOMException && v.name === "AbortError")
367
- throw r.value = "已取消", v;
368
- const y = v instanceof Error ? v.message : "压缩失败";
369
- throw r.value = y, console.error("Compression error:", v), v;
405
+ return t.value = m, o.value = 100, m;
406
+ } catch (g) {
407
+ if (g instanceof DOMException && g.name === "AbortError")
408
+ throw n.value = "已取消", g;
409
+ const x = g instanceof Error ? g.message : "压缩失败";
410
+ throw n.value = x, console.error("Compression error:", g), g;
370
411
  } finally {
371
- t.value = !1, u.value = null;
412
+ r.value = !1, e.value = null;
372
413
  }
373
414
  }
374
- function R() {
375
- u.value && u.value.abort();
415
+ function P() {
416
+ e.value && e.value.abort();
376
417
  }
377
- function O() {
378
- if (!n.value) return;
379
- const { compressed: d } = n.value, o = document.createElement("a");
380
- o.href = d.blobUrl, o.download = d.file.name, document.body.appendChild(o), o.click(), document.body.removeChild(o);
418
+ function T() {
419
+ if (!t.value) return;
420
+ const { compressed: f } = t.value, i = document.createElement("a");
421
+ i.href = f.blobUrl, i.download = f.file.name, document.body.appendChild(i), i.click(), document.body.removeChild(i);
381
422
  }
382
423
  return {
383
- compress: G,
384
- cancel: R,
385
- downloadResult: O,
386
- isCompressing: t,
387
- progress: e,
388
- error: r,
389
- result: n,
390
- canCancel: h
424
+ compress: q,
425
+ cancel: P,
426
+ downloadResult: T,
427
+ isCompressing: r,
428
+ progress: o,
429
+ error: n,
430
+ result: t,
431
+ canCancel: p
391
432
  };
392
433
  }
393
434
  function fe() {
394
- const t = b("mozJPEG"), e = b({}), r = q(() => L[t.value]), n = q(
395
- () => W[t.value]
396
- ), u = q(
397
- () => re.map((w) => ({
398
- type: w,
399
- label: L[w].label,
400
- mimeType: L[w].mimeType,
401
- extension: L[w].extension
435
+ const r = y("mozJPEG"), o = y({}), n = R(() => G[r.value]), t = R(
436
+ () => N[r.value]
437
+ ), e = R(
438
+ () => ae.map((u) => ({
439
+ type: u,
440
+ label: G[u].label,
441
+ mimeType: G[u].mimeType,
442
+ extension: G[u].extension
402
443
  }))
403
444
  );
404
- function p(w) {
405
- t.value = w, e.value = { ...W[w] };
445
+ function s(u) {
446
+ r.value = u, o.value = { ...N[u] };
406
447
  }
407
- function c(w, P) {
408
- e.value = {
409
- ...e.value,
410
- [w]: P
448
+ function a(u, h) {
449
+ o.value = {
450
+ ...o.value,
451
+ [u]: h
411
452
  };
412
453
  }
413
- function h() {
414
- e.value = { ...n.value };
454
+ function p() {
455
+ o.value = { ...t.value };
415
456
  }
416
- return h(), {
417
- selectedEncoder: t,
418
- encoderOptions: e,
419
- currentMeta: r,
420
- currentDefaults: n,
421
- availableEncoders: u,
422
- selectEncoder: p,
423
- updateOption: c,
424
- resetOptions: h
457
+ return p(), {
458
+ selectedEncoder: r,
459
+ encoderOptions: o,
460
+ currentMeta: n,
461
+ currentDefaults: t,
462
+ availableEncoders: e,
463
+ selectEncoder: s,
464
+ updateOption: a,
465
+ resetOptions: p
425
466
  };
426
467
  }
427
- const ve = { class: "image-compressor" }, ge = {
468
+ const ve = { class: "image-compressor" }, we = {
428
469
  key: 0,
429
470
  class: "preview-area"
430
- }, we = { class: "preview-item" }, be = ["src"], he = { class: "preview-item" }, ye = ["src"], Ee = {
471
+ }, ge = { class: "preview-item" }, be = ["src"], Ee = { class: "preview-item" }, he = ["src"], ye = {
431
472
  key: 1,
432
473
  class: "compressing"
433
- }, _e = ["value"], ke = { key: 2 }, xe = {
474
+ }, xe = ["value"], _e = { key: 2 }, ze = {
434
475
  key: 1,
435
476
  class: "encoder-settings"
436
- }, Oe = ["value"], Te = {
477
+ }, ke = ["value"], Oe = {
437
478
  key: 0,
438
479
  class: "quality-control"
439
- }, ze = ["value"], Pe = {
480
+ }, Pe = ["value"], Te = {
440
481
  key: 2,
441
482
  class: "actions"
442
- }, Ie = ["disabled"], Ce = {
483
+ }, De = ["disabled"], Ie = {
443
484
  key: 3,
444
485
  class: "error"
445
- }, Re = /* @__PURE__ */ H({
486
+ }, qe = /* @__PURE__ */ Z({
446
487
  __name: "ImageCompressor",
447
488
  props: {
448
489
  defaultEncoder: {},
449
490
  defaultOptions: {}
450
491
  },
451
492
  emits: ["success", "error", "cancel"],
452
- setup(t, { emit: e }) {
453
- const r = t, n = e, u = b(null), p = b(null), c = b(""), h = b(""), {
454
- compress: w,
455
- cancel: P,
456
- downloadResult: x,
457
- isCompressing: C,
458
- progress: G,
459
- error: R,
460
- result: O,
461
- canCancel: d
462
- } = pe(), {
463
- selectedEncoder: o,
464
- encoderOptions: a,
465
- availableEncoders: i,
466
- selectEncoder: f,
467
- updateOption: v
493
+ setup(r, { emit: o }) {
494
+ const n = r, t = o, e = y(null), s = y(null), a = y(""), p = y(""), {
495
+ compress: u,
496
+ cancel: h,
497
+ downloadResult: b,
498
+ isCompressing: O,
499
+ progress: q,
500
+ error: P,
501
+ result: T,
502
+ canCancel: f
503
+ } = me(), {
504
+ selectedEncoder: i,
505
+ encoderOptions: l,
506
+ availableEncoders: d,
507
+ selectEncoder: w,
508
+ updateOption: g
468
509
  } = fe();
469
- r.defaultEncoder && f(r.defaultEncoder), r.defaultOptions && Object.entries(r.defaultOptions).forEach(([l, s]) => v(l, s));
470
- const y = q(
471
- () => p.value ? $(p.value.size) : ""
472
- ), j = q(
473
- () => O.value ? $(O.value.compressed.size) : ""
474
- ), U = q(
475
- () => ["mozJPEG", "webP", "jxl", "browserJPEG", "wp2"].includes(o.value)
510
+ n.defaultEncoder && w(n.defaultEncoder), n.defaultOptions && Object.entries(n.defaultOptions).forEach(([m, c]) => g(m, c));
511
+ const x = R(
512
+ () => s.value ? $(s.value.size) : ""
513
+ ), U = R(
514
+ () => T.value ? $(T.value.compressed.size) : ""
515
+ ), j = R(
516
+ () => ["mozJPEG", "webP", "jxl", "browserJPEG", "wp2"].includes(i.value)
476
517
  );
477
- function F(l) {
478
- const s = l.target;
479
- s.files && s.files[0] && M(s.files[0]);
518
+ function F(m) {
519
+ const c = m.target;
520
+ c.files && c.files[0] && A(c.files[0]);
480
521
  }
481
- function I(l) {
482
- var s;
483
- (s = l.dataTransfer) != null && s.files[0] && M(l.dataTransfer.files[0]);
522
+ function C(m) {
523
+ var c;
524
+ (c = m.dataTransfer) != null && c.files[0] && A(m.dataTransfer.files[0]);
484
525
  }
485
- function M(l) {
486
- l.type.startsWith("image/") && (p.value = l, c.value = URL.createObjectURL(l), h.value && (URL.revokeObjectURL(h.value), h.value = ""));
526
+ function A(m) {
527
+ m.type.startsWith("image/") && (a.value && URL.revokeObjectURL(a.value), p.value && (URL.revokeObjectURL(p.value), p.value = ""), s.value = m, a.value = URL.createObjectURL(m));
487
528
  }
488
- function D() {
489
- f(o.value);
529
+ function L() {
530
+ w(i.value);
490
531
  }
491
- async function B() {
492
- if (p.value)
532
+ async function M() {
533
+ if (s.value)
493
534
  try {
494
- const l = {
495
- encoder: o.value,
496
- encoderOptions: { ...a.value }
497
- }, s = await w(p.value, l);
498
- h.value = s.compressed.blobUrl, n("success", s);
499
- } catch (l) {
500
- if (l instanceof DOMException && l.name === "AbortError") {
501
- n("cancel");
535
+ const m = {
536
+ encoder: i.value,
537
+ encoderOptions: { ...l.value }
538
+ }, c = await u(s.value, m);
539
+ p.value = c.compressed.blobUrl, t("success", c);
540
+ } catch (m) {
541
+ if (m instanceof DOMException && m.name === "AbortError") {
542
+ t("cancel");
502
543
  return;
503
544
  }
504
- n("error", l instanceof Error ? l : new Error(String(l)));
545
+ t("error", m instanceof Error ? m : new Error(String(m)));
505
546
  }
506
547
  }
507
- return K(
508
- () => p.value,
509
- (l, s) => {
510
- l && l !== s && c.value && URL.revokeObjectURL(c.value);
511
- }
512
- ), (l, s) => (_(), k("div", ve, [
513
- g("div", {
548
+ return (m, c) => (z(), k("div", ve, [
549
+ E("div", {
514
550
  class: "upload-zone",
515
- onDrop: N(I, ["prevent"]),
516
- onDragover: s[1] || (s[1] = N(() => {
551
+ onDrop: J(C, ["prevent"]),
552
+ onDragover: c[1] || (c[1] = J(() => {
517
553
  }, ["prevent"]))
518
554
  }, [
519
- g("input", {
555
+ E("input", {
520
556
  type: "file",
521
557
  accept: "image/*",
522
558
  onChange: F,
523
559
  ref_key: "fileInput",
524
- ref: u,
560
+ ref: e,
525
561
  class: "file-input"
526
562
  }, null, 544),
527
- g("div", {
563
+ E("div", {
528
564
  class: "upload-prompt",
529
- onClick: s[0] || (s[0] = (E) => {
530
- var A;
531
- return (A = u.value) == null ? void 0 : A.click();
565
+ onClick: c[0] || (c[0] = (_) => {
566
+ var W;
567
+ return (W = e.value) == null ? void 0 : W.click();
532
568
  })
533
569
  }, [
534
- S(l.$slots, "upload-prompt", {}, () => [
535
- s[6] || (s[6] = g("p", null, "拖拽图片到此处,或点击上传", -1))
570
+ S(m.$slots, "upload-prompt", {}, () => [
571
+ c[6] || (c[6] = E("p", null, "拖拽图片到此处,或点击上传", -1))
536
572
  ], !0)
537
573
  ])
538
574
  ], 32),
539
- c.value ? (_(), k("div", ge, [
540
- g("div", we, [
541
- s[7] || (s[7] = g("h4", null, "原始图片", -1)),
542
- g("img", {
543
- src: c.value,
575
+ a.value ? (z(), k("div", we, [
576
+ E("div", ge, [
577
+ c[7] || (c[7] = E("h4", null, "原始图片", -1)),
578
+ E("img", {
579
+ src: a.value,
544
580
  alt: "Original"
545
581
  }, null, 8, be),
546
- g("p", null, T(y.value), 1)
582
+ E("p", null, D(x.value), 1)
547
583
  ]),
548
- g("div", he, [
549
- s[8] || (s[8] = g("h4", null, "压缩后", -1)),
550
- h.value ? (_(), k("img", {
584
+ E("div", Ee, [
585
+ c[8] || (c[8] = E("h4", null, "压缩后", -1)),
586
+ p.value ? (z(), k("img", {
551
587
  key: 0,
552
- src: h.value,
588
+ src: p.value,
553
589
  alt: "Compressed"
554
- }, null, 8, ye)) : m(C) ? (_(), k("div", Ee, [
555
- g("progress", {
556
- value: m(G),
590
+ }, null, 8, he)) : v(O) ? (z(), k("div", ye, [
591
+ E("progress", {
592
+ value: v(q),
557
593
  max: "100"
558
- }, null, 8, _e),
559
- g("p", null, "压缩中... " + T(m(G)) + "%", 1)
560
- ])) : z("", !0),
561
- j.value ? (_(), k("p", ke, T(j.value), 1)) : z("", !0)
594
+ }, null, 8, xe),
595
+ E("p", null, "压缩中... " + D(v(q)) + "%", 1)
596
+ ])) : I("", !0),
597
+ U.value ? (z(), k("p", _e, D(U.value), 1)) : I("", !0)
562
598
  ])
563
- ])) : z("", !0),
564
- c.value ? (_(), k("div", xe, [
565
- s[9] || (s[9] = g("label", null, "编码器:", -1)),
566
- X(g("select", {
567
- "onUpdate:modelValue": s[2] || (s[2] = (E) => Y(o) ? o.value = E : null),
568
- onChange: D
599
+ ])) : I("", !0),
600
+ a.value ? (z(), k("div", ze, [
601
+ c[9] || (c[9] = E("label", null, "编码器:", -1)),
602
+ ee(E("select", {
603
+ "onUpdate:modelValue": c[2] || (c[2] = (_) => te(i) ? i.value = _ : null),
604
+ onChange: L
569
605
  }, [
570
- (_(!0), k(Z, null, ee(m(i), (E) => (_(), k("option", {
571
- key: E.type,
572
- value: E.type
573
- }, T(E.label) + " (" + T(E.extension) + ") ", 9, Oe))), 128))
606
+ (z(!0), k(oe, null, re(v(d), (_) => (z(), k("option", {
607
+ key: _.type,
608
+ value: _.type
609
+ }, D(_.label) + " (" + D(_.extension) + ") ", 9, ke))), 128))
574
610
  ], 544), [
575
- [te, m(o)]
611
+ [ne, v(i)]
576
612
  ]),
577
- U.value ? (_(), k("div", Te, [
578
- g("label", null, [
579
- ne(" 质量: " + T(m(a).quality ?? 75) + " ", 1),
580
- g("input", {
613
+ j.value ? (z(), k("div", Oe, [
614
+ E("label", null, [
615
+ se(" 质量: " + D(v(l).quality ?? 75) + " ", 1),
616
+ E("input", {
581
617
  type: "range",
582
618
  min: "0",
583
619
  max: "100",
584
- value: m(a).quality ?? 75,
585
- onInput: s[3] || (s[3] = (E) => m(v)("quality", Number(E.target.value))),
586
- "-------": "",
587
- REPLACE: ""
588
- }, null, 40, ze)
620
+ value: v(l).quality ?? 75,
621
+ onInput: c[3] || (c[3] = (_) => v(g)("quality", Number(_.target.value)))
622
+ }, null, 40, Pe)
589
623
  ])
590
- ])) : z("", !0),
591
- S(l.$slots, "encoder-settings", {
592
- encoder: m(o),
593
- options: m(a)
624
+ ])) : I("", !0),
625
+ S(m.$slots, "encoder-settings", {
626
+ encoder: v(i),
627
+ options: v(l)
594
628
  }, void 0, !0)
595
- ])) : z("", !0),
596
- c.value ? (_(), k("div", Pe, [
597
- g("button", {
598
- onClick: B,
599
- disabled: m(C),
629
+ ])) : I("", !0),
630
+ a.value ? (z(), k("div", Te, [
631
+ E("button", {
632
+ onClick: M,
633
+ disabled: v(O),
600
634
  class: "btn-primary"
601
- }, T(m(C) ? "压缩中..." : "开始压缩"), 9, Ie),
602
- m(d) ? (_(), k("button", {
635
+ }, D(v(O) ? "压缩中..." : "开始压缩"), 9, De),
636
+ v(f) ? (z(), k("button", {
603
637
  key: 0,
604
- onClick: s[4] || (s[4] = //@ts-ignore
605
- (...E) => m(P) && m(P)(...E)),
638
+ onClick: c[4] || (c[4] = //@ts-ignore
639
+ (..._) => v(h) && v(h)(..._)),
606
640
  class: "btn-secondary"
607
- }, " 取消 ")) : z("", !0),
608
- m(O) ? (_(), k("button", {
641
+ }, " 取消 ")) : I("", !0),
642
+ v(T) ? (z(), k("button", {
609
643
  key: 1,
610
- onClick: s[5] || (s[5] = //@ts-ignore
611
- (...E) => m(x) && m(x)(...E)),
644
+ onClick: c[5] || (c[5] = //@ts-ignore
645
+ (..._) => v(b) && v(b)(..._)),
612
646
  class: "btn-success"
613
- }, " 下载 (节省 " + T(m(O).savingsPercent) + "%) ", 1)) : z("", !0)
614
- ])) : z("", !0),
615
- m(R) ? (_(), k("div", Ce, T(m(R)), 1)) : z("", !0)
647
+ }, " 下载 (节省 " + D(v(T).savingsPercent) + "%) ", 1)) : I("", !0)
648
+ ])) : I("", !0),
649
+ v(P) ? (z(), k("div", Ie, D(v(P)), 1)) : I("", !0)
616
650
  ]));
617
651
  }
618
- }), Ue = (t, e) => {
619
- const r = t.__vccOpts || t;
620
- for (const [n, u] of e)
621
- r[n] = u;
622
- return r;
623
- }, Le = /* @__PURE__ */ Ue(Re, [["__scopeId", "data-v-86f2b482"]]), je = [
652
+ }), Ce = (r, o) => {
653
+ const n = r.__vccOpts || r;
654
+ for (const [t, e] of o)
655
+ n[t] = e;
656
+ return n;
657
+ }, Me = /* @__PURE__ */ Ce(qe, [["__scopeId", "data-v-d7e6eec9"]]), Ae = [
624
658
  { value: "lanczos3", label: "Lanczos3" },
625
659
  { value: "catrom", label: "Catrom" },
626
660
  { value: "mitchell", label: "Mitchell" },
627
661
  { value: "triangle", label: "Triangle" },
628
662
  { value: "vector", label: "Vector" }
629
- ], De = [
663
+ ], Be = [
630
664
  { value: "stretch", label: "拉伸" },
631
665
  { value: "contain", label: "适应" }
632
- ], Be = [
666
+ ], Fe = [
633
667
  { value: 0, label: "不旋转" },
634
668
  { value: 90, label: "顺时针 90°" },
635
669
  { value: 180, label: "旋转 180°" },
636
670
  { value: 270, label: "逆时针 90°" }
637
- ];
671
+ ], je = {
672
+ mozJPEG: "mozjpegEncode",
673
+ webP: "webpEncode",
674
+ avif: "avifEncode",
675
+ jxl: "jxlEncode",
676
+ oxiPNG: "oxipngEncode",
677
+ qoi: "qoiEncode",
678
+ wp2: "wp2Encode"
679
+ };
680
+ function Ge() {
681
+ return new Worker(
682
+ new URL(
683
+ /* @vite-ignore */
684
+ "/assets/imageWorker-Qv3pCULy.js",
685
+ import.meta.url
686
+ ),
687
+ { type: "module" }
688
+ );
689
+ }
690
+ function Re(r, o, n, t) {
691
+ return new Promise((e, s) => {
692
+ const a = Ge();
693
+ let p = !1;
694
+ function u() {
695
+ p || (p = !0, a.terminate());
696
+ }
697
+ a.addEventListener("message", (O) => {
698
+ const { result: q, error: P } = O.data;
699
+ u(), P ? s(new Error(P)) : e(q.buffer);
700
+ }), a.addEventListener("error", (O) => {
701
+ u(), s(O.error || O);
702
+ });
703
+ const h = () => {
704
+ u(), s(new DOMException("AbortError", "AbortError"));
705
+ };
706
+ t && t.addEventListener("abort", h, { once: !0 });
707
+ const b = je[r];
708
+ if (!b) {
709
+ u(), s(new Error(`Unknown encoder: ${r}`));
710
+ return;
711
+ }
712
+ a.postMessage({ id: 1, method: b, args: [o, n] });
713
+ });
714
+ }
715
+ async function Ne(r, o = {}) {
716
+ const { signal: n } = o;
717
+ if (n != null && n.aborted)
718
+ throw new DOMException("AbortError", "AbortError");
719
+ const t = o.encoder || "mozJPEG", e = {
720
+ ...N[t],
721
+ ...o.encoderOptions
722
+ }, s = G[t], a = await Q(r);
723
+ if (n != null && n.aborted)
724
+ throw new DOMException("AbortError", "AbortError");
725
+ let p;
726
+ if (t === "browserJPEG")
727
+ p = await (await B(
728
+ a,
729
+ "image/jpeg",
730
+ e.quality
731
+ )).arrayBuffer();
732
+ else if (t === "browserPNG")
733
+ p = await (await B(a, "image/png")).arrayBuffer();
734
+ else {
735
+ if (t === "browserGIF")
736
+ throw new Error("Browser GIF encoding not supported directly");
737
+ p = await Re(
738
+ t,
739
+ a,
740
+ e,
741
+ n
742
+ );
743
+ }
744
+ if (n != null && n.aborted)
745
+ throw new DOMException("AbortError", "AbortError");
746
+ const u = X(r.name, s.extension), h = H(
747
+ p,
748
+ u,
749
+ s.mimeType
750
+ );
751
+ return {
752
+ file: h,
753
+ originalSize: r.size,
754
+ compressedSize: h.size,
755
+ savingsBytes: r.size - h.size,
756
+ savingsPercent: K(r.size, h.size),
757
+ width: a.width,
758
+ height: a.height,
759
+ encoderType: t,
760
+ encoderOptions: e
761
+ };
762
+ }
638
763
  export {
639
- W as DEFAULT_ENCODER_OPTIONS,
640
- re as ENCODER_LIST,
641
- L as ENCODER_REGISTRY,
642
- De as FIT_METHODS,
643
- Le as ImageCompressor,
644
- je as RESIZE_METHODS,
645
- Be as ROTATE_OPTIONS,
646
- ie as arrayBufferToFile,
647
- oe as blobToImageData,
648
- le as calculateSavings,
649
- ae as canDecodeImageType,
764
+ N as DEFAULT_ENCODER_OPTIONS,
765
+ ae as ENCODER_LIST,
766
+ G as ENCODER_REGISTRY,
767
+ Be as FIT_METHODS,
768
+ Me as ImageCompressor,
769
+ Ae as RESIZE_METHODS,
770
+ Fe as ROTATE_OPTIONS,
771
+ H as arrayBufferToFile,
772
+ Q as blobToImageData,
773
+ K as calculateSavings,
774
+ le as canDecodeImageType,
775
+ Ne as compressFile,
650
776
  V as createBlobUrl,
651
777
  $ as formatBytes,
652
- ce as generateCompressedFilename,
653
- Ge as getImageDimensions,
654
- J as imageDataToBlob,
655
- ue as revokeBlobUrl,
656
- se as sniffMimeType,
657
- pe as useCompression,
778
+ X as generateCompressedFilename,
779
+ Le as getImageDimensions,
780
+ B as imageDataToBlob,
781
+ ce as revokeBlobUrl,
782
+ ie as sniffMimeType,
783
+ me as useCompression,
658
784
  fe as useEncoderRegistry,
659
- me as useWorker
785
+ pe as useWorker
660
786
  };