From 7b35f88feefc685000128faacd75138f38b10453 Mon Sep 17 00:00:00 2001 From: Kakune55 Date: Tue, 19 Dec 2023 22:52:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=AF=B7=E6=B1=82=E7=BC=A9?= =?UTF-8?q?=E7=95=A5=E5=9B=BE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + go.mod | 6 +++++- go.sum | 5 +++++ main.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..989552d --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +![12](http://127.0.0.1:9090/img?filename=8bf802009a2889807de418cfa800e5e4.png) \ No newline at end of file diff --git a/go.mod b/go.mod index 7d27c1c..bdfd525 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,8 @@ module Pixel go 1.21.3 -require github.com/mattn/go-sqlite3 v1.14.19 // indirect +require ( + github.com/disintegration/imaging v1.6.2 + github.com/mattn/go-sqlite3 v1.14.19 + golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect +) diff --git a/go.sum b/go.sum index 3042612..244159e 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,7 @@ +github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= +github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI= github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/main.go b/main.go index d943fa5..e977b50 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "crypto/md5" "fmt" "html/template" + "image" "io" "log" "math/rand" @@ -14,6 +15,8 @@ import ( "path/filepath" "strconv" "time" + + "github.com/disintegration/imaging" ) @@ -62,6 +65,7 @@ func main() { http.HandleFunc("/info", showimg) http.HandleFunc("/upload", upload) http.HandleFunc("/img/",downloadHandler)//设置访问的路由 + http.HandleFunc("/img/mini",displayThumbnailHandler) fmt.Println("Web服务器已启动") err := http.ListenAndServe(":9090", nil) //设置监听的端口 if err != nil { @@ -185,3 +189,44 @@ func fileSize(file *os.File) int64 { } return stat.Size() } + +func generateThumbnail(filePath string, width, height int) (*image.NRGBA, error) { + // 打开图像文件 + img, err := imaging.Open(filePath) + if err != nil { + return nil, err + } + + // 调整图像大小以创建缩略图 + thumbnail := imaging.Resize(img, width, height, imaging.Lanczos) + + return thumbnail, nil +} + +func displayThumbnailHandler(w http.ResponseWriter, r *http.Request) { + // 获取请求参数,例如文件名 + filename := r.FormValue("id") + if filename == "" { + http.Error(w, "未提供文件名", http.StatusBadRequest) + return + } + + // 拼接文件路径,确保路径安全性 + filePath := filepath.Join("./data/img", database.GetFileName(filename)) + + // 生成缩略图 + thumbnail, err := generateThumbnail(filePath, 128, 0) // 设置缩略图的宽度和高度 + if err != nil { + http.Error(w, "无法生成缩略图", http.StatusInternalServerError) + return + } + + // 设置响应头,指示这是一张图像 + w.Header().Set("Content-Type", "image/jpeg") // 使用 JPEG 格式或根据需要调整 + + // 将缩略图写入响应体 + if err := imaging.Encode(w, thumbnail, imaging.JPEG); err != nil { + http.Error(w, "无法编码图像", http.StatusInternalServerError) + return + } +}