123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package utils
- import (
- "image"
- _ "image/jpeg"
- "io"
- "net/http"
- "os"
- )
- func DownloadImage(url, filepath string) error {
- // 发送 HTTP GET 请求
- resp, err := http.Get(url)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- // 创建文件
- file, err := os.Create(filepath)
- if err != nil {
- return err
- }
- defer file.Close()
- // 将响应流中的数据写入文件
- _, err = io.Copy(file, resp.Body)
- return err
- }
- func GetImage(url string) (image.Image, error) {
- resp, err := http.Get(url)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- img, _, err := image.Decode(resp.Body)
- if err != nil {
- return nil, err
- }
- return img, nil
- }
|