imageupload

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2022 License: BSD-2-Clause Imports: 12 Imported by: 11

README

go-imageupload

GoDoc

🔳 Gracefully handle image uploading and thumbnail creation.

Install

go get github.com/olahol/go-imageupload

Example

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)

Documentation

Documentation

Overview

Gracefully handle image uploading and thumbnail creation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LimitFileSize

func LimitFileSize(maxSize int64, w http.ResponseWriter, r *http.Request)

Limit the size of uploaded files, put this before imageupload.Process.

Types

type Image

type Image struct {
	Filename    string
	ContentType string
	Data        []byte
	Size        int
}

func Process

func Process(r *http.Request, field string) (*Image, error)

Process uploaded file into an image.

func ThumbnailJPEG

func ThumbnailJPEG(i *Image, width int, height int, quality int) (*Image, error)

Create JPEG thumbnail.

func ThumbnailPNG

func ThumbnailPNG(i *Image, width int, height int) (*Image, error)

Create PNG thumbnail.

func (*Image) DataURI

func (i *Image) DataURI() string

Convert image to base64 data uri.

func (*Image) Save

func (i *Image) Save(filename string) error

Save image to file.

func (*Image) ThumbnailJPEG

func (i *Image) ThumbnailJPEG(width int, height int, quality int) (*Image, error)

Create JPEG thumbnail from image.

func (*Image) ThumbnailPNG

func (i *Image) ThumbnailPNG(width int, height int) (*Image, error)

Create PNG thumbnail from image.

func (*Image) Write

func (i *Image) Write(w http.ResponseWriter)

Write image to HTTP response.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL