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 ¶
- func ImageRescale(inputPath, outputPath string, desiredWidth, desiredHeight int) error
- func ImageRescaleAutofill(inputPath, outputPath string, forcedWidth, desiredHeight int) error
- func IsLandscape(imagePath string) (bool, error)
- func IsPortrait(imagePath string) (bool, error)
- type ImageInfo
- type ImageOrientation
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ImageRescale ¶
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 ¶
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:
- Opens and decodes the input image
- Uses the top-left pixel as the background color
- Resizes the image to the desired height while maintaining aspect ratio
- Creates a new image with the forced width and desired height
- Fills the new image with the background color
- Pastes the resized image centered in the new image
- Saves the result in the original format
Supported Formats:
- JPEG (.jpg, .jpeg)
- PNG (.png)
func IsLandscape ¶
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 ¶
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 )