package controller import ( "encoding/hex" "errors" "github.com/gin-gonic/gin" "github.com/skip2/go-qrcode" "gopkg.in/gographics/imagick.v3/imagick" "image/color" "img/app/common" "img/app/config" "log" "net/url" "os" "path/filepath" "strconv" "strings" ) type Image struct { } type argsQr struct { Text string `form:"text"` Size int `form:"size"` Fcolor string `form:"fcolor"` Bcolor string `form:"bcolor"` } // Qr 生成二维码 func (i *Image) Qr(c *gin.Context) { args := argsQr{} if err := c.ShouldBindQuery(&args); err != nil { } log.Println(args) if args.Text == "" { args.Text = config.Config.Qr.DefaultText } if args.Size < 10 || args.Size > 1000 { args.Size = config.Config.Qr.DefaultSize } if args.Fcolor == "" { args.Fcolor = config.Config.Qr.DefaultFcolor } if args.Bcolor == "" { args.Bcolor = config.Config.Qr.DefaultBcolor } fcolor, _ := hex.DecodeString(args.Fcolor) if len(fcolor) != 3 { fcolor = []byte{255, 255, 255} } bcolor, _ := hex.DecodeString(args.Bcolor) if len(bcolor) != 3 { bcolor = []byte{255, 255, 255} } qrCode, _ := qrcode.New(args.Text, qrcode.Medium) qrCode.ForegroundColor = color.RGBA{fcolor[0], fcolor[1], fcolor[2], 255} qrCode.BackgroundColor = color.RGBA{bcolor[0], bcolor[1], bcolor[2], 255} buff, _ := qrCode.PNG(args.Size) _, _ = c.Writer.Write(buff) } type argsThumb struct { W float64 H float64 } // Thumb 生成缩略图 func (i *Image) Thumb(c *gin.Context) { urls := strings.Split(c.Request.URL.String(), "@") filename := filepath.Join(config.Config.Image.Root, config.Config.Image.Source, urls[0]) saveName := filepath.Join(config.Config.Image.Root, config.Config.Image.Thumb, c.Request.URL.String()) if common.FileExists(saveName) { c.File(saveName) return } args := argsThumb{} if len(urls) > 1 { query, _ := url.ParseQuery(urls[1]) args.W, _ = strconv.ParseFloat(query.Get("w"), 64) args.H, _ = strconv.ParseFloat(query.Get("h"), 64) } //创建新的MagicWand mw := imagick.NewMagickWand() //读取文件 err := mw.ReadImage(filename) if err != nil { return } if args.W != 0 || args.H != 0 { err := i.reset(args.W, args.H, mw) if err != nil { c.File(filename) return } } //改变图片质量 err = mw.SetImageCompressionQuality(85) if err != nil { return } //导出图片 if err := os.MkdirAll(filepath.Dir(saveName), os.ModePerm); err != nil { return } err = mw.WriteImage(saveName) c.File(saveName) } func (i *Image) reset(w float64, h float64, mw *imagick.MagickWand) error { width := float64(mw.GetImageWidth()) height := float64(mw.GetImageHeight()) if w >= width || h >= height { return errors.New("can't max") } var newH, newW, crop_x, crop_y float64 if w == 0 { newH = h newW = width / (height / h) } else if h == 0 { newW = w newH = height / (width / w) } else { if width/w < height/h { newW = w newH = height / (width / w) crop_x = 0 crop_y = (newH - h) / 2 } else { newH = h newW = width / (height / h) crop_x = (newW - w) / 2 crop_y = 0 } } _ = mw.ResizeImage(uint(newW), uint(newH), imagick.FILTER_LANCZOS) if w != 0 && h != 0 { _ = mw.CropImage(uint(w), uint(h), int(crop_x), int(crop_y)) } return nil }