123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package image
- import (
- "image"
- "image/jpeg"
- "image/png"
- "log"
- "os"
- "runtime"
- "strings"
- )
- func LoadImg(filepath string) (img image.Image, err error) {
- file, err := os.Open(filepath)
- if err != nil {
- return
- }
- defer file.Close()
- img, _, err = image.Decode(file)
- return
- }
- func MakeResize(path string, newX, newY, quality int, t int) (newName string, err error) {
- defer func() {
- if r := recover(); r != nil {
- log.Println("[E]", r)
- for skip := 1; ; skip++ {
- _, file, line, ok := runtime.Caller(skip)
- if !ok {
- break
- }
- go log.Printf("%v,%v\n", file, line)
- }
- }
- }()
- img, err := LoadImg(path)
- if nil == err {
- path := strings.Replace(strings.Replace(path, "\\\\", "\\", -1), "\\", "/", -1)
- strs := strings.Split(path, "/")
- oname := strs[len(strs)-1]
- onames := strings.Split(oname, ".")
- // 产生缩略图,等比例缩放
- b := img.Bounds()
- //判断大小
- var newBounds image.Rectangle
- if newX < 0 && newY < 0 {
- newBounds = b
- } else if newX > 0 && newY < 0 {
- newYY := (float32(newX) / float32(b.Dx())) * float32(b.Dy())
- newBounds = image.Rect(0, 0, newX, int(newYY))
- } else if newY > 0 && newX < 0 {
- newXX := (float32(newY) / float32(b.Dy())) * float32(b.Dx())
- newBounds = image.Rect(0, 0, int(newXX), newY)
- } else {
- newBounds = image.Rect(0, 0, newX, newY)
- }
- var max int
- if newBounds.Dx() >= newBounds.Dy() {
- max = newBounds.Dx()
- } else {
- max = newBounds.Dy()
- }
- // If it's gigantic, it's more efficient to downsample first
- // and then resize; resizing will smooth out the roughness.
- var i1 *image.RGBA
- if b.Dx() > 4*max || b.Dy() > 4*max {
- w, h := 2*max, 2*max
- if b.Dx() > b.Dy() {
- h = b.Dy() * h / b.Dx()
- } else {
- w = b.Dx() * w / b.Dy()
- }
- i1 = Resample(img, b, w, h)
- } else {
- // "Resample" to same size, just to convert to RGBA.
- i1 = Resample(img, b, b.Dx(), b.Dy())
- }
- b = i1.Bounds()
- // Encode to PNG.
- dx, dy := newBounds.Dx(), newBounds.Dy()
- log.Println(b.Dx(), b.Dy(), dx, dy, max)
- if b.Dx() > b.Dy() {
- dy = b.Dy() * dx / b.Dx()
- } else {
- dx = b.Dx() * dy / b.Dy()
- }
- i128 := ResizeRGBA(i1, i1.Bounds(), dx, dy)
- var s = "/s_"
- if t == 1 {
- s = "/"
- }
- newF, err1 := os.Create(strings.Join(strs[:(len(strs)-1)], "/") + s + oname)
- defer newF.Close()
- var err error
- switch strings.ToLower(onames[1]) {
- case "jpg":
- err = jpeg.Encode(newF, i128, &jpeg.Options{quality})
- case "jpeg":
- err = jpeg.Encode(newF, i128, &jpeg.Options{quality})
- case "png":
- err = png.Encode(newF, i128)
- }
- if err != nil {
- log.Println("生成缩略图出错", err)
- }
- return strings.Replace(s, "/", "", -1) + oname, err1
- }
- return "", err
- }
- //是否达到压缩条件
- func IsCompress(path string) string {
- img, err := LoadImg(path)
- log.Println(err)
- if nil == err {
- x := img.Bounds().Dx()
- y := img.Bounds().Dy()
- if x > 1920 {
- if x > y {
- return "x"
- } else {
- return "y"
- }
- } else {
- if y > 1080 {
- return "y"
- }
- }
- }
- return ""
- }
- func MakeShareImage(path string) string {
- path = "./web/staticres" + path
- image, err := LoadImg(path)
- if err != nil {
- return "f"
- }
- x := image.Bounds().Dx()
- y := image.Bounds().Dy()
- if x < 300 && y < 300 {
- if x > y {
- x = -1
- y = 350
- } else {
- y = -1
- x = 350
- }
- } else if x < 300 && y > 300 {
- y = -1
- x = 350
- } else if x > 300 && y < 300 {
- x = -1
- y = 350
- }
- log.Println("x:", x, "y:", y)
- rs, _ := MakeResize(path, x, y, 100, 0)
- log.Println("rs:", rs)
- return rs
- }
|