media

package
v0.0.78 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package media provides utilities for image processing and manipulation. It includes functions for resizing images, checking image orientation, and handling EXIF metadata.

Features:

  • Image rescaling with aspect ratio preservation
  • Image rescaling with auto-fill background
  • EXIF orientation detection
  • Portrait/landscape orientation checking
  • Support for JPEG and PNG formats
  • High-quality resizing using Lanczos3 algorithm

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ImageRescale

func ImageRescale(inputPath, outputPath string, desiredWidth, desiredHeight int) error

ImageRescale resizes an image to the desired dimensions. This function supports maintaining aspect ratio by setting either width or height to 0. If both dimensions are provided, the image may be distorted to fit exactly.

Parameters:

  • inputPath: Path to the input image file
  • outputPath: Path to save the resized image
  • desiredWidth: Target width in pixels (0 to maintain aspect ratio based on height)
  • desiredHeight: Target height in pixels (0 to maintain aspect ratio based on width)

Returns:

  • error: If any step in the process fails (file operations, decoding, resizing, encoding)

Supported Formats:

  • JPEG (.jpg, .jpeg)
  • PNG (.png)

Resizing Method:

  • Uses Lanczos3 algorithm for high-quality resizing
  • Maintains original image format in the output

func ImageRescaleAutofill

func ImageRescaleAutofill(inputPath, outputPath string, forcedWidth, desiredHeight int) error

ImageRescaleAutofill resizes an image to fit within specified dimensions while maintaining aspect ratio and filling empty space with the background color. This function is useful for creating thumbnails or standardized images where you want to maintain the original aspect ratio but need a specific output size.

Parameters:

  • inputPath: Path to the input image file
  • outputPath: Path to save the processed image
  • forcedWidth: The exact width of the output image
  • desiredHeight: The height to resize the image to (maintaining aspect ratio)

Returns:

  • error: If any step in the process fails

Process:

  1. Opens and decodes the input image
  2. Uses the top-left pixel as the background color
  3. Resizes the image to the desired height while maintaining aspect ratio
  4. Creates a new image with the forced width and desired height
  5. Fills the new image with the background color
  6. Pastes the resized image centered in the new image
  7. Saves the result in the original format

Supported Formats:

  • JPEG (.jpg, .jpeg)
  • PNG (.png)

func IsLandscape

func IsLandscape(imagePath string) (bool, error)

IsLandscape checks if an image is in landscape orientation. This function uses EXIF data if available to determine the true orientation.

Parameters:

  • imagePath: Path to the image file to check

Returns:

  • bool: True if the image is in landscape orientation
  • error: If the image cannot be read or processed

func IsPortrait

func IsPortrait(imagePath string) (bool, error)

IsPortrait checks if an image is in portrait orientation. This function uses EXIF data if available to determine the true orientation.

Parameters:

  • imagePath: Path to the image file to check

Returns:

  • bool: True if the image is in portrait orientation
  • error: If the image cannot be read or processed

Types

type ImageInfo

type ImageInfo struct {
	Width           int              // Image width in pixels
	Height          int              // Image height in pixels
	Orientation     ImageOrientation // Image orientation (portrait, landscape, square)
	EXIFOrientation int              // EXIF orientation tag value
	Format          string           // Image format (e.g., "jpg", "png")
}

ImageInfo contains metadata about an image including dimensions, orientation, and format. Used to store information about an image for processing decisions.

type ImageOrientation

type ImageOrientation int

ImageOrientation represents the orientation of an image. Used to determine if an image is in portrait, landscape, or square format.

const (
	// OrientationUnknown represents an image with unknown orientation
	OrientationUnknown ImageOrientation = iota

	// OrientationPortrait represents an image in portrait orientation (height > width)
	OrientationPortrait

	// OrientationLandscape represents an image in landscape orientation (width > height)
	OrientationLandscape

	// OrientationSquare represents a square image (width = height)
	OrientationSquare
)

Jump to

Keyboard shortcuts

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