Image Converter: how it works and how to build one
Convert images between PNG, JPEG and WebP. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
Turn PNG screenshots into JPEG or WebP, or WebP downloads into PNG. Transparent areas get a background color you choose when converting to JPEG, which has no transparency.
When to use it
- HEIC/PNG to JPG for upload forms
- PNG to WebP for websites
- SVG to PNG for apps that don’t take SVG
How to use it
- Drop images or click to choose them.
- Pick the output format.
- Download the converted files.
Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/image-converter
How it works
Converting formats in the browser is decode then encode: load the file into an <img> (the browser decodes JPG, PNG, WebP, GIF, BMP, SVG), draw it on a canvas, and export with toBlob in the new format.
JPEG has no transparency, so transparent areas need a background color first, or they turn black.
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.
convert.ts (ts)
export async function convertImage(file: File, type: "image/png" | "image/jpeg" | "image/webp") {
const url = URL.createObjectURL(file);
const img = new Image(); img.src = url; await img.decode();
const c = Object.assign(document.createElement("canvas"), { width: img.naturalWidth, height: img.naturalHeight });
const g = c.getContext("2d")!;
if (type === "image/jpeg") { g.fillStyle = "#fff"; g.fillRect(0, 0, c.width, c.height); } // no alpha in JPEG
g.drawImage(img, 0, 0);
URL.revokeObjectURL(url);
return new Promise<Blob>((r) => c.toBlob((b) => r(b!), type, 0.92));
}
Make it production-ready
- Set a size for SVG input (it may have no natural size).
- Batch convert and zip the results.
Questions
Why did my transparent PNG get a white background?
JPEG cannot store transparency. Pick WebP or PNG to keep it.
Free, runs in your browser, nothing is uploaded.