heic-to 1.2.2 → 1.3.1

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.
@@ -0,0 +1,28 @@
1
+ import {heicTo, isHeic} from '../../dist/heic-to.js'
2
+
3
+ const imagesContainer = document.querySelector(".images")
4
+ const messageLabel = document.querySelector("label")
5
+ const field = document.querySelector("input")
6
+
7
+ field.addEventListener('change', async (event) => {
8
+
9
+ let canvasDom = document.querySelector("canvas")
10
+ if (!canvasDom) {
11
+ canvasDom = document.createElement('canvas')
12
+ imagesContainer.appendChild(canvasDom)
13
+ }
14
+
15
+ const image = field.files[0]
16
+ if (await isHeic(image)) {
17
+ messageLabel.textContent = "It's a HEIC image"
18
+ const bitmapImage = await heicTo({blob: image, type: 'bitmap', options: {imageOrientation: 'flipY'}})
19
+ const ctx = canvasDom.getContext('2d')
20
+ canvasDom.width = bitmapImage.width
21
+ canvasDom.height = bitmapImage.height
22
+ ctx.drawImage(bitmapImage, 0, 0)
23
+ } else {
24
+ messageLabel.textContent = "It's not a HEIC image"
25
+ canvasDom.remove()
26
+ }
27
+
28
+ })
@@ -0,0 +1,47 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>heic-to</title>
7
+ <style>
8
+ body {
9
+ display: flex;
10
+ justify-content: center;
11
+ align-items: center;
12
+ flex-direction: column;
13
+ height: 100vh;
14
+ }
15
+ input {
16
+ border: 1px solid black;
17
+ padding: 10px;
18
+ }
19
+ label {
20
+ display: block;;
21
+ padding-top: 20px;
22
+ padding-bottom: 20px;
23
+ color: red;
24
+ }
25
+ .images {
26
+ width: 200px;
27
+ height: 200px;
28
+ }
29
+ img {
30
+ display: block;
31
+ width: 100%;
32
+ }
33
+ </style>
34
+ <meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate, post-check=0, pre-check=0">
35
+ <script src="./app.js" type="module"></script>
36
+ </head>
37
+ <body>
38
+
39
+ <h1>heic-to</h1>
40
+
41
+ <main>
42
+ <input type="file">
43
+ <label></label>
44
+ <div class="images"></div>
45
+ </main>
46
+ </body>
47
+ </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "heic-to",
3
- "version": "1.2.2",
3
+ "version": "1.3.1",
4
4
  "description": "Converting HEIF/HEIF image formats to PNG/JPEG in the browser",
5
5
  "main": "dist/heic-to.js",
6
6
  "exports": {
package/src/index.js CHANGED
@@ -79,23 +79,29 @@ const releaseCanvas = (canvas) => {
79
79
  ctx && ctx.clearRect(0, 0, 1, 1);
80
80
  }
81
81
 
82
- const heicTo = async ({blob, type, quality}) => {
83
- const imageBuffer = await blob.arrayBuffer()
84
- let canvas;
85
- try {
86
- canvas = await encodeByCanvas(imageBuffer);
87
- return await new Promise((resolve, reject) => canvas.toBlob(blob => {
88
- if (blob != null)
89
- resolve(blob);
90
- else
91
- reject(`Can't convert canvas to blob.`);
92
- }, type, quality));
93
- } finally {
94
- if (canvas) releaseCanvas(canvas);
82
+ const heicTo = async ({blob, type, quality, options}) => {
83
+ if (type == "bitmap") {
84
+ const imageBuffer = await blob.arrayBuffer();
85
+ const imageData = await decodeBuffer(imageBuffer);
86
+ return createImageBitmap(imageData, options);
87
+ } else {
88
+ const imageBuffer = await blob.arrayBuffer()
89
+ let canvas;
90
+ try {
91
+ canvas = await encodeByCanvas(imageBuffer);
92
+ return await new Promise((resolve, reject) => canvas.toBlob(blob => {
93
+ if (blob != null)
94
+ resolve(blob);
95
+ else
96
+ reject(`Can't convert canvas to blob.`);
97
+ }, type, quality));
98
+ } finally {
99
+ if (canvas) releaseCanvas(canvas);
100
+ }
95
101
  }
96
102
  };
97
103
 
98
104
  export {
99
105
  isHeic,
100
- heicTo
106
+ heicTo,
101
107
  }
package/src/next/index.js CHANGED
@@ -77,34 +77,24 @@ const releaseCanvas = (canvas) => {
77
77
  ctx && ctx.clearRect(0, 0, 1, 1);
78
78
  }
79
79
 
80
- const heicTo = async ({blob, type, quality}) => {
81
- const imageBuffer = await blob.arrayBuffer()
82
- let canvas;
83
- try {
84
- canvas = await encodeByCanvas(imageBuffer);
85
-
86
- return await new Promise((resolve, reject) => {
87
-
88
- canvas.convertToBlob({
89
- type: type,
90
- quality: quality,
91
- }).then(blob => {
92
- if (blob) {
93
- resolve(blob);
94
- } else {
95
- reject(`Can't convert canvas to blob.`);
96
- }
97
- }).catch(error => {
98
- reject(`Can't convert canvas to blob.`);
99
- })
100
-
101
- });
102
- } finally {
103
- if (canvas) releaseCanvas(canvas);
80
+ const heicTo = async ({blob, type, quality, options}) => {
81
+ if (type == "bitmap") {
82
+ const imageBuffer = await blob.arrayBuffer();
83
+ const imageData = await decodeBuffer(imageBuffer);
84
+ return createImageBitmap(imageData, options);
85
+ } else {
86
+ const imageBuffer = await blob.arrayBuffer()
87
+ let canvas;
88
+ try {
89
+ canvas = await encodeByCanvas(imageBuffer);
90
+ return await canvas.convertToBlob({ type, quality });
91
+ } finally {
92
+ if (canvas) releaseCanvas(canvas);
93
+ }
104
94
  }
105
95
  };
106
96
 
107
97
  export {
108
98
  isHeic,
109
- heicTo
99
+ heicTo,
110
100
  }