Package Exports
- @cf-wasm/png
- @cf-wasm/png/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@cf-wasm/png) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
PNG encoder and decoder CF workers
PNG encoder and decoder for Cloudflare Workers using WebAssembly.
Installation
Install the package:
npm install @cf-wasm/pngExample
Usage in Cloudflare Workers:
import * as photon from "@cf-wasm/photon";
import * as png from "@cf-wasm/png";
photon.initCloudflare();
png.initCloudflare();
const worker: ExportedHandler<Env> = {
async fetch() {
const imageUrl = "https://avatars.githubusercontent.com/u/314135";
const imageBuffer = await fetch(imageUrl).then((res) => res.arrayBuffer());
const imageBytes = new Uint8Array(imageBuffer);
const inputImage = photon.PhotonImage.new_from_byteslice(imageBytes);
// resizing using photon
const outputImage = photon.resize(inputImage, inputImage.get_width() * 0.5, inputImage.get_height() * 0.5, 1);
// encoding using png
const outputPng = png.encode(outputImage.get_raw_pixels(), outputImage.get_width(), outputImage.get_height());
const imageResponse = new Response(outputPng, {
headers: {
"Content-Type": "image/png"
}
});
inputImage.free();
outputImage.free();
return imageResponse;
}
};
export default worker;