mirror of
https://github.com/Kakune55/Pixel.git
synced 2025-06-28 07:58:03 +08:00
完成请求缩略图功能
This commit is contained in:
parent
ee9aeb4820
commit
7b35f88fee
1
README.md
Normal file
1
README.md
Normal file
@ -0,0 +1 @@
|
|||||||
|

|
6
go.mod
6
go.mod
@ -2,4 +2,8 @@ module Pixel
|
|||||||
|
|
||||||
go 1.21.3
|
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
|
||||||
|
)
|
||||||
|
5
go.sum
5
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 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI=
|
||||||
github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
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=
|
||||||
|
45
main.go
45
main.go
@ -5,6 +5,7 @@ import (
|
|||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"image"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@ -14,6 +15,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/disintegration/imaging"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -62,6 +65,7 @@ func main() {
|
|||||||
http.HandleFunc("/info", showimg)
|
http.HandleFunc("/info", showimg)
|
||||||
http.HandleFunc("/upload", upload)
|
http.HandleFunc("/upload", upload)
|
||||||
http.HandleFunc("/img/",downloadHandler)//设置访问的路由
|
http.HandleFunc("/img/",downloadHandler)//设置访问的路由
|
||||||
|
http.HandleFunc("/img/mini",displayThumbnailHandler)
|
||||||
fmt.Println("Web服务器已启动")
|
fmt.Println("Web服务器已启动")
|
||||||
err := http.ListenAndServe(":9090", nil) //设置监听的端口
|
err := http.ListenAndServe(":9090", nil) //设置监听的端口
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -185,3 +189,44 @@ func fileSize(file *os.File) int64 {
|
|||||||
}
|
}
|
||||||
return stat.Size()
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user