img

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2026 License: MIT Imports: 14 Imported by: 0

README

img

Functions for optimizing, resizing, and converting images using libvips.

License

Table of Contents

Features

  • Parallel processing for batch operations
  • Automatic format detection based on file content
  • Preserves original if optimization does not reduce file size
  • Stripped metadata
  • Compress images with format-specific settings tuned for web

Prerequisites

  • Go 1.25 or later
  • libvips

Arch Linux:

sudo pacman -S libvips

macOS:

brew install vips

Ubuntu/Debian:

sudo apt install libvips-dev

See libvips install instructions for other platforms.

Installation

go get github.com/andmitr/pkg/img

Usage

func Optimize
func Optimize(file string, outDir string) error

Optimize compresses a single image and saves it as filename_min.ext in outDir.

Parameters:

  • file - path to input image
  • outDir - directory for output file

Returns error if file cannot be read or processed.

Example
err := img.Optimize("inputDir/photo.jpg", "outputDir")
// Creates: outputDir/photo_min.jpg

The function applies format-specific compression:

  • JPEG: quality 70, interlaced
  • PNG: compression 6, palette mode, quality 85
  • WebP: quality 80
  • AVIF: quality 70, speed 7
  • SVG: minified via tdewolff/minify

Note
Compression options are hardcoded for optimal web performance. If you need custom settings, fork the repository and modify the options in optimize.go.

func OptimizeAll
func OptimizeAll(inDir string, outDir string) error

OptimizeAll processes all supported images in inDir and saves optimized versions to outDir.

Processes files in parallel using all available CPU cores. Skips files with unsupported extensions.

Example
err := img.OptimizeAll("inputDir", "outputDir")
// Processes all supported images in parallel
// Creates: outputDir/photo1_min.jpg, outputDir/photo2_min.png, etc.
func Resize
func Resize(file string, outDir string, width int, height int) error

Resize creates a new image stretched to the specified dimensions.

Parameters:

  • file - path to input image
  • outDir - directory for output file
  • width - target width in pixels
  • height - target height in pixels
Example
err := img.Resize("input/photo.jpg", "output", 800, 600)
// Creates: output/photo_800x600.jpg
// Stretches the image to exactly 800x600 pixels.
func Convert
func Convert(file string, outDir string, outType string) error

Convert transforms an image to a different format.

Parameters:

  • file - path to input image
  • outDir - directory for output file
  • outType - target format: jpg, jpeg, png, webp, avif
Example
err := img.Convert("input/photo.png", "output", "webp")
// Creates: output/photo.webp

Supported Formats

Format Optimize Resize Convert
JPEG Yes Yes Yes
PNG Yes Yes Yes
WebP Yes Yes Yes
AVIF Yes Yes Yes
SVG Yes No No

Performance Notes

  • OptimizeAll uses goroutine pooling with a limit of runtime.NumCPU() to prevent excessive memory usage
  • Processing order is non-deterministic due to parallel execution
  • AVIF encoding is slower but provides excellent compression
  • SVG optimization uses a different pipeline (minify) rather than libvips

Sponsorship

BoostySupport

Bitcoin

1CCnwAvJYEoDVGM7vsBg2Q99cF9EHtBVaY

Tether

0x54f0ccc6b2987de454f69f2814fc9202bcfb74fe

License

MIT Licensed. See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(file, outDir, outType string) error

Convert converts the source image to the specified format.

Supported outType values: jpg, jpeg, png, avif, webp.

func Optimize

func Optimize(file string, outDir string) error

Optimize creates a new compressed image named file_min.ext in outDir.

Parameters are optimized for web and provide a balance between output image quality and processing speed.

Supported formats: jpg, jpeg, png, avif, webp, svg.

func OptimizeAll

func OptimizeAll(inDir string, outDir string) error

OptimizeAll optimizes all images in inDir and saves them to outDir.

Applies the Optimize function to each image.

func Resize

func Resize(file, outDir string, width, height int) error

Resize creates a new image with the specified width and height.

Stretches the image to the exact dimensions without preserving aspect ratio.

Supported formats: jpg, jpeg, png, avif, webp.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL