imageresizer

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2020 License: MIT Imports: 24 Imported by: 0

README

Image resizer

Created as part of inspishop e-commerce platform by inspirum team.

Build Status Software License Docker Version Docker Pulls Docker Image Size

On-demand image resizing, format converting and size optimization with the best CLI tools. Prepared to be used as Docker image with cloud storage as Amazon S3.

  • image resizing, format converting and optimization
  • support for jpeg, png, webp, bmp, gif, tiff, svg image formats
  • file-cache for resized images on local disk (temporary cache) and cloud storage (persistent cache)

Usage example

Resizing

Application handle route with dynamic template (parameters divided by dash) and filepath parameters.

GET /image/{template}/{filepath}
Template

Options for resizing are specified as a dash delimited list of parameters, which can be supplied in any order. Duplicate parameters overwrite last values.

Name Template Example Description
width w[X] w200 Resize to 200px width with proportional height
height h[X] h100 Resize to 100px height with proportional width
ratio [X]x[X] 1x2 Resize to ratio to 1:2 (width : height)
crop crop crop Image fill whole canvas (no addition background added)
upscale upscale upscale Image can be larger than original

There are special template string original. This will return optimized image with original dimensions. It is helpful to show image with maximal resolution without knowing its dimension.

When there are set both height and width then ratio parameter will be ignored.

If output image has a different ratio than an original, image will be centered on transparent canvas (jpg has white background because it doesn't support transparent color).

Examples

Values are for an image with dimension 200x400px (width : height).

Template string Output dimension
original 200x400
h100 50x100
w100 100x200
w50-w200 100x200
w100-h300 100x300
w200-1x1 200x200
w200-1x3 200x600
w100-h300-2x1 100x300
w1000-1x1 1000x1000
w1000-1x1-crop 200x200
w1000-1x1-crop-upscale 1000x1000
Width & height

Examples use jpg format because canvas background is white (for better visibility)

h200 w200 w188-h233
(300x200) (200x134) (188x233)
h200 w200 w188-h233
Ratio
h200-1x1 h200-1x2 h200-2x1
(200x200) (100x200) (400x200)
h200-1x1 h200-1x2 h200-121
Crop

crop parameter will crop image to fill whole canvas

h200-1x1-crop h200-1x2-crop h200-2x1-crop
(200x200) (100x200) (400x200)
h200-1x1-crop h200-1x2-crop h200-121-crop
Upscale

By default, image can't up-size (be larger than original)

original w250-h300 w250-h300-upscale w250-h300-crop w250-h300-crop-upscale
(200x200) (250x300) (250x300) (167x200) (250x300)
original w250-h300 w250-h300-upscale w250-h300-crop w250-h300-crop-upscale
original w500-h300-crop w500-h300-crop w500-h300-crop-upscale
(200x200) (500x300) (200x120) (500x300)
original w500-h300 w500-h300-crop w5w500-h300-crop-upscale
Format encoding

Image can be encoded to any supported format simple by changing file extension in URL.

Original image extension is specified in original query parameter.

This is typically used to encode image as web-friendly webp format

http://localhost:3000/image/custom-w400-1x1-upscale/data/products/0/1/background.webp?original=png
Optimization

The package will use these optimizers if they are present on your system:

By setting environment variable you can change default path to binary file or default arguments.

Environment variable Default value
CMD_OPTIMIZER_PNG pngquant --force --ext .png --skip-if-larger --quality 0-75 --speed 4 --strip --
CMD_OPTIMIZER_JPG jpegoptim --force --strip-all --max 75 --quiet --all-progressive
CMD_OPTIMIZER_GIF gifsicle --batch --optimize=3
CMD_OPTIMIZER_SVG svgcleaner
CMD_OPTIMIZER_WEBP cwebp -m 6 -pass 10 -mt -q 75 -quiet
Examples
64 kB 28 kB (43 %) 22 kB (34 %)
original optimized optimized
421 kB 91 kB (21 %) 23 kB (5 %)
original optimized optimized

System requirements

Installation

Testing

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email tomas.novotny@inspirum.cz instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertFile

func ConvertFile(inputFilePath string, inputWidth int, inputHeight int, outputFilePath string, template *Template) (err error)

func CopyFile

func CopyFile(src, dst string) (err error)

func CreateTempFileFromReader

func CreateTempFileFromReader(c io.Reader, ext string) (f *os.File, err error)

func GetEnv

func GetEnv(key string, fallback string) string

func GetEnvAsBool

func GetEnvAsBool(key string, fallback bool) bool

func GetEnvAsInt

func GetEnvAsInt(key string, fallback int) int

func GetImageDimensions

func GetImageDimensions(p string) (w int, h int, err error)

func GetReaderContentType

func GetReaderContentType(c io.ReadSeeker) (mime string, err error)

func NewS3Client

func NewS3Client(endpoint string, bucket string, key string, secret string, region string) *s3client

func OpenFileWithModTime

func OpenFileWithModTime(p string) (*os.File, *time.Time, error)

func OptimizeFile

func OptimizeFile(filePath string) (err error)

func ReplacePathExt

func ReplacePathExt(p string, r *http.Request) string

func ShouldNotResize

func ShouldNotResize(template string, filePath string) bool

func ValidateFilename

func ValidateFilename(filePath string) error

func ValidateTemplate

func ValidateTemplate(template string) error

func WriteFileFromReader

func WriteFileFromReader(p string, c io.Reader) (bool, error)

Types

type HttpError

type HttpError struct {
	StatusCode int
	// contains filtered or unexported fields
}

type Storage

type Storage interface {
	DownloadFile(path string, outputPath string) (f *os.File, err error)
	DownloadFileWithModTime(path string, outputPath string) (f *os.File, t *time.Time, err error)
	UploadContentReaderIfNewer(path string, time time.Time, content io.ReadSeeker) (bool, error)
	UploadContentReader(path string, content io.ReadSeeker) error
}

type Template

type Template struct {
	// contains filtered or unexported fields
}

func NewTemplate

func NewTemplate(template string) *Template

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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