From 5d4580cbf732db9845912e3651813492cd411ae0 Mon Sep 17 00:00:00 2001 From: Kakune55 Date: Mon, 25 Dec 2023 22:21:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=AF=A6=E7=BB=86=E9=A1=B5?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=B8=83=E5=B1=80=E5=A4=A7=E5=B0=8F&?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=A9=E7=95=A5=E5=9B=BE=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 ++++++++- Web/info.html | 2 +- Web/list.html | 2 +- main.go | 9 ++++++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 776cb08..46d0d28 100644 --- a/README.md +++ b/README.md @@ -17,12 +17,19 @@ - /idlist 列出所有图片id ## 部分接口文档 -### `/img` `/img/mini` `/del` `/info` +### `/img` `/del` `/info` #### 请求方式 Get #### 请求参数 id 图片id +### `/img/mini` +#### 请求方式 +Get +#### 请求参数 +id 图片id +size 图片横向大小 单位像素 0-1024 + ## 使用的外部库 "github.com/disintegration/imaging" MIT LICENSE diff --git a/Web/info.html b/Web/info.html index 8a7d364..cdb4f75 100644 --- a/Web/info.html +++ b/Web/info.html @@ -68,7 +68,7 @@ } #imgshow { - width: 100%; + max-width: 100%; } #delete-button { diff --git a/Web/list.html b/Web/list.html index 803cff2..95e24ee 100644 --- a/Web/list.html +++ b/Web/list.html @@ -77,7 +77,7 @@ galleryItem.classList.add("gallery-item"); const img = document.createElement("img"); - img.src = `/img/mini?id=${id}`; + img.src = `/img/mini?id=${id}&size=192`; const infoBox = document.createElement("div"); infoBox.classList.add("info-box"); diff --git a/main.go b/main.go index a43e443..829fce0 100644 --- a/main.go +++ b/main.go @@ -227,6 +227,13 @@ func generateThumbnail(filePath string, width, height int) (*image.NRGBA, error) func displayThumbnailHandler(w http.ResponseWriter, r *http.Request) { // 获取请求参数,例如文件名 filename := r.FormValue("id") + imgsize, err := strconv.Atoi(r.FormValue("size")) + if err != nil { + http.Error(w, "size参数错误 应为纯数字", http.StatusBadRequest) + } + if imgsize <= 1 || imgsize > 1024 { + http.Error(w, "size参数错误 范围应在0-1024之间", http.StatusBadRequest) + } if filename == "" { http.Error(w, "未提供文件名", http.StatusBadRequest) return @@ -236,7 +243,7 @@ func displayThumbnailHandler(w http.ResponseWriter, r *http.Request) { filePath := filepath.Join("./data/img", database.GetFileName(filename)) // 生成缩略图 - thumbnail, err := generateThumbnail(filePath, 128, 0) // 设置缩略图的宽度和高度 + thumbnail, err := generateThumbnail(filePath, imgsize, 0) // 设置缩略图的宽度和高度 if err != nil { http.Error(w, "无法生成缩略图", http.StatusInternalServerError) return