image

package
v1.32.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 20 Imported by: 0

README

Image Utilities

The image package provides a set of utilities for loading, manipulating, encoding, and decoding images in various formats. Below are the main features and methods of the package:


LoadFromFile
  • LoadFromFile(path string) (*Image, error)
    Loads an image from a local file.
    • path: Path to the image file.
    • Returns an *Image object and an error if loading fails.
    • Supports formats: JPG, JPEG, PNG, GIF, BMP, TIFF, WEBP, and more.

LoadFromURL
  • LoadFromURL(url string) (*Image, error)
    Loads an image from a URL.
    • url: The image URL.
    • Returns an *Image object and an error if loading fails.
    • Supports content types: PNG, JPEG, GIF, BMP, TIFF, WEBP.

LoadFromBytes
  • LoadFromBytes(format ImageFormat, data []byte) (*Image, error)
    Loads an image from a byte slice.
    • format: The format of the image (e.g., FormatPNG, FormatJPEG).
    • data: Byte slice containing image data.
    • Returns an *Image object.

SaveToFile
  • (img *Image) SaveToFile(path string) error
    Saves an image to a local file.
    • path: Path to save the image.
    • Supports all formats supported by the Image object.

SaveToWriter
  • (img *Image) SaveToWriter(writer io.Writer) error
    Saves an image to any io.Writer.
    • writer: Destination writer.

ToBytes
  • (img *Image) ToBytes() ([]byte, error)
    Converts the image into a byte slice.
    • Returns the encoded image data.

ToBase64
  • (img *Image) ToBase64() (string, error)
    Converts the image to a base64-encoded string.
    • Returns the base64 string representation of the image.

Resize
  • (img *Image) Resize(width, height uint, interp resize.InterpolationFunction) *Image
    Returns a resized copy of the image.
    • width: Target width.
    • height: Target height.
    • interp: Interpolation function (e.g., resize.NearestNeighbor, resize.Bilinear).

ResizeToWidth
  • (img *Image) ResizeToWidth(width uint, interp resize.InterpolationFunction) *Image
    Returns a resized copy with a new width while preserving aspect ratio (height is computed).

ResizeToHeight
  • (img *Image) ResizeToHeight(height uint, interp resize.InterpolationFunction) *Image
    Returns a resized copy with a new height while preserving aspect ratio (width is computed).

ResizeSelf
  • (img *Image) ResizeSelf(width, height uint, interp resize.InterpolationFunction)
    Resizes the original image in place.

Scale
  • (img *Image) Scale(factor float64, interp resize.InterpolationFunction) (*Image, error)
    Returns a resized copy scaled by the given factor.
    • factor: Scaling factor (must be positive).

ScaleSelf
  • (img *Image) ScaleSelf(factor float64, interp resize.InterpolationFunction) error
    Scales the original image in place by the given factor.

ScaleDown
  • (img *Image) ScaleDown(maxWidth, maxHeight uint, interp resize.InterpolationFunction) (*Image, error)
    Scales down the image to fit within maxWidth and maxHeight while maintaining the aspect ratio.
    • If the image is already within the bounds, the original object is returned.

Examples

For examples of using each function, please check out EXAMPLES.md

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Image

type Image struct {
	Image  image.Image
	Format ImageFormat
	Width  uint
	Height uint

	// Metadata
	ColorModel color.Model
}

func LoadFromBytes

func LoadFromBytes(format ImageFormat, data []byte) (*Image, error)

LoadFromBytes loads an image from a raw byte slice with the specified format.

func LoadFromFile

func LoadFromFile(path string) (*Image, error)

LoadFromFile loads an image from the given file path.

func LoadFromReader

func LoadFromReader(format ImageFormat, r io.Reader) (*Image, error)

LoadFromReader loads an image from an io.Reader with the specified format.

func LoadFromURL

func LoadFromURL(url string) (*Image, error)

LoadFromURL downloads and loads an image from the given URL.

func (*Image) Resize

func (img *Image) Resize(width, height uint, interp resize.InterpolationFunction) *Image

Resize creates a new resized copy of the image using the given dimensions and interpolation. The original image remains unchanged.

func (*Image) ResizeSelf

func (img *Image) ResizeSelf(width, height uint, interp resize.InterpolationFunction)

ResizeSelf resizes the image in place to the given dimensions using the specified interpolation.

func (*Image) ResizeToHeight

func (img *Image) ResizeToHeight(height uint, interp resize.InterpolationFunction) *Image

ResizeToHeight resizes the image to the given height while preserving aspect ratio. The original image remains unchanged.

func (*Image) ResizeToWidth

func (img *Image) ResizeToWidth(width uint, interp resize.InterpolationFunction) *Image

ResizeToWidth resizes the image to the given width while preserving aspect ratio. The original image remains unchanged.

func (*Image) SaveToFile

func (img *Image) SaveToFile(path string) error

SaveToFile saves the image to a file on disk in its current format.

func (*Image) SaveToWriter

func (img *Image) SaveToWriter(writer io.Writer) error

SaveToWriter writes the image to any io.Writer in its current format.

func (*Image) Scale

func (img *Image) Scale(factor float64, interp resize.InterpolationFunction) (*Image, error)

Scale creates a new scaled copy of the image by the given factor (e.g., 0.5 for half size). The original image remains unchanged.

func (*Image) ScaleDown

func (img *Image) ScaleDown(maxWidth, maxHeight uint, interp resize.InterpolationFunction) (*Image, error)

ScaleDown creates a new image scaled down to fit within the given max width and height, preserving aspect ratio. The original image remains unchanged.

func (*Image) ScaleSelf

func (img *Image) ScaleSelf(factor float64, interp resize.InterpolationFunction) error

ScaleSelf scales the image in place by the given factor (e.g., 2.0 for double size).

func (*Image) ToBase64

func (img *Image) ToBase64() (string, error)

ToBase64 encodes the image as a base64 string in its current format.

func (*Image) ToBytes

func (img *Image) ToBytes() ([]byte, error)

ToBytes encodes the image into a byte slice in its current format.

type ImageFormat

type ImageFormat string
const (
	FormatJPG     ImageFormat = "jpg"
	FormatJFIF    ImageFormat = "jfif"
	FormatJPE     ImageFormat = "jpe"
	FormatJPEG    ImageFormat = "jpeg"
	FormatPNG     ImageFormat = "png"
	FormatGIF     ImageFormat = "gif"
	FormatTIF     ImageFormat = "tif"
	FormatTIFF    ImageFormat = "tiff"
	FormatTIFF_FX ImageFormat = "tiff-fx"
	FormatWEBP    ImageFormat = "webp"
	FormatBMP     ImageFormat = "bmp"
	FormatDIB     ImageFormat = "dib"
	FormatXBMP    ImageFormat = "x-bmp"
)

Jump to

Keyboard shortcuts

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