update image options

This commit is contained in:
2026-03-11 00:00:19 +08:00
parent 9fc2a50e8e
commit 27510a29d4

View File

@@ -26,6 +26,7 @@ func addWatermark(img *image.RGBA, text string, textColor color.Color, bgColor c
fmt.Println("Error parsing font collection:", err)
return
}
ttFont, err := ttCollection.Font(0)
if err != nil {
fmt.Println("Error getting font from collection:", err)
@@ -67,7 +68,7 @@ func addWatermark(img *image.RGBA, text string, textColor color.Color, bgColor c
d.DrawString(text)
}
func scaleAndAddWatermark(inputImagePath, outputImagePath string, scaleSize int, watermarkText string) {
func scaleAndAddWatermark(inputImagePath, outputImagePath string, options ImageOptions) {
file, err := os.Open(inputImagePath)
if err != nil {
fmt.Println("Error opening file:", err)
@@ -87,11 +88,11 @@ func scaleAndAddWatermark(inputImagePath, outputImagePath string, scaleSize int,
fmt.Printf("Image %s, width: %d, height: %d\n", inputImagePath, width, height)
var resizedImg image.Image
if width > scaleSize && height > scaleSize {
if width > options.ScaleSize && height > options.ScaleSize {
if width < height {
resizedImg = resize.Resize(uint(scaleSize), 0, img, resize.Lanczos3)
resizedImg = resize.Resize(uint(options.ScaleSize), 0, img, resize.Lanczos3)
} else {
resizedImg = resize.Resize(0, uint(scaleSize), img, resize.Lanczos3)
resizedImg = resize.Resize(0, uint(options.ScaleSize), img, resize.Lanczos3)
}
} else {
resizedImg = img
@@ -109,10 +110,12 @@ func scaleAndAddWatermark(inputImagePath, outputImagePath string, scaleSize int,
}
}
if options.WatermarkText != "" {
// 添加水印(使用柔和的深灰色文字和米白色背景)
softTextColor := color.RGBA{R: 60, G: 60, B: 60, A: 255}
softBgColor := color.RGBA{R: 250, G: 250, B: 245, A: 255}
addWatermark(rgbaImg, watermarkText, softTextColor, softBgColor)
addWatermark(rgbaImg, options.WatermarkText, softTextColor, softBgColor)
}
outFile, err := os.Create(outputImagePath)
if err != nil {
@@ -122,7 +125,7 @@ func scaleAndAddWatermark(inputImagePath, outputImagePath string, scaleSize int,
defer outFile.Close()
err = jpeg.Encode(outFile, rgbaImg, &jpeg.Options{
Quality: 95,
Quality: options.Quality,
})
if err != nil {
fmt.Println("Error encoding image:", err)
@@ -132,11 +135,19 @@ func scaleAndAddWatermark(inputImagePath, outputImagePath string, scaleSize int,
fmt.Println("Image processed and saved as", filepath.Base(outputImagePath))
}
func main() {
inputImagePath := "input.jpg" // 输入图片路径
outputImagePath := "output.jpg" // 输出图片路径
imageSize := 1200 // 默认尺寸
scaleAndAddWatermark(inputImagePath, outputImagePath, imageSize, "email@example.com")
type ImageOptions struct {
Quality int
ScaleSize int
WatermarkText string
}
func main() {
inputImagePath := "input.jpg"
outputImagePath := "output.jpg"
scaleAndAddWatermark(inputImagePath, outputImagePath, ImageOptions{
Quality: 95,
WatermarkText: "email@example.com",
ScaleSize: 1200,
})
}