Thumbnail-generierung für dir Fotos mit eingebaut
This commit is contained in:
parent
176dfefa84
commit
c746f0bae5
@ -4,6 +4,7 @@ Zum Kompilieren folgende Go-Abhängigkeiten installieren:
|
||||
- `go get github.com/gorilla/mux`
|
||||
- `go get github.com/boombuler/barcode`
|
||||
- `go get github.com/boombuler/barcode/qr`
|
||||
|
||||
- `go get github.com/disintegration/imaging`
|
||||
|
||||
Kompilieren mit: `go build main.go`
|
||||
|
||||
|
55
main.go
55
main.go
@ -9,9 +9,11 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/qr"
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
@ -24,6 +26,7 @@ func main() {
|
||||
router := mux.NewRouter()
|
||||
router.HandleFunc("/folder/{foldername:.*}", GetFolderContent).Methods("GET")
|
||||
router.HandleFunc("/qr/{qrlink:.*}", GetQRCode).Methods("GET")
|
||||
router.HandleFunc("/thumb/{imgname:.*}", GetThumbNail).Methods("GET")
|
||||
router.PathPrefix("/img/").Handler(http.StripPrefix("/img/", http.FileServer(http.Dir("C:/Fotos")))).Methods("GET")
|
||||
router.PathPrefix("/").Handler(http.FileServer(http.Dir("web"))).Methods("GET")
|
||||
log.Fatal(http.ListenAndServe(":8000", router))
|
||||
@ -41,7 +44,9 @@ func GetFolderContent(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var dateien []string
|
||||
for _, f := range files {
|
||||
dateien = append(dateien, f.Name())
|
||||
if !strings.HasSuffix(f.Name(), ".thumb.jpg") {
|
||||
dateien = append(dateien, f.Name())
|
||||
}
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(dateien)
|
||||
@ -66,3 +71,51 @@ func GetQRCode(w http.ResponseWriter, r *http.Request) {
|
||||
// Datei ausgeben
|
||||
png.Encode(w, qrCode)
|
||||
}
|
||||
|
||||
func GetThumbNail(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Variablen auswerten
|
||||
params := mux.Vars(r)
|
||||
pIMGName := params["imgname"]
|
||||
|
||||
// Das Thumbnail soll nur generiert werden wenn es noch nicht existiert
|
||||
if !fileExists(StartUpPath + "/" + pIMGName + ".thumb.jpg") {
|
||||
// Originaldatei öffnen, welche angefragt wird
|
||||
src, err := imaging.Open(StartUpPath + "/" + pIMGName)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open image: %v", err)
|
||||
}
|
||||
|
||||
// Foto auf 200px skalieren
|
||||
src = imaging.Resize(src, 200, 0, imaging.NearestNeighbor)
|
||||
|
||||
err = imaging.Save(src, StartUpPath+"/"+pIMGName+".thumb.jpg")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to save image: %v", err)
|
||||
}
|
||||
|
||||
// Datei ausgeben
|
||||
png.Encode(w, src)
|
||||
} else {
|
||||
|
||||
src, err := imaging.Open(StartUpPath + "/" + pIMGName + ".thumb.jpg")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open image: %v", err)
|
||||
}
|
||||
|
||||
// Datei ausgeben
|
||||
png.Encode(w, src)
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Hilfsfunktionen
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
func fileExists(filename string) bool {
|
||||
info, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
||||
|
@ -64,7 +64,7 @@
|
||||
for ( var i = 0, l = content.length; i < l; i++ ) {
|
||||
var ObjInhalt = content[i];
|
||||
$("#Ordner").append(""+
|
||||
"<img src=\""+ServerAPI+"/img/"+folder+"/"+ObjInhalt+"\" width=100 height=100>"+
|
||||
"<img src=\""+ServerAPI+"/thumb/"+folder+"/"+ObjInhalt+"\" width=200>"+
|
||||
"<a href=\""+ServerAPI+"/img/"+folder+"/"+ObjInhalt+"\">"+
|
||||
ObjInhalt+"</a><hr>");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user