CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tinify

Node.js client for the Tinify API that intelligently compresses, resizes, converts, and stores images in AVIF, WebP, JPEG, and PNG formats.

Pending
Overview
Eval results
Files

image-sources.mddocs/

Image Sources

Factory methods for creating image sources from files, buffers, or URLs. Sources represent images that can be processed with various transformations.

Capabilities

From File Path

Create a source from a local file path. The file will be read and uploaded to the Tinify API.

/**
 * Create a source from a local file path
 * @param path - Path to the image file on the local filesystem
 * @returns Source instance for chaining transformations
 * @example tinify.fromFile("./images/photo.jpg")
 */
function fromFile(path: string): Source;

Usage Examples:

const tinify = require("tinify");

tinify.key = "your-api-key";

// Basic compression
const source = tinify.fromFile("./images/photo.jpg");
await source.toFile("./images/compressed.jpg");

// With transformations
await tinify.fromFile("./images/large-photo.png")
  .resize({ method: "fit", width: 800, height: 600 })
  .toFile("./images/resized.png");

From Buffer

Create a source from image data in memory as a string or Uint8Array buffer.

/**
 * Create a source from image data in memory
 * @param data - Image data as string or Uint8Array buffer
 * @returns Source instance for chaining transformations
 * @example tinify.fromBuffer(imageBuffer)
 */
function fromBuffer(data: string | Uint8Array): Source;

Usage Examples:

const tinify = require("tinify");
import * as fs from "fs";

tinify.key = "your-api-key";

// From file buffer
const imageBuffer = fs.readFileSync("./images/photo.jpg");
const source = tinify.fromBuffer(imageBuffer);
await source.toFile("./images/compressed.jpg");

// From API response
const response = await fetch("https://api.example.com/generate-image");
const imageData = await response.arrayBuffer();
await tinify.fromBuffer(new Uint8Array(imageData))
  .convert({ type: "image/webp" })
  .toFile("./images/converted.webp");

From URL

Create a source from an image URL. The image will be downloaded by the Tinify API.

/**
 * Create a source from an image URL
 * @param url - URL of the image to process
 * @returns Source instance for chaining transformations
 * @example tinify.fromUrl("https://example.com/image.jpg")
 */
function fromUrl(url: string): Source;

Usage Examples:

const tinify = require("tinify");

tinify.key = "your-api-key";

// Process image from URL
await tinify.fromUrl("https://example.com/large-image.png")
  .toFile("./images/compressed.png");

// URL with transformations
await tinify.fromUrl("https://example.com/photo.jpg")
  .resize({ method: "cover", width: 400, height: 300 })
  .preserve("copyright", "location")
  .toFile("./images/thumbnail.jpg");

// Convert format from URL
const source = tinify.fromUrl("https://example.com/image.png")
  .convert({ type: ["image/webp", "image/avif"] });

const result = source.result();
const extension = await result.extension();
await source.toFile(`./images/converted.${extension}`);

Source Class

All factory methods return a Source instance that provides a fluent API for chaining transformations.

class Source {
  // Transformation methods (see transformations.md)
  resize(options: ResizeOptions): Source;
  convert(options: ConvertOptions): Source;
  preserve(options: string[]): Source;
  transform(options: object): Source;
  
  // Output methods (see results.md)
  result(): Result;
  toFile(path: string): Promise<void>;
  toBuffer(): Promise<Uint8Array>;
  
  // Cloud storage (see cloud-storage.md)
  store(options: StoreOptions): ResultMeta;
}

Error Handling

Source creation methods can throw the following errors:

  • ClientError: Invalid file path, unsupported image format, or malformed URL
  • ConnectionError: Network issues when downloading from URL
  • AccountError: API key issues or monthly limit exceeded
  • ServerError: Temporary API service issues

Error Handling Example:

const tinify = require("tinify");

tinify.key = "your-api-key";

try {
  const source = tinify.fromFile("./images/photo.jpg");
  await source.toFile("./images/compressed.jpg");
} catch (error) {
  if (error instanceof tinify.AccountError) {
    console.error("Account issue:", error.message);
  } else if (error instanceof tinify.ClientError) {
    console.error("Client error:", error.message);
  } else if (error instanceof tinify.ConnectionError) {
    console.error("Network error:", error.message);
  }
}

Install with Tessl CLI

npx tessl i tessl/npm-tinify

docs

cloud-storage.md

configuration.md

error-handling.md

image-sources.md

index.md

results.md

transformations.md

tile.json