Categorygithub.com/inspirum/image-resizer-go
repositorypackage
0.2.1
Repository: https://github.com/inspirum/image-resizer-go.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# 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.

NameTemplateExampleDescription
widthw[X]w200Resize to 200px width with proportional height
heighth[X]h100Resize to 100px height with proportional width
ratio[X]x[X]1x2Resize to ratio to 1:2 (width : height)
cropcropcropImage fill whole canvas (no addition background added)
upscaleupscaleupscaleImage 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 stringOutput dimension
original200x400
h10050x100
w100100x200
w50-w200100x200
w100-h300100x300
w200-1x1200x200
w200-1x3200x600
w100-h300-2x1100x300
w1000-1x11000x1000
w1000-1x1-crop200x200
w1000-1x1-crop-upscale1000x1000

Width & height

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

h200w200w188-h233
(300x200)(200x134)(188x233)
h200w200w188-h233

Ratio

h200-1x1h200-1x2h200-2x1
(200x200)(100x200)(400x200)
h200-1x1h200-1x2h200-121

Crop

crop parameter will crop image to fill whole canvas

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

Upscale

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

originalw250-h300w250-h300-upscalew250-h300-cropw250-h300-crop-upscale
(200x200)(250x300)(250x300)(167x200)(250x300)
originalw250-h300w250-h300-upscalew250-h300-cropw250-h300-crop-upscale
originalw500-h300-cropw500-h300-cropw500-h300-crop-upscale
(200x200)(500x300)(200x120)(500x300)
originalw500-h300w500-h300-cropw5w500-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 variableDefault value
CMD_OPTIMIZER_PNGpngquant --force --ext .png --skip-if-larger --quality 0-75 --speed 4 --strip --
CMD_OPTIMIZER_JPGjpegoptim --force --strip-all --max 75 --quiet --all-progressive
CMD_OPTIMIZER_GIFgifsicle --batch --optimize=3
CMD_OPTIMIZER_SVGsvgcleaner
CMD_OPTIMIZER_WEBPcwebp -m 6 -pass 10 -mt -q 75 -quiet

Examples

64 kB28 kB (43 %)22 kB (34 %)
originaloptimizedoptimized
421 kB91 kB (21 %)23 kB (5 %)
originaloptimizedoptimized

System requirements

Installation

Testing

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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