1
0
mirror of https://github.com/fumiama/imago.git synced 2026-06-05 00:10:26 +08:00

增加层级log

This commit is contained in:
fumiama
2021-08-10 17:22:56 +08:00
parent 8e0e78837e
commit 3c99a8c654
5 changed files with 50 additions and 9 deletions

View File

@@ -18,4 +18,7 @@ Pick a random image
### func Saveimg(r io.Reader, imgdir string, uid string) string
Save image into imgdir with name like 编码后哈希.webp
### func Addimage(name string)
manually add an image name into map
manually add an image name into map
### func Imgexsits(name string) bool
Return whether the name is in map
### func Setloglevel(level log.Level)

2
go.mod
View File

@@ -6,4 +6,6 @@ require (
github.com/corona10/goimagehash v1.0.3
github.com/fumiama/go-base16384 v1.1.0
github.com/kolesa-team/go-webp v1.0.0
github.com/sirupsen/logrus v1.8.1
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
)

16
go.sum
View File

@@ -1,13 +1,29 @@
github.com/corona10/goimagehash v1.0.3 h1:NZM518aKLmoNluluhfHGxT3LGOnrojrxhGn63DR/CZA=
github.com/corona10/goimagehash v1.0.3/go.mod h1:VkvE0mLn84L4aF8vCb6mafVajEb6QYMHl2ZJLn0mOGI=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fumiama/go-base16384 v1.1.0 h1:4npregEpgJ4HtHYNMcw7eabSSO8GIqfG3xGZWtcYcIA=
github.com/fumiama/go-base16384 v1.1.0/go.mod h1:YCaxEQyBX717X2lMlJvdccNhA83zhKRR/ipPOIIIQj8=
github.com/kolesa-team/go-webp v1.0.0 h1:HFE7NhW1jkM59HgUwRdcSJ+VVZz7F1RZk6VFY27YZpQ=
github.com/kolesa-team/go-webp v1.0.0/go.mod h1:P0sDD6OLl4jBVcwCcYRuu1C+kUrR4V19LYWs+yV3UcY=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1:J6v8awz+me+xeb/cUTotKgceAYouhIB3pjzgRd6IlGk=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA=
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d h1:RNPAfi2nHY7C2srAV8A49jpsYr0ADedCk1wq6fTMTvs=
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

View File

@@ -1,4 +1,3 @@
// Package imago 图片处理相关
package imago
import (

View File

@@ -1,8 +1,8 @@
// Package imago Image saving & comparing tool for go based on webp.
package imago
import (
"bytes"
"fmt"
"image"
"io"
"math/rand"
@@ -14,6 +14,8 @@ import (
"github.com/kolesa-team/go-webp/decoder"
"github.com/kolesa-team/go-webp/encoder"
"github.com/kolesa-team/go-webp/webp"
log "github.com/sirupsen/logrus"
easy "github.com/t-tomalak/logrus-easy-formatter"
)
var (
@@ -21,6 +23,20 @@ var (
mutex sync.Mutex
)
func init() {
log.SetFormatter(&easy.Formatter{
TimestampFormat: "2006-01-02 15:04:05",
LogFormat: "[imago][%time%][%lvl%]: %msg% \n",
})
log.SetLevel(log.DebugLevel)
}
// Setloglevel
func Setloglevel(level log.Level) {
log.SetLevel(level)
}
// Imgexsits Return whether the name is in map
func Imgexsits(name string) bool {
index := name[:3]
tail := name[3:]
@@ -46,10 +62,10 @@ func Addimage(name string) {
defer mutex.Unlock()
if images[index] == nil {
images[index] = make([]string, 0)
fmt.Println("[addimage] create index", index, ".")
log.Debugln("[addimage] create index", index, ".")
}
images[index] = append(images[index], tail)
fmt.Println("[addimage] index", index, "append file", tail, ".")
log.Debugln("[addimage] index", index, "append file", tail, ".")
images["sum"] = append(images["sum"], name)
}
@@ -64,44 +80,49 @@ func Saveimgbytes(b []byte, imgdir string, uid string, force bool) string {
if err == nil {
iswebp = true
} else {
fmt.Printf("[saveimg] decode image error: %v\n", err)
log.Errorf("[saveimg] decode image error: %v\n", err)
return "\"stat\": \"notanimg\""
}
}
dh, err := GetDHashStr(img)
if err != nil {
log.Errorf("[saveimg] get dhash error: %v\n", err)
return "\"stat\": \"dherr\""
}
if force && Imgexsits(dh) {
log.Debugf("[saveimg] force find similar image %s.\n", dh)
return "\"stat\":\"exist\", \"img\": \"" + url.QueryEscape(dh) + "\""
} else {
for _, name := range images["sum"] {
diff, err := HammDistance(dh, name)
if err == nil && diff < 10 { // 认为是一张图片
fmt.Printf("[saveimg] old %s.\n", name)
log.Debugf("[saveimg] old %s.\n", name)
return "\"stat\":\"exist\", \"img\": \"" + url.QueryEscape(name) + "\""
}
}
}
f, err := os.Create(imgdir + dh + ".webp")
if err != nil {
log.Errorf("[saveimg] create webp file error: %v\n", err)
return "\"stat\": \"ioerr\""
}
defer f.Close()
if !iswebp {
options, err := encoder.NewLossyEncoderOptions(encoder.PresetDefault, 75)
if err != nil || webp.Encode(f, img, options) != nil {
log.Errorf("[saveimg] encode webp error: %v\n", err)
return "\"stat\": \"encerr\""
}
} else {
r.Seek(0, io.SeekStart)
c, err := io.Copy(f, r)
if err != nil {
log.Errorf("[saveimg] copy file error: %v\n", err)
return "\"stat\": \"ioerr\""
}
fmt.Printf("[saveimg] save %d bytes.\n", c)
log.Debugf("[saveimg] save %d bytes.\n", c)
}
fmt.Printf("[saveimg] new %s.\n", dh)
log.Debugf("[saveimg] new %s.\n", dh)
return "\"stat\":\"success\", \"img\": \"" + url.QueryEscape(dh) + "\""
}