@@ -0,0 +1,38 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type PictureProp struct {
|
||||
Size int `json:"size" bson:"size,omitempty"` // 单位:b
|
||||
Width int `json:"height" bson:"height,omitempty"`
|
||||
Height int `json:"width" bson:"width,omitempty"`
|
||||
}
|
||||
|
||||
const ImgUrlHost = "http://image.lhexm.com/"
|
||||
|
||||
func GetPictureProp(imgURL string) PictureProp {
|
||||
res, err := http.Get(imgURL)
|
||||
if err != nil {
|
||||
return PictureProp{}
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return PictureProp{}
|
||||
}
|
||||
buf := bytes.NewBuffer(body)
|
||||
cfg, _, err := image.DecodeConfig(buf)
|
||||
if err != nil {
|
||||
return PictureProp{}
|
||||
}
|
||||
return PictureProp{
|
||||
Size: len(body),
|
||||
Width: cfg.Width,
|
||||
Height: cfg.Height,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user