File tree Expand file tree Collapse file tree 3 files changed +26
-12
lines changed Expand file tree Collapse file tree 3 files changed +26
-12
lines changed Original file line number Diff line number Diff line change @@ -7,27 +7,27 @@ Scripts to transform the source code of
7
7
sh run.sh
8
8
```
9
9
10
- To import Imagemagick in your Deno project:
10
+ To import ImageMagick into your Deno project:
11
11
12
12
``` ts
13
13
import {
14
14
ImageMagick ,
15
15
IMagickImage ,
16
- initializeImageMagick ,
16
+ initialize ,
17
17
MagickFormat ,
18
18
} from " https://deno.land/x/imagemagick_deno/mod.ts" ;
19
19
20
- await initializeImageMagick (); // make sure to initialize first!
20
+ await initialize (); // make sure to initialize first!
21
21
22
22
const data: Uint8Array = await Deno .readFile (" image.jpg" );
23
23
24
- ImageMagick .read (data , (img : IMagickImage ) => {
24
+ await ImageMagick .read (data , (img : IMagickImage ) => {
25
25
img .resize (200 , 100 );
26
26
img .blur (20 , 6 );
27
27
28
- img .write (
29
- (data : Uint8Array ) => Deno .writeFile (" image-blur.jpg" , data ),
28
+ await img .write (
30
29
MagickFormat .Jpeg ,
30
+ (data : Uint8Array ) => Deno .writeFile (" image-blur.jpg" , data ),
31
31
);
32
32
});
33
33
```
Original file line number Diff line number Diff line change @@ -19,6 +19,6 @@ export async function initialize() {
19
19
}
20
20
21
21
const response = await fetch ( wasmUrl ) ;
22
- await cache . put ( wasmUrl , response ) ;
23
- await initializeImageMagick ( await response . clone ( ) . arrayBuffer ( ) ) ;
22
+ await cache . put ( wasmUrl , response . clone ( ) ) ;
23
+ await initializeImageMagick ( await response . arrayBuffer ( ) ) ;
24
24
}
Original file line number Diff line number Diff line change @@ -2,9 +2,23 @@ export * from "./src/index.ts";
2
2
import { initializeImageMagick } from "./src/index.ts" ;
3
3
4
4
export async function initialize ( ) {
5
- const wasmFile = import . meta. resolve ( "./src/wasm/magick_native.wasm" ) ;
6
- const file = await fetch ( wasmFile ) ;
7
- const wasm = await file . arrayBuffer ( ) ;
5
+ const wasmUrl = new URL ( import . meta. resolve ( "./src/wasm/magick_native.wasm" ) ) ;
8
6
9
- await initializeImageMagick ( wasm ) ;
7
+ if ( wasmUrl . protocol === "file:" ) {
8
+ await initializeImageMagick ( await Deno . readFile ( wasmUrl ) ) ;
9
+ return ;
10
+ }
11
+
12
+ const cache = await caches . open ( "magick_native" ) ;
13
+ const cached = await cache . match ( wasmUrl ) ;
14
+
15
+ if ( cached ) {
16
+ const wasm = await cached . arrayBuffer ( ) ;
17
+ await initializeImageMagick ( wasm ) ;
18
+ return ;
19
+ }
20
+
21
+ const response = await fetch ( wasmUrl ) ;
22
+ await cache . put ( wasmUrl , response . clone ( ) ) ;
23
+ await initializeImageMagick ( await response . arrayBuffer ( ) ) ;
10
24
}
You can’t perform that action at this time.
0 commit comments