引用包
import (
“bufio”
“flag”
“image”
“image/color”
“image/draw”
“image/png”
“io/ioutil”
“log”
“os”
“github.com/golang/freetype”
“golang.org/x/image/font”
“spiders/mee_nu/conf”
“github.com/disintegration/imaging”
“runtime”
“strings”
“fmt”
)
func ImageCreate(text []string, fileName string, wide, higt int) string {
var (
dpi = flag.Float64("dpi", 180, "screen resolution in Dots Per Inch")
fontfile = flag.String("fontfile", "./media/korssTwo.ttf", "filename of the ttf font")
hinting = flag.String("hinting", "none", "none | full")
size = flag.Float64("size", 15, "font size in points")
spacing = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)")
wonb = flag.Bool("whiteonblack", false, "white text on a black background")
)
flag.Parse()
// Read the font data.
fontBytes, err := ioutil.ReadFile(*fontfile)
if err != nil {
log.Println(err)
return ""
}
f, err := freetype.ParseFont(fontBytes)
if err != nil {
log.Println(err)
return ""
}
// Initialize the context.
fg, bg := image.Black, image.White
ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff}
if *wonb {
fg, bg = image.White, image.Black
ruler = color.RGBA{0x22, 0x22, 0x22, 0xff}
}
rgba := image.NewRGBA(image.Rect(0, 0, wide, higt))
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetDPI(*dpi)
c.SetFont(f)
c.SetFontSize(*size)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
c.SetSrc(fg)
switch *hinting {
default:
c.SetHinting(font.HintingNone)
case "full":
c.SetHinting(font.HintingFull)
}
// Draw the guidelines.
for i := 0; i < 200; i++ {
rgba.Set(10, 10+i, ruler)
rgba.Set(10+i, 10, ruler)
}
// Draw the text.
pt := freetype.Pt(10, 10+int(c.PointToFixed(*size)>>6))
for _, s := range text {
_, err = c.DrawString(s, pt)
if err != nil {
log.Println(err)
return ""
}
pt.Y += c.PointToFixed(*size * *spacing)
}
// Save that RGBA image to disk.
outFile, err := os.Create(conf.ImageFronPath + fileName + "font" + ".png")
if err != nil {
log.Println(err)
os.Exit(1)
}
defer outFile.Close()
b := bufio.NewWriter(outFile)
err = png.Encode(b, rgba)
if err != nil {
log.Println(err)
os.Exit(1)
}
err = b.Flush()
if err != nil {
log.Println(err)
os.Exit(1)
}
//fmt.Println("Wrote out.png OK.")
return conf.ImageFronPath + fileName + "font" + ".png"
}
func ImageZoom(file, path string, ) string { //图片缩放
runtime.GOMAXPROCS(runtime.NumCPU())
var thumbnails []image.Image
img, err := imaging.Open(file)
if err != nil {
log.Println("img_create.go ImageZoom imaging.Open err=", err)
return ""
}
thumb := imaging.Thumbnail(img, conf.ImageWith, conf.ImageWith, imaging.CatmullRom)
thumbnails = append(thumbnails, thumb)
dst := imaging.New(conf.ImageWith, conf.ImageWith, color.NRGBA{0, 0, 0, 0})
errs := imaging.Save(dst, path)
if errs != nil {
log.Println("img_create.go ImageZoom imaging.Save err=", err)
return ""
}
return path
}
func ImageGroup(files []string, path string) string {
runtime.GOMAXPROCS(runtime.NumCPU())
var thumbnails []image.Image
imageWith := 0
for i, file := range files {
fmt.Println(file)
img, err := imaging.Open(file)
if err != nil {
log.Println(“img_create.go ImageGroup imaging.Open err=”, err)
return “”
}
if i == 0 {
thumb := imaging.Thumbnail(img, conf.ImageWith, conf.ImageHigth, imaging.CatmullRom)
thumbnails = append(thumbnails, thumb)
imageWith += conf.ImageWith
} else {
file_name, _ := os.Open(file)
c, _, _ := image.DecodeConfig(file_name)
thumb := imaging.Thumbnail(img, c.Width, c.Height, imaging.CatmullRom)
thumbnails = append(thumbnails, thumb)
imageWith += c.Width
}
}
// create a new blank image
dst := imaging.New(imageWith, conf.ImageHigth, color.NRGBA{0, 0, 0, 0})
// paste thumbnails into the new image side by side
for i, thumb := range thumbnails {
dst = imaging.Paste(dst, thumb, image.Pt(i*conf.ImageWith, 0))
}
// save the combined image to file
err := imaging.Save(dst, path)
if err != nil {
log.Println("img_create.go ImageGroup imaging.Save err=", err)
return ""
}
return path
}
func ImageStart(user string) {
//1,在固定的文件夹里随机哪一张图片
nameFeild := RandUpdateImg()
//2,根据图片的宽高比例生成带有文字的图片
//2.1 拿取string[]
fontImgPath := ImageCreate(ImageText(), user, conf.ImageWith/2, conf.ImageHigth)
//3.合成两张图片存到新的文件夹
resultImgPath := conf.ImageFronPath + user + "_result.jpg"
ImageGroup([]string{nameFeild, fontImgPath}, resultImgPath)
}
conf.go
//图片设置
var ImageTextPath ="./media/key/imgword.txt"
var ImageFronPath ="./media/create/"//创建图片保存位置
var ImageWith =600
var ImageHigth=900