Pixelshift

Core API

Convert images without UI using the framework-independent browser API.

Install the core when you need conversion behavior without the Lit component:

pnpm add pixelshift-core

The core API runs in the browser and depends on canvas, Blob, File, and browser image decoders.

convertImage()

import { convertImage } from "pixelshift-core";

const result = await convertImage(file, {
  format: "webp",
  quality: 0.85,
  maxWidth: 2400,
  maxHeight: 2400,
});

Conversion options

OptionTypeDefaultPurpose
format"png" | "jpeg" | "webp"requiredSelect the output MIME type
qualitynumber from 0 to 10.85Set JPEG/WebP encoder quality
maxWidthpositive numberoriginal widthDownscale while preserving aspect ratio
maxHeightpositive numberoriginal heightDownscale while preserving aspect ratio
backgroundCSS color string#ffffff for JPEGFill transparent pixels when creating JPEG
maxBytespositive number25 MiBReject larger input files
maxPixelspositive number40 million pixelsReject larger decoded images
signalAbortSignalunsetCooperatively cancel at conversion boundaries

Conversion result

interface ConversionResult {
  blob: Blob;
  file: File;
  inputType: string;
  outputType: string;
  originalSize: number;
  convertedSize: number;
  width: number;
  height: number;
  durationMs: number;
}

Batch conversion

convertImages() processes files sequentially and returns results in input order:

import { convertImages } from "pixelshift-core";

const results = await convertImages(files, {
  format: "jpeg",
  quality: 0.9,
  background: "#ffffff",
});

Batch conversion is fail-fast. If one file fails, the promise rejects immediately and does not return partial results from earlier files.

Cancellation

Pass an AbortSignal to cancel before work begins or at the next conversion boundary:

const controller = new AbortController();

const conversion = convertImage(file, {
  format: "webp",
  signal: controller.signal,
});

controller.abort();
await conversion;

Cancellation rejects with an ImageConversionError whose code is ABORTED.

Errors

convertImage() and convertImages() reject with ImageConversionError for known conversion failures.

CodeMeaning
ABORTEDThe supplied signal cancelled conversion
DECODE_FAILEDThe browser could not decode the input
EMPTY_FILEThe input file contains no bytes
FILE_TOO_LARGEThe input exceeds maxBytes
IMAGE_TOO_LARGEDecoded dimensions exceed maxPixels
INVALID_OPTIONQuality or target-size inputs are invalid
UNSUPPORTED_INPUTThe input signature or MIME type is unsupported
UNSUPPORTED_OUTPUTThe browser cannot encode the requested format
import { ImageConversionError, convertImage } from "pixelshift-core";

try {
  await convertImage(file, { format: "webp" });
} catch (error) {
  if (error instanceof ImageConversionError) {
    console.error(error.code, error.message);
  }
}

Output support

Encoder support is browser-dependent. Probe it before offering an output format, or handle UNSUPPORTED_OUTPUT:

import { supportsOutputFormat } from "pixelshift-core";

if (supportsOutputFormat("webp")) {
  // It is safe to offer WebP in this browser.
}

Helpers

ExportPurpose
calculateTargetSizeCalculate aspect-ratio-preserving output dimensions
createOutputNameReplace a filename extension for the output format
detectImageTypeDetect a supported image MIME type
normalizeQualityApply the quality default and validate its range
supportsOutputFormatTest the current browser's canvas encoder
OUTPUT_FORMATSList the supported output format names
ImageConversionErrorRepresent a known failure with a stable error code

On this page