diff --git a/image-scale-and-addwatermark/main.go b/image-scale-and-addwatermark/main.go index eb4da53..8979d42 100644 --- a/image-scale-and-addwatermark/main.go +++ b/image-scale-and-addwatermark/main.go @@ -67,12 +67,7 @@ func addWatermark(img *image.RGBA, text string, textColor color.Color, bgColor c d.DrawString(text) } -func main() { - inputImagePath := "input.jpg" // 输入图片路径 - outputImagePath := "output.jpg" // 输出图片路径 - - maxSize := 1200 // 默认最大尺寸 - +func scaleAndAddWatermark(inputImagePath, outputImagePath string, scaleSize int, watermarkText string) { file, err := os.Open(inputImagePath) if err != nil { fmt.Println("Error opening file:", err) @@ -92,11 +87,11 @@ func main() { fmt.Printf("Image %s, width: %d, height: %d\n", inputImagePath, width, height) var resizedImg image.Image - if width > maxSize && height > maxSize { + if width > scaleSize && height > scaleSize { if width < height { - resizedImg = resize.Resize(uint(maxSize), 0, img, resize.Lanczos3) + resizedImg = resize.Resize(uint(scaleSize), 0, img, resize.Lanczos3) } else { - resizedImg = resize.Resize(0, uint(maxSize), img, resize.Lanczos3) + resizedImg = resize.Resize(0, uint(scaleSize), img, resize.Lanczos3) } } else { resizedImg = img @@ -117,7 +112,7 @@ func main() { // 添加水印(使用柔和的深灰色文字和米白色背景) softTextColor := color.RGBA{R: 60, G: 60, B: 60, A: 255} softBgColor := color.RGBA{R: 250, G: 250, B: 245, A: 255} - addWatermark(rgbaImg, "email@example.com", softTextColor, softBgColor) + addWatermark(rgbaImg, watermarkText, softTextColor, softBgColor) outFile, err := os.Create(outputImagePath) if err != nil { @@ -136,3 +131,12 @@ func main() { 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") +}