Using magick-wasm in the browser without node.js? #112
Replies: 5 comments 7 replies
-
It looks like that import alias doesn't work? I had to use this instead: <html>
<head>
<meta charset="UTF-8" />
<title>Magick-WASM Example</title>
<script type="module">
//import the library to talk to imagemagick
import * as foo from '/index.umd.js';
console.log(window['magick-wasm']);
</script>
</head>
<body></body>
</html> |
Beta Was this translation helpful? Give feedback.
-
The const magickModule = window['magick-wasm'];
await magickModule.initializeImageMagick();
const magick = magickModule.Magick;
console.log(magick.delegates); |
Beta Was this translation helpful? Give feedback.
-
Has anyone managed to import with a bundler (vite, also using I tried: import magickWasm from '@imagemagick/magick-wasm/magick.wasm?init'
// import magickWasm from '@imagemagick/magick-wasm/magick.wasm?raw'
initializeImageMagick(magickWasm)... And some variations but everything I've tried causes exceptions |
Beta Was this translation helpful? Give feedback.
-
I know I'm late, but this is what I'm doing to use this awesome library on my NextJS project:
import { ImageMagick, MagickFormat, MagickGeometry, initializeImageMagick } from '@imagemagick/magick-wasm'
let initializing = false
let initialized = false
async function load() {
if (initializing) {
// Wait until the first load execution is complete...
while (true) {
await delayed(500)
if (!initializing && !initialized) {
break
} else if (initialized) {
return
}
}
}
initializing = true
try {
const response = await fetch(process.env.NEXT_PUBLIC_IMAGE_MAGICK_WASM_URL, {
cache: 'force-cache'
})
await initializeImageMagick(await response.arrayBuffer())
initialized = true
initializing = false
} catch (err) {
initializing = false
throw err
}
}
export async function extractGIFFrames(file, maxFrameSize = 150, signal) {
if (!initialized) {
await load()
}
if (signal?.aborted) {
throw new Error('Aborted')
}
const buffer = await file.arrayBuffer()
const data = new Uint8Array(buffer)
return new Promise(resolve => {
const blobs = []
ImageMagick.readCollection(data, async frames => {
frames.coalesce() // Fix GIF frames artifacts
frames.forEach(frame => {
const geometry = new MagickGeometry(maxFrameSize)
geometry.greater = true
frame.resize(geometry)
const blob = await new Promise(resolve => {
frame.write(MagickFormat.Jpeg, data => {
const blob = new Blob([data], { type: 'image/jpeg' })
resolve(blob)
})
})
blobs.push(blob)
})
resolve(blobs)
})
})
} |
Beta Was this translation helpful? Give feedback.
-
I was able to get it working on Nextjs 14:
|
Beta Was this translation helpful? Give feedback.
-
Hi
I'm trying to import magick.wasm to use within my own javascript, without using node.js - but it doesn't seem to be working.
Downloading the library gives me 4 files:
index.d.ts
,index.mjs
,index.umd.js
andmagick.wasm
I've tried both using
index.umd.js
and a "browserified" bundle ofindex.mjs
andindex.umd.js
but so far no luckI have my HTML like so to see if the Magick functions are being imported:
...but the array is returning empty. I must be doing something wrong. Is there any way to do this without using npm/node.js?
Beta Was this translation helpful? Give feedback.
All reactions