Documentation
¶
Index ¶
- func ApplyGrid(inputImg image.Image, watermarkImg image.Image, config GridConfig) (image.Image, error)
- func ApplySingle(inputImg image.Image, watermarkImg image.Image, config SingleConfig) (image.Image, error)
- func BatchApplyGrid(inputImgs []image.Image, watermarkImg image.Image, config GridConfig) ([]image.Image, error)
- func BatchApplySingle(inputImgs []image.Image, watermarkImg image.Image, config SingleConfig) ([]image.Image, error)
- func OpenImage(path string) (image.Image, error)
- type GeneralConfig
- type GridConfig
- type HorizontalAlign
- type SingleConfig
- type VerticalAlign
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyGrid ¶
func ApplyGrid( inputImg image.Image, watermarkImg image.Image, config GridConfig, ) (image.Image, error)
ApplyGrid applies a grid pattern of watermarks to an input image based on the provided configuration.
The function performs the following steps:
- Validates the GridConfig.
- Preprocesses the watermark (resize, rotate, and apply opacity).
- Generates grid positions based on spacing and offset settings.
- Applies the watermark at each grid position.
- Returns the final image with the grid watermarks applied.
The parallel image loading uses goroutines to improve performance for I/O operations. If the offset or spacing values are negative, the grid pattern will be shifted or compressed accordingly.
Parameters:
- inputImg: The input image to which the grid watermark will be applied.
- watermarkImg: The watermark image to overlay on the input image.
- config: GridConfig struct containing spacing, offset, and watermark appearance settings.
Returns:
- An image.Image containing the final image with the grid watermark pattern applied.
- An error if configuration is invalid or image loading fails.
Example:
config := GridConfig{
GeneralConfig: GeneralConfig{
WatermarkWidthPercent: 20,
OpacityAlpha: 0.5,
RotationDegrees: 45,
},
GridSpacingX: 75,
GridSpacingY: 75,
OffsetX: -50,
OffsetY: -50,
}
result, err := ApplyGrid(config)
if err != nil {
log.Fatal(err)
}
func ApplySingle ¶
func ApplySingle( inputImg image.Image, watermarkImg image.Image, config SingleConfig, ) (image.Image, error)
ApplySingle overlays a single watermark onto an input image at a specific position based on the provided configuration.
The function performs the following steps:
- Validates the SingleConfig to ensure all settings are valid.
- Preprocesses the watermark (resize, rotate, and apply opacity).
- Calculates the watermark position based on vertical/horizontal alignment and spacing.
- Creates an RGBA copy of the input image.
- Overlays the watermark onto the input image using the "Over" compositing operator.
Parameters:
- inputImg: The input image to which the watermark will be applied.
- watermarkImg: The watermark image to overlay on the input image.
- config: SingleConfig struct containing opacity, size, alignment, and rotation settings.
Returns:
- An image.Image containing the final image with the watermark applied.
- An error if any step fails, such as invalid configuration or failure to load images.
Example:
config := SingleConfig{
GeneralConfig: GeneralConfig{
WatermarkWidthPercent: 20,
OpacityAlpha: 0.6,
RotationDegrees: 0,
},
VerticalAlign: VerticalBottom,
HorizontalAlign: HorizontalRight,
Spacing: 10,
}
result, err := ApplySingle(inputImg, watermarkImg, config)
if err != nil {
log.Fatal(err)
}
func BatchApplyGrid ¶
func BatchApplySingle ¶
func BatchApplySingle( inputImgs []image.Image, watermarkImg image.Image, config SingleConfig, ) ([]image.Image, error)
BatchApplySingle applies a single watermark to a batch of input images concurrently based on the provided configuration.
This function performs the following steps:
- Validates the SingleConfig to ensure all settings are valid.
- Preprocesses the watermark (rotate and apply opacity) once for efficiency.
- Uses a worker pool to process multiple images concurrently, applying the watermark to each image.
- Calculates the watermark position for each image based on alignment and spacing settings.
- Returns a slice of images with the watermark applied.
Parameters:
- inputImg: A slice of input images to which the watermark will be applied.
- watermarkImg: The watermark image to overlay on each input image.
- config: SingleConfig struct containing opacity, size, alignment, rotation, and concurrency settings.
Returns:
- A slice of image.Image objects containing the final images with the watermark applied.
- An error if any step fails, such as invalid configuration.
Example:
cfg := SingleConfig{
GeneralConfig: GeneralConfig{
WatermarkWidthPercent: 20,
OpacityAlpha: 0.6,
RotationDegrees: 0,
},
VerticalAlign: VerticalBottom,
HorizontalAlign: HorizontalRight,
Spacing: 10,
MaxWorkers: 4,
}
results, err := BatchApplySingle(inputImages, watermarkImg, cfg)
if err != nil {
log.Fatal(err)
}
func OpenImage ¶
OpenImage loads an image from the specified path with auto-orientation support.
The function automatically corrects the image orientation based on EXIF metadata, ensuring that images taken with different device orientations display correctly.
Parameters:
- path: The file path to the image to be loaded.
Returns:
- An image.Image containing the loaded image data.
- An error if the file cannot be read or the format is not supported.
Types ¶
type GeneralConfig ¶
type GeneralConfig struct {
OpacityAlpha float64
WatermarkWidthPercent float64
RotationDegrees float64
ResampleFilter imaging.ResampleFilter
MaxWorkers int
}
GeneralConfig holds common configuration settings for all watermarking operations.
This struct contains the paths to the input image and watermark, along with general appearance settings that apply to any watermarking method (single or grid).
Fields:
- OpacityAlpha: Transparency level of the watermark (0.0 to 1.0, where 1.0 is fully opaque).
- WatermarkWidthPercent: Desired watermark width as a percentage of the input image width (0-100).
- RotationDegrees: Rotation angle for the watermark in degrees (0-360).
- ResampleFilter: Resampling filter to use when resizing the watermark. (Default is CatmullRom)
- MaxWorkers: Maximum number of concurrent workers for batch processing (Default is number of CPU cores).
type GridConfig ¶
type GridConfig struct {
GeneralConfig
GridSpacingX int
GridSpacingY int
OffsetX int
OffsetY int
}
GridConfig holds all configuration settings for applying a grid pattern of watermarks to an image.
This struct extends GeneralConfig with grid-specific options for spacing and positioning. The grid pattern can be controlled using positive values for regular grids, or negative values for overlapping or inverted patterns.
Fields:
- GeneralConfig: Embedded struct containing common watermarking settings.
- GridSpacingX: Horizontal spacing between watermarks in the grid (in pixels). Can be negative for overlapping.
- GridSpacingY: Vertical spacing between watermarks in the grid (in pixels). Can be negative for overlapping.
- OffsetX: Initial horizontal offset for the grid starting position (in pixels).
- OffsetY: Initial vertical offset for the grid starting position (in pixels).
type HorizontalAlign ¶
type HorizontalAlign int
HorizontalAlign defines the horizontal alignment options for watermark placement.
Supported values:
- HorizontalLeft: Aligns the watermark to the left of the image.
- HorizontalMiddle: Centers the watermark horizontally.
- HorizontalRight: Aligns the watermark to the right of the image.
- HorizontalRandom: Places the watermark at a random horizontal position.
const ( HorizontalLeft HorizontalAlign = iota HorizontalMiddle HorizontalRight HorizontalRandom )
type SingleConfig ¶
type SingleConfig struct {
GeneralConfig
VerticalAlign VerticalAlign
HorizontalAlign HorizontalAlign
Spacing int
}
SingleConfig holds all configuration settings for applying a single watermark to an image.
This struct extends GeneralConfig with alignment and spacing options specific to single watermark placement.
Fields:
- GeneralConfig: Embedded struct containing common watermarking settings.
- VerticalAlign: Vertical alignment of the watermark (top, middle, bottom, or random).
- HorizontalAlign: Horizontal alignment of the watermark (left, middle, right, or random).
- Spacing: Distance in pixels between the watermark and the aligned edge.
type VerticalAlign ¶
type VerticalAlign int
VerticalAlign defines the vertical alignment options for watermark placement.
Supported values:
- VerticalTop: Aligns the watermark to the top of the image.
- VerticalMiddle: Centers the watermark vertically.
- VerticalBottom: Aligns the watermark to the bottom of the image.
- VerticalRandom: Places the watermark at a random vertical position.
const ( VerticalTop VerticalAlign = iota VerticalMiddle VerticalBottom VerticalRandom )