39 lines
728 B
Go
39 lines
728 B
Go
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,
|
|
}
|
|
}
|