update image options
This commit is contained in:
@@ -26,6 +26,7 @@ func addWatermark(img *image.RGBA, text string, textColor color.Color, bgColor c
|
|||||||
fmt.Println("Error parsing font collection:", err)
|
fmt.Println("Error parsing font collection:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ttFont, err := ttCollection.Font(0)
|
ttFont, err := ttCollection.Font(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error getting font from collection:", err)
|
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)
|
d.DrawString(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func scaleAndAddWatermark(inputImagePath, outputImagePath string, scaleSize int, watermarkText string) {
|
func scaleAndAddWatermark(inputImagePath, outputImagePath string, options ImageOptions) {
|
||||||
file, err := os.Open(inputImagePath)
|
file, err := os.Open(inputImagePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error opening file:", err)
|
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)
|
fmt.Printf("Image %s, width: %d, height: %d\n", inputImagePath, width, height)
|
||||||
|
|
||||||
var resizedImg image.Image
|
var resizedImg image.Image
|
||||||
if width > scaleSize && height > scaleSize {
|
if width > options.ScaleSize && height > options.ScaleSize {
|
||||||
if width < height {
|
if width < height {
|
||||||
resizedImg = resize.Resize(uint(scaleSize), 0, img, resize.Lanczos3)
|
resizedImg = resize.Resize(uint(options.ScaleSize), 0, img, resize.Lanczos3)
|
||||||
} else {
|
} else {
|
||||||
resizedImg = resize.Resize(0, uint(scaleSize), img, resize.Lanczos3)
|
resizedImg = resize.Resize(0, uint(options.ScaleSize), img, resize.Lanczos3)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
resizedImg = img
|
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}
|
softTextColor := color.RGBA{R: 60, G: 60, B: 60, A: 255}
|
||||||
addWatermark(rgbaImg, watermarkText, softTextColor, softBgColor)
|
softBgColor := color.RGBA{R: 250, G: 250, B: 245, A: 255}
|
||||||
|
addWatermark(rgbaImg, options.WatermarkText, softTextColor, softBgColor)
|
||||||
|
}
|
||||||
|
|
||||||
outFile, err := os.Create(outputImagePath)
|
outFile, err := os.Create(outputImagePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -122,7 +125,7 @@ func scaleAndAddWatermark(inputImagePath, outputImagePath string, scaleSize int,
|
|||||||
defer outFile.Close()
|
defer outFile.Close()
|
||||||
|
|
||||||
err = jpeg.Encode(outFile, rgbaImg, &jpeg.Options{
|
err = jpeg.Encode(outFile, rgbaImg, &jpeg.Options{
|
||||||
Quality: 95,
|
Quality: options.Quality,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error encoding image:", err)
|
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))
|
fmt.Println("Image processed and saved as", filepath.Base(outputImagePath))
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
type ImageOptions struct {
|
||||||
inputImagePath := "input.jpg" // 输入图片路径
|
Quality int
|
||||||
outputImagePath := "output.jpg" // 输出图片路径
|
ScaleSize int
|
||||||
|
WatermarkText string
|
||||||
imageSize := 1200 // 默认尺寸
|
}
|
||||||
|
|
||||||
scaleAndAddWatermark(inputImagePath, outputImagePath, imageSize, "email@example.com")
|
func main() {
|
||||||
|
inputImagePath := "input.jpg"
|
||||||
|
outputImagePath := "output.jpg"
|
||||||
|
|
||||||
|
scaleAndAddWatermark(inputImagePath, outputImagePath, ImageOptions{
|
||||||
|
Quality: 95,
|
||||||
|
WatermarkText: "email@example.com",
|
||||||
|
ScaleSize: 1200,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user