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(path string) (*Image, error)
Loads an image from a local file.path: Path to the image file.- Returns an
*Imageobject and an error if loading fails. - Supports formats: JPG, JPEG, PNG, GIF, BMP, TIFF, WEBP, and more.
LoadFromURL(url string) (*Image, error)
Loads an image from a URL.url: The image URL.- Returns an
*Imageobject and an error if loading fails. - Supports content types: PNG, JPEG, GIF, BMP, TIFF, WEBP.
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
*Imageobject.
(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
Imageobject.
(img *Image) SaveToWriter(writer io.Writer) error
Saves an image to anyio.Writer.writer: Destination writer.
(img *Image) ToBytes() ([]byte, error)
Converts the image into a byte slice.- Returns the encoded image data.
(img *Image) ToBase64() (string, error)
Converts the image to a base64-encoded string.- Returns the base64 string representation of the image.
(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).
(img *Image) ResizeToWidth(width uint, interp resize.InterpolationFunction) *Image
Returns a resized copy with a new width while preserving aspect ratio (height is computed).
(img *Image) ResizeToHeight(height uint, interp resize.InterpolationFunction) *Image
Returns a resized copy with a new height while preserving aspect ratio (width is computed).
(img *Image) ResizeSelf(width, height uint, interp resize.InterpolationFunction)
Resizes the original image in place.
(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).
(img *Image) ScaleSelf(factor float64, interp resize.InterpolationFunction) error
Scales the original image in place by the given factor.
(img *Image) ScaleDown(maxWidth, maxHeight uint, interp resize.InterpolationFunction) (*Image, error)
Scales down the image to fit withinmaxWidthandmaxHeightwhile maintaining the aspect ratio.- If the image is already within the bounds, the original object is returned.
For examples of using each function, please check out EXAMPLES.md