Readme

Code from https://deno.com/blog/build-image-resizing-api

Useful for compressing an image before sending to chatgpt4v, for example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { ImageMagick, initializeImageMagick, MagickGeometry } from "https://deno.land/x/imagemagick_deno@0.0.14/mod.ts";
export async function modifyImage(
file: File,
params: { width: number; height: number },
) {
const imageBuffer = new Uint8Array(await file.arrayBuffer());
const sizingData = new MagickGeometry(
params.width,
params.height,
);
sizingData.ignoreAspectRatio = params.height > 0 && params.width > 0;
return new Promise<File>((resolve) => {
ImageMagick.read(imageBuffer, (image) => {
image.resize(sizingData);
image.write((data) => resolve(new File([data], file.name, { type: file.type })));
});
});
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
April 18, 2024