olahol / go-imageupload

:white_square_button: Gracefully handle image uploading and thumbnail creation.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

go-imageupload

GoDoc

🔳 Gracefully handle image uploading and thumbnail creation.

Install

go get github.com/olahol/go-imageupload

Thumbnail creator.

package main

import (
	"net/http"

	"github.com/olahol/go-imageupload"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, "index.html")
	})

	http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			http.NotFound(w, r)
			return
		}

		img, err := imageupload.Process(r, "file")

		if err != nil {
			panic(err)
		}

		thumb, err := imageupload.ThumbnailPNG(img, 300, 300)

		if err != nil {
			panic(err)
		}

		thumb.Write(w)
	})

	http.ListenAndServe(":5000", nil)
}
<html>
<body>
  <form method="POST" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
  </form>
</body>
</html>

Contributors

  • Ola Holmström (@olahol)
  • Shintaro Kaneko (@kaneshin)

About

:white_square_button: Gracefully handle image uploading and thumbnail creation.

License:BSD 2-Clause "Simplified" License


Languages

Language:Go 100.0%