-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (70 loc) · 2.66 KB
/
main.go
File metadata and controls
93 lines (70 loc) · 2.66 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"syscall/js"
"fmt"
)
func processHeifFile(this js.Value, args []js.Value) interface{} {
fmt.Println("processHeifFile called")
buffer := args[0]
libheifInstance := args[1]
callback := args[2]
decoder := libheifInstance.Get("HeifDecoder").New()
image := decoder.Call("decode", buffer).Index(0)
origWidth := image.Call("get_width").Int()
origHeight := image.Call("get_height").Int()
fmt.Printf("Original image size: %dx%d\n", origWidth, origHeight)
callback.Invoke(fmt.Sprintf("Original image size: %dx%d", origWidth, origHeight))
// Set maximum thumbnail size
maxSize := 150
var width, height int
// Calculate thumbnail size while preserving aspect ratio
if origWidth > origHeight {
width = maxSize
height = (origHeight * maxSize) / origWidth
} else {
height = maxSize
width = (origWidth * maxSize) / origHeight
}
fmt.Printf("Thumbnail size: %dx%d\n", width, height)
callback.Invoke(fmt.Sprintf("Thumbnail size: %dx%d", width, height))
// Create a temporary offscreen canvas to draw the thumbnail
offscreenCanvas := js.Global().Get("document").Call("createElement", "canvas")
offscreenCtx := offscreenCanvas.Call("getContext", "2d")
offscreenCanvas.Set("width", width)
offscreenCanvas.Set("height", height)
// Convert the decoded image to ImageData
imageData := offscreenCtx.Call("createImageData", origWidth, origHeight)
image.Call("display", imageData, js.FuncOf(func(this js.Value, args []js.Value) interface{} {
offscreenCtx.Call("putImageData", args[0], 0, 0)
// Now create an ImageBitmap from the ImageData
imageBitmapPromise := js.Global().Call("createImageBitmap", args[0])
imageBitmapPromise.Call("then", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
imageBitmap := args[0]
// Draw the ImageBitmap on the offscreen canvas
offscreenCtx.Call("drawImage", imageBitmap, 0, 0, origWidth, origHeight, 0, 0, width, height)
// Create a Blob from the offscreen canvas
offscreenCanvas.Call("toBlob", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
blob := args[0]
// Read the Blob as a base64-encoded data URL
reader := js.Global().Get("FileReader").New()
reader.Call("readAsDataURL", blob)
reader.Set("onloadend", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
dataURL := reader.Get("result").String()
// Return the data URL back to the JavaScript code
fmt.Println("Returning data URL")
callback.Invoke(dataURL)
return nil
}))
return nil
}), "image/png")
return nil
}))
return nil
}))
return nil
}
func main() {
c := make(chan struct{}, 0)
js.Global().Set("processHeifFile", js.FuncOf(processHeifFile))
<-c
}