Documentation
¶
Index ¶
- Variables
- func Circle(img image.Image) (image.Image, error)
- func ContextProcess(img image.Image, processors []ContextProcessor) (image.Image, error)
- func DrawLine(img *image.RGBA, x0, y0, x1, y1 int, c color.RGBA)
- func GenCaptchaImage(captcha string) (*bytes.Buffer, error)
- func GenCaptchaImageWithConfig(captcha string, config *CaptchaConfig) (*bytes.Buffer, error)
- func GenMultipleColumnsTableImage(font *truetype.Font, headers []string, data [][]string) (*bytes.Buffer, error)
- func GenMultipleRowsTableImage(font *truetype.Font, headers []string, data [][]string, widths []float64) (*bytes.Buffer, error)
- func GetDefaultFont() (*truetype.Font, error)
- func LoadFont(font **truetype.Font, localPath, downloadUrl string) *truetype.Font
- func LoadHarmonyOSSansSCBlack() *truetype.Font
- func LoadNotoSansSCVariableFontWght() *truetype.Font
- func MosaicImage(img []byte, regions []*MosaicRegion) ([]byte, error)
- func MosaicImageSingle(img []byte, fromX, fromY, toX, toY int) ([]byte, error)
- func MosaicImageSingleWithOptions(img []byte, fromX, fromY, toX, toY int, mosaicPercent float32, ...) ([]byte, error)
- func MosaicImageWithOptions(img []byte, regions []*MosaicRegion, mosaicPercent float32, ...) ([]byte, error)
- func Process(img image.Image, processors []Processor) (image.Image, error)
- func ProcessImage(imgData []byte, processors []Processor, options *ProcessorOptions) ([]byte, error)
- func SetDefaultFont(font *truetype.Font)
- type CaptchaConfig
- type CompressProcessor
- func (p *CompressProcessor) EstimateFileSize(img image.Image) (int64, error)
- func (p *CompressProcessor) GetStrategyName() string
- func (p *CompressProcessor) Process(img image.Image) (image.Image, error)
- func (p *CompressProcessor) ProcessFile(input []byte) ([]byte, error)
- func (p *CompressProcessor) SetFormat(format string) error
- func (p *CompressProcessor) SetStrategy(strategy *CompressionStrategy)
- func (p *CompressProcessor) WithScaler(scaler draw.Scaler) *CompressProcessor
- type CompressionStrategy
- type ContextProcessor
- type CutCircleProcessor
- type CutPosition
- type CutProcessor
- func NewCutProcessor(width, height int, position CutPosition) *CutProcessor
- func NewCutProcessorWithRegion(width, height, x, y int) *CutProcessor
- func NewCutSquareProcessor(position string) *CutProcessor
- func NewCutSquareProcessorWithRegion(size, x, y int) *CutProcessor
- func NewCutSquareProcessorWithSize(size int, position string) *CutProcessor
- func NewSquareCutProcessor(size int, position CutPosition) *CutProcessor
- type CutSquareProcessor
- type Direction
- type DrawCircleProcessor
- type DrawRectProcessor
- type EmptyProcessor
- type ImageProcessContext
- type MosaicProcessor
- type MosaicRegion
- type NoiseProcessor
- type OverlayProcessor
- type Processor
- type ProcessorOptions
- type RotateProcessor
- type RoundedCornerProcessor
- type TextOptions
- type TextProcessor
- type WatermarkProcessor
- type ZoomMode
- type ZoomProcessor
- func NewZoomHeightProcessor(height int) *ZoomProcessor
- func NewZoomMaxProcessor(size int) *ZoomProcessor
- func NewZoomMinProcessor(size int) *ZoomProcessor
- func NewZoomProcessor(width, height int) *ZoomProcessor
- func NewZoomRatioProcessor(ratio float64) *ZoomProcessor
- func NewZoomWidthProcessor(width int) *ZoomProcessor
Constants ¶
This section is empty.
Variables ¶
var ( CompressStrategyDefault = &CompressionStrategy{ Name: "Default Strategy", Description: "General compression suitable for most scenarios", MinPixelRatio: 0.3, MaxPixelRatio: 1.0, PreferredRatio: 0.7, QualityLevels: []int{95, 90, 85, 80, 75}, DefaultQuality: 85, PreferredFormats: []string{"jpeg", "webp", "png"}, DefaultFormat: "jpeg", MaxIterations: 3, Tolerance: 0.1, } CompressStrategyHighQuality = &CompressionStrategy{ Name: "High Quality", Description: "Prioritize visual quality, file size is secondary", MinPixelRatio: 0.8, MaxPixelRatio: 1.0, PreferredRatio: 0.9, QualityLevels: []int{98, 95, 92, 90}, DefaultQuality: 95, PreferredFormats: []string{"png", "webp", "jpeg"}, DefaultFormat: "png", MaxIterations: 2, Tolerance: 0.2, } CompressStrategySmallSize = &CompressionStrategy{ Name: "Small Size", Description: "Minimize file size, quality loss is acceptable", MinPixelRatio: 0.2, MaxPixelRatio: 0.6, PreferredRatio: 0.4, QualityLevels: []int{85, 75, 65, 60, 55}, DefaultQuality: 70, PreferredFormats: []string{"webp", "jpeg"}, DefaultFormat: "webp", MaxIterations: 5, Tolerance: 0.05, } CompressStrategyBalanced = &CompressionStrategy{ Name: "Balanced Strategy", Description: "Balance between quality and file size", MinPixelRatio: 0.4, MaxPixelRatio: 0.8, PreferredRatio: 0.6, QualityLevels: []int{90, 85, 80, 75, 70}, DefaultQuality: 80, PreferredFormats: []string{"jpeg", "webp"}, DefaultFormat: "jpeg", MaxIterations: 3, Tolerance: 0.08, } CompressStrategyWebOptimized = &CompressionStrategy{ Name: "Web Optimized", Description: "Optimized for web loading speed", MinPixelRatio: 0.5, MaxPixelRatio: 0.9, PreferredRatio: 0.7, QualityLevels: []int{85, 80, 75, 70}, DefaultQuality: 80, PreferredFormats: []string{"webp", "jpeg"}, DefaultFormat: "webp", MaxIterations: 2, Tolerance: 0.1, } CompressStrategyThumbnail = &CompressionStrategy{ Name: "Thumbnail Strategy", Description: "Generate high quality thumbnails", MinPixelRatio: 0.1, MaxPixelRatio: 0.3, PreferredRatio: 0.2, QualityLevels: []int{90, 85, 80}, DefaultQuality: 85, PreferredFormats: []string{"jpeg", "webp"}, DefaultFormat: "jpeg", MaxIterations: 1, Tolerance: 0.15, } )
Predefined strategies
var DefaultCaptchaConfig = &CaptchaConfig{ Width: 120, Height: 40, BgColor: color.RGBA{R: 255, G: 255, B: 255, A: 255}, TextColor: color.RGBA{R: 0, G: 0, B: 0, A: 255}, NoiseLines: 5, NoiseDots: 50, Face: basicfont.Face7x13, CharSpacing: 18, CharWidth: 16, CharYOffsetRange: 8, CharXOffsetRange: 6, }
DefaultCaptchaConfig 默认验证码配置
var DefaultProcessorOptions = ProcessorOptions{
Quality: 90,
}
DefaultProcessorOptions 默认处理器选项
var DefaultTextOptions = TextOptions{ Font: basicfont.Face7x13, Color: color.Black, MaxWidth: 0, LineSpacing: 1.5, Align: gg.AlignLeft, CharWrap: false, }
DefaultTextOptions 默认文本选项
Functions ¶
func Circle ¶
Circle crops the image into a circle, making pixels outside the circle transparent If the image is not square, returns an error
func ContextProcess ¶
ContextProcess 上下文处理
func GenCaptchaImage ¶
GenCaptchaImage 生成验证码图片 captcha: 验证码文本 返回: 图片字节缓冲区和错误信息
func GenCaptchaImageWithConfig ¶
func GenCaptchaImageWithConfig(captcha string, config *CaptchaConfig) (*bytes.Buffer, error)
GenCaptchaImageWithConfig 使用自定义配置生成验证码图片
func GenMultipleColumnsTableImage ¶
func GenMultipleColumnsTableImage(font *truetype.Font, headers []string, data [][]string) (*bytes.Buffer, error)
GenMultipleColumnsTableImage 根据数据生成列式表格图片 headers 为标题,作为最左侧的一列显示 data 为数据内容,每一条数据作为一列展示 返回PNG格式的图片数据
func GenMultipleRowsTableImage ¶
func GenMultipleRowsTableImage(font *truetype.Font, headers []string, data [][]string, widths []float64) (*bytes.Buffer, error)
GenMultipleRowsTableImage 根据数据生成表格图片 headers 为标题, data 为数据内容 widths 为每列宽度,如果为空则使用默认宽度 返回PNG格式的图片数据
func GetDefaultFont ¶
func MosaicImage ¶
func MosaicImage(img []byte, regions []*MosaicRegion) ([]byte, error)
MosaicImage 对图片指定区域添加马赛克效果 (向后兼容版本) img: 原始图片字节数据 regions: 需要添加马赛克的区域列表 返回: 处理后的图片字节数据和错误信息
func MosaicImageSingle ¶
MosaicImageSingle 对图片单个区域添加马赛克效果 (向后兼容版本)
func MosaicImageSingleWithOptions ¶
func MosaicImageSingleWithOptions(img []byte, fromX, fromY, toX, toY int, mosaicPercent float32, startDirection Direction) ([]byte, error)
MosaicImageSingleWithOptions 对图片单个区域添加马赛克效果,支持指定百分比和方向
func MosaicImageWithOptions ¶
func MosaicImageWithOptions(img []byte, regions []*MosaicRegion, mosaicPercent float32, startDirection Direction) ([]byte, error)
MosaicImageWithOptions 对图片指定区域添加马赛克效果,支持指定百分比和方向 img: 原始图片字节数据 regions: 需要添加马赛克的区域列表 mosaicPercent: 马赛克区域百分比,范围0-1,表示要处理的区域比例 startDirection: 开始马赛克的方向(left, right, top, bottom) 返回: 处理后的图片字节数据和错误信息
func ProcessImage ¶
func ProcessImage(imgData []byte, processors []Processor, options *ProcessorOptions) ([]byte, error)
ProcessImage 使用处理器链处理图片 imgData: 原始图片字节数据 processors: 处理器链 options: 处理选项 返回: 处理后的图片字节数据和错误信息
func SetDefaultFont ¶
Types ¶
type CaptchaConfig ¶
type CaptchaConfig struct {
Width int // 图片宽度
Height int // 图片高度
BgColor color.RGBA // 背景颜色
TextColor color.RGBA // 文字颜色
NoiseLines int // 干扰线数量
NoiseDots int // 干扰点数量
Face font.Face // 字体
CharSpacing int // 字符间距
CharWidth int // 字符宽度
CharYOffsetRange int // 垂直随机偏移范围
CharXOffsetRange int // 水平随机偏移范围
}
CaptchaConfig 验证码配置
type CompressProcessor ¶
type CompressProcessor struct {
// Basic configuration
TargetFileSize int64
Strategy *CompressionStrategy
Format string
// contains filtered or unexported fields
}
CompressProcessor smart compression processor
func NewCompressProcessor ¶
func NewCompressProcessor(targetFileSize int64, strategy *CompressionStrategy) *CompressProcessor
NewCompressProcessor creates a new compression processor
func (*CompressProcessor) EstimateFileSize ¶
func (p *CompressProcessor) EstimateFileSize(img image.Image) (int64, error)
EstimateFileSize estimates compressed file size (for testing)
func (*CompressProcessor) GetStrategyName ¶
func (p *CompressProcessor) GetStrategyName() string
GetStrategyName gets current strategy name
func (*CompressProcessor) ProcessFile ¶
func (p *CompressProcessor) ProcessFile(input []byte) ([]byte, error)
ProcessFile processes image file bytes
func (*CompressProcessor) SetFormat ¶
func (p *CompressProcessor) SetFormat(format string) error
SetFormat sets the output format
func (*CompressProcessor) SetStrategy ¶
func (p *CompressProcessor) SetStrategy(strategy *CompressionStrategy)
SetStrategy sets the compression strategy
func (*CompressProcessor) WithScaler ¶
func (p *CompressProcessor) WithScaler(scaler draw.Scaler) *CompressProcessor
WithScaler sets the scaling algorithm
type CompressionStrategy ¶
type CompressionStrategy struct {
Name string // Strategy name
Description string // Strategy description
MinPixelRatio float64 // Minimum pixel ratio
MaxPixelRatio float64 // Maximum pixel ratio
PreferredRatio float64 // Preferred pixel ratio
QualityLevels []int // Quality levels options
DefaultQuality int // Default quality
PreferredFormats []string // Preferred formats
DefaultFormat string // Default format
MaxIterations int // Maximum iterations
Tolerance float64 // Tolerance range
}
CompressionStrategy configures the compression strategy
type ContextProcessor ¶
type ContextProcessor interface {
ContextProcess(ctx *ImageProcessContext) error
}
type CutCircleProcessor ¶
type CutCircleProcessor struct{}
CutCircleProcessor implements the Processor interface for circular image cropping
func NewCutCircleProcessor ¶
func NewCutCircleProcessor() *CutCircleProcessor
type CutPosition ¶
type CutPosition string
CutPosition 定义切割位置
const ( // CutPositionCenter 居中切割 CutPositionCenter CutPosition = "center" // CutPositionTop 从顶部切割 CutPositionTop CutPosition = "top" // CutPositionBottom 从底部切割 CutPositionBottom CutPosition = "bottom" // CutPositionLeft 从左侧切割 CutPositionLeft CutPosition = "left" // CutPositionRight 从右侧切割 CutPositionRight CutPosition = "right" )
type CutProcessor ¶
type CutProcessor struct {
// 目标宽度和高度(0表示自动检测)
Width int
Height int
// 切割位置
Position CutPosition
// 自定义切割区域(如果指定了,则忽略Position)
X int // 左上角X坐标
Y int // 左上角Y坐标
// 是否使用自定义区域
UseCustomRegion bool
// 是否为正方形模式(自动使用较小边)
SquareMode bool
}
CutProcessor 图像切割处理器 从原始图像中切割出指定区域(支持矩形和正方形)
func NewCutProcessor ¶
func NewCutProcessor(width, height int, position CutPosition) *CutProcessor
NewCutProcessor 创建新的矩形切割处理器(使用预定义位置)
func NewCutProcessorWithRegion ¶
func NewCutProcessorWithRegion(width, height, x, y int) *CutProcessor
NewCutProcessorWithRegion 创建新的切割处理器(使用自定义区域)
func NewCutSquareProcessor ¶
func NewCutSquareProcessor(position string) *CutProcessor
NewCutSquareProcessor 创建正方形切割处理器(自动使用较小边)
func NewCutSquareProcessorWithRegion ¶
func NewCutSquareProcessorWithRegion(size, x, y int) *CutProcessor
NewCutSquareProcessorWithRegion 创建使用自定义坐标的正方形切割处理器
func NewCutSquareProcessorWithSize ¶
func NewCutSquareProcessorWithSize(size int, position string) *CutProcessor
NewCutSquareProcessorWithSize 创建指定尺寸的正方形切割处理器
func NewSquareCutProcessor ¶
func NewSquareCutProcessor(size int, position CutPosition) *CutProcessor
NewSquareCutProcessor 创建正方形切割处理器
type CutSquareProcessor ¶
type CutSquareProcessor = CutProcessor
Type alias for backward compatibility
type DrawCircleProcessor ¶
type DrawCircleProcessor struct {
X, Y, Radius int
Color color.Color
Fill bool // If true, fill the circle with the same color as the border
}
DrawCircleProcessor draws a circle on the image using fogleman/gg.
func NewDrawCircleProcessor ¶
func NewDrawCircleProcessor(x, y, radius int, c color.Color, fill bool) *DrawCircleProcessor
NewDrawCircleProcessor creates a new DrawCircleProcessor. If fill is true, the circle will be filled with the same color as the border.
func (*DrawCircleProcessor) ContextProcess ¶
func (p *DrawCircleProcessor) ContextProcess(ctx *ImageProcessContext) error
ContextProcess draws a circle on the image using the gg.Context.
type DrawRectProcessor ¶
type DrawRectProcessor struct {
Rect image.Rectangle
Color color.Color // Border/stroke color
FillColor *color.Color // Optional fill color (nil = no fill, use Color for both)
Fill bool // If true, fill the rectangle
}
DrawRectProcessor draws a rectangle on the image using fogleman/gg.
func NewDrawRectProcessor ¶
NewDrawRectProcessor creates a new DrawRectProcessor. If fill is true, the rectangle will be filled with the same color as the border.
func NewDrawRectProcessorWithFillColor ¶
func NewDrawRectProcessorWithFillColor(rect image.Rectangle, borderColor, fillColor color.Color) *DrawRectProcessor
NewDrawRectProcessorWithFillColor creates a DrawRectProcessor with separate border and fill colors. The rectangle will be filled with fillColor and stroked with borderColor.
func (*DrawRectProcessor) ContextProcess ¶
func (p *DrawRectProcessor) ContextProcess(ctx *ImageProcessContext) error
ContextProcess draws a rectangle on the image using the gg.Context.
type EmptyProcessor ¶
type EmptyProcessor struct{}
type ImageProcessContext ¶
func NewImageProcessContext ¶
func NewImageProcessContext(img image.Image) *ImageProcessContext
func (*ImageProcessContext) DC ¶
func (ctx *ImageProcessContext) DC() *gg.Context
type MosaicProcessor ¶
type MosaicProcessor struct {
Regions []*MosaicRegion // 马赛克区域
MosaicPercent float32 // 马赛克区域百分比 (0-1)
StartDirection Direction // 开始方向
}
MosaicProcessor 马赛克处理器
func NewMosaicProcessor ¶
func NewMosaicProcessor(regions []*MosaicRegion, mosaicPercent float32, startDirection Direction) *MosaicProcessor
NewMosaicProcessor 创建新的马赛克处理器
type MosaicRegion ¶
type MosaicRegion struct {
FromX int // 区域左上角X坐标
FromY int // 区域左上角Y坐标
ToX int // 区域右下角X坐标
ToY int // 区域右下角Y坐标
}
MosaicRegion 表示一个需要添加马赛克的区域
type NoiseProcessor ¶
type NoiseProcessor struct {
NoiseLines int // 干扰线数量
NoiseDots int // 干扰点数量
LineColor color.RGBA // 线条颜色
DotColor color.RGBA // 点颜色
}
NoiseProcessor 噪点处理器
func NewNoiseProcessor ¶
func NewNoiseProcessor(noiseLines, noiseDots int, lineColor, dotColor color.RGBA) *NoiseProcessor
NewNoiseProcessor 创建新的噪点处理器
type OverlayProcessor ¶
type OverlayProcessor struct {
OverlayImage image.Image // 叠加图像
X int // 叠加位置X坐标
Y int // 叠加位置Y坐标
Opacity float64 // 不透明度 (0-1)
Scale float64 // 缩放比例 (0-n)
Position string // 预设位置 ("center", "top-left", "bottom-right" 等)
}
OverlayProcessor 图层叠加处理器
func NewOverlayProcessor ¶
func NewOverlayProcessor(overlayImage image.Image, x, y int, opacity, scale float64) *OverlayProcessor
NewOverlayProcessor 创建新的图层叠加处理器
func NewOverlayProcessorWithPosition ¶
func NewOverlayProcessorWithPosition(overlayImage image.Image, position string, opacity, scale float64) *OverlayProcessor
NewOverlayProcessorWithPosition 创建新的图层叠加处理器(使用预设位置)
func (*OverlayProcessor) ContextProcess ¶
func (p *OverlayProcessor) ContextProcess(ctx *ImageProcessContext) error
ContextProcess 实现 ContextProcessor 接口
type ProcessorOptions ¶
type ProcessorOptions struct {
// 可以添加通用选项
Quality int // JPEG压缩质量 (1-100)
}
ProcessorOptions 处理器选项
type RotateProcessor ¶
type RotateProcessor struct {
// 旋转角度(度数,顺时针方向)
Angle float64
// 背景颜色(旋转后可能出现的空白区域填充颜色)
Background color.Color
// 是否保持原始尺寸
KeepSize bool
}
RotateProcessor 图像旋转处理器 对图像进行旋转处理,保持原有清晰度
func NewRotateProcessor ¶
func NewRotateProcessor(angle float64) *RotateProcessor
NewRotateProcessor 创建新的旋转处理器
func (*RotateProcessor) WithBackground ¶
func (p *RotateProcessor) WithBackground(background color.Color) *RotateProcessor
WithBackground 设置背景颜色
func (*RotateProcessor) WithKeepSize ¶
func (p *RotateProcessor) WithKeepSize(keepSize bool) *RotateProcessor
WithKeepSize 设置是否保持原始尺寸
type RoundedCornerProcessor ¶
type RoundedCornerProcessor struct {
// 圆角半径,单位为像素
Radius int
}
RoundedCornerProcessor 实现圆角处理器 将图片的四个角切割成圆角,角的大小可以通过半径参数控制
func NewRoundedCornerProcessor ¶
func NewRoundedCornerProcessor(radius int) *RoundedCornerProcessor
NewRoundedCornerProcessor 创建新的圆角处理器 radius: 圆角半径,单位为像素
type TextOptions ¶
type TextOptions struct {
Text string
Position image.Point
Font font.Face
Color color.Color
// 旋转角度(度数,顺时针方向)
Angle float64
// 最大文本宽度(像素)。>0 时按宽度自动换行
MaxWidth float64
// 行距倍数(相对于字体行高),用于换行模式
LineSpacing float64
// 文本对齐方式(左/中/右),用于换行模式
Align gg.Align
// 使用按字符换行(适合中文、日文等无空格语言)
CharWrap bool
}
TextOptions 定义文本处理器的选项
type TextProcessor ¶
type TextProcessor struct {
Options TextOptions
}
TextProcessor 实现文本处理器
func NewTextProcessor ¶
func NewTextProcessor(opts TextOptions) *TextProcessor
NewTextProcessor 创建新的文本处理器
func (*TextProcessor) ContextProcess ¶
func (p *TextProcessor) ContextProcess(ctx *ImageProcessContext) error
ContextProcess 实现 ContextProcessor 接口
func (*TextProcessor) WithAngle ¶
func (p *TextProcessor) WithAngle(angle float64) *TextProcessor
WithAngle 设置文本旋转角度
type WatermarkProcessor ¶
type WatermarkProcessor struct {
Text string // 水印文本
FontSize float64 // 字体大小
Color color.RGBA // 水印颜色
Opacity float64 // 不透明度 (0-1)
Position string // 位置 ("center", "top-left", "bottom-right" 等)
Rotation float64 // 旋转角度
FontFace font.Face // 字体
}
WatermarkProcessor 水印处理器
func NewWatermarkProcessor ¶
func NewWatermarkProcessor(text string, fontSize float64, color color.RGBA, opacity float64, position string, rotation float64) *WatermarkProcessor
NewWatermarkProcessor 创建新的水印处理器
func (*WatermarkProcessor) ContextProcess ¶
func (p *WatermarkProcessor) ContextProcess(ctx *ImageProcessContext) error
ContextProcess 实现 ContextProcessor 接口
type ZoomProcessor ¶
type ZoomProcessor struct {
// 目标宽度和高度
Width int
Height int
// 缩放比例 (0.0-1.0表示缩小,>1.0表示放大)
Ratio float64
// 缩放模式
Mode ZoomMode
// 缩放算法
Scaler draw.Scaler
}
ZoomProcessor 图像缩放处理器 对图像进行像素级缩放,不进行裁剪
func NewZoomHeightProcessor ¶
func NewZoomHeightProcessor(height int) *ZoomProcessor
NewZoomHeightProcessor 创建新的按高度缩放处理器
func NewZoomMaxProcessor ¶
func NewZoomMaxProcessor(size int) *ZoomProcessor
NewZoomMaxProcessor 创建新的按最大边缩放处理器
func NewZoomMinProcessor ¶
func NewZoomMinProcessor(size int) *ZoomProcessor
NewZoomMinProcessor 创建新的按最小边缩放处理器
func NewZoomProcessor ¶
func NewZoomProcessor(width, height int) *ZoomProcessor
NewZoomProcessor 创建新的精确缩放处理器
func NewZoomRatioProcessor ¶
func NewZoomRatioProcessor(ratio float64) *ZoomProcessor
NewZoomRatioProcessor 创建新的按比例缩放处理器
func NewZoomWidthProcessor ¶
func NewZoomWidthProcessor(width int) *ZoomProcessor
NewZoomWidthProcessor 创建新的按宽度缩放处理器
func (*ZoomProcessor) WithScaler ¶
func (p *ZoomProcessor) WithScaler(scaler draw.Scaler) *ZoomProcessor
WithScaler 设置缩放算法