imageflux

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2023 License: MIT Imports: 13 Imported by: 0

README

go-imageflux

Build Status Go Reference

URL builder and parser for ImageFlux.

Usage

ImageFlux is Image Conversion & Distribution Engine. This allows you to easily generate images optimized for each device based on a single source image, and delivers them quickly and with high quality.

The imageflux package builds and parse URLs for ImageFlux.

Build URL

In ImageFlux, parameters for image transformation are embedded in the URL.

proxy := &imageflux.Proxy{
    Host: "demo.imageflux.jp",
}
cfg := &imageflux.Config{
    // resize the image to 200px width.
    Width: 200,

    // convert the image to WebP format.
    Format: imageflux.FormatWebPAuto,
}
u := proxy.Image("/images/1.jpg", cfg).SignedURL()
fmt.Println(u)

// Output:
// https://demo.imageflux.jp/c/w=200,f=webp:auto/images/1.jpg
Build Signed URL

By attaching a signature to the transformation parameters, it prevents third parties from rewriting the URL.

proxy := &imageflux.Proxy{
    Host:   "demo.imageflux.jp",
    Secret: "testsigningsecret",
}
cfg := &imageflux.Config{
    // resize the image to 200px width.
    Width: 200,
}
u := proxy.Image("/images/1.jpg", cfg).SignedURL()
fmt.Println(u)

// Output:
// https://demo.imageflux.jp/c/sig=1.tiKX5u2kw6wp9zDgl1tLiOIi8IsoRIBw8fVgVc0yrNg=,w=200/images/1.jpg
Parse URL
proxy := &imageflux.Proxy{}
image, err := proxy.Parse("/c/w=200/images/1.jpg", "")
if err != nil {
    log.Fatal(err)
}
fmt.Printf("path = %s\n", image.Path)
fmt.Printf("width = %d\n", image.Config.Width)

// Output:
// path = /images/1.jpg
// width = 200

References

Documentation

Overview

The imageflux package builds and parse URLs for ImageFlux.

ImageFlux is Image Conversion & Distribution Engine. This allows you to easily generate images optimized for each device based on a single source image, and delivers them quickly and with high quality.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrExpired = errors.New("imageflux: expired")

ErrExpired is returned when the image is expired.

View Source
var ErrInvalidSignature = errors.New("imageflux: invalid signature")

ErrInvalidSignature is returned when the signature is invalid.

Functions

This section is empty.

Types

type AspectMode

type AspectMode int

AspectMode is aspect mode.

const (
	// AspectModeDefault is the default value of aspect mode.
	AspectModeDefault AspectMode = iota

	// AspectModeScale holds the the aspect ratio of the input image,
	// and scales to fit in the specified size.
	AspectModeScale

	// AspectModeForceScale ignores the aspect ratio of the input image.
	AspectModeForceScale

	// AspectModeCrop holds the the aspect ratio of the input image,
	// and crops the image.
	AspectModeCrop

	// AspectModePad holds the the aspect ratio of the input image,
	// and fills the unfilled portion with the specified background color.
	AspectModePad
)

func (AspectMode) String

func (a AspectMode) String() string

type Blur added in v1.1.0

type Blur struct {
	Radius int
	Sigma  float64
}

Blur is a blur config.

type Config

type Config struct {
	// Width is width in pixel of the scaled image.
	Width int

	// Height is height in pixel of the scaled image.
	Height int

	// DisableEnlarge disables enlarge.
	DisableEnlarge bool

	// AspectMode is aspect mode.
	AspectMode AspectMode

	// DevicePixelRatio is a scale factor of device pixel ratio.
	// If DevicePixelRatio is 0, it is ignored.
	DevicePixelRatio float64

	// InputClip is a position in pixel of clipping area.
	// This is used for the input image.
	InputClip image.Rectangle

	// InputClipRatio is a position in ratio of clipping area.
	// The coordinates of the rectangle are divided by ClipMax.X or ClipMax.Y.
	// This is used for the input image.
	InputClipRatio image.Rectangle

	// InputOrigin is the position of the input image origin.
	InputOrigin Origin

	// OutputClip is a position in pixel of clipping area.
	// This is used for the output image.
	OutputClip image.Rectangle

	// Clip is an alias of OutputClip.
	// If both Clip and OutputClip are set, OutputClip is used.
	//
	// Deprecated: Use OutputClip instead.
	Clip image.Rectangle

	// OutputClipRatio is a position in ratio of clipping area.
	// The coordinates of the rectangle are divided by ClipMax.X or ClipMax.Y.
	OutputClipRatio image.Rectangle

	// ClipRatio is an alias of OutputClipRatio.
	// If both ClipRatio and OutputClipRatio are set, OutputClipRatio is used.
	//
	// Deprecated: Use OutputClipRatio instead.
	ClipRatio image.Rectangle

	// OutputOrigin is the position of the output image origin.
	OutputOrigin Origin

	// ClipMax is the denominators of ClipRatio.
	ClipMax image.Point

	// Origin is the position of the image origin.
	Origin Origin

	// Background is background color.
	Background color.Color

	// InputRotate rotates the image before processing.
	InputRotate Rotate

	// OutputRotate rotates the image after processing.
	OutputRotate Rotate

	// OutputRotate rotates the image after processing.
	// This is an alias of OutputRotate.
	// If both Rotate and OutputRotate are set, OutputRotate is used.
	//
	// Deprecated: Use OutputRotate instead.
	Rotate Rotate

	// Through is a format to pass through.
	Through Through

	// Overlay Parameters.
	Overlays []*Overlay

	// Output Parameters.
	Format Format

	// Quality is quality of the output image.
	// It is used when the output format is JPEG or WebP.
	Quality int

	// DisableOptimization disables optimization of the Huffman coding table
	// of the output image when the output format is JPEG.
	DisableOptimization bool

	// Lossless enables lossless compression when the output format is WebP.
	Lossless bool

	// ExifOption specifies the Exif information to be included in the output image.
	ExifOption ExifOption

	// Unsharp configures unsharp mask.
	Unsharp Unsharp

	// Blur configures blur.
	Blur Blur

	// GrayScale converts to gray scale.
	// 0 means no conversion and 100 means full conversion.
	GrayScale int

	// Sepia converts to sepia.
	// 0 means no conversion and 100 means full conversion.
	Sepia int

	// Brightness adjusts brightness.
	// The value set in Brightness plus 100 is actually used.
	Brightness int

	// Contrast adjusts contrast.
	// The value set in Contrast plus 100 is actually used.
	Contrast int

	// Invert inverts the image if it is true.
	Invert bool
}

Config is configure of image.

func ParseConfig added in v1.2.0

func ParseConfig(s string) (config *Config, rest string, err error)

func (*Config) String

func (c *Config) String() string

String returns a string representing the Config. If c is nil or zero value, it returns "f=auto".

type ExifOption added in v1.2.0

type ExifOption int

ExifOption specifies the Exif information to be included in the output image.

const (
	// ExifOptionDefault is the default value of ExifOption.
	ExifOptionDefault ExifOption = 0

	// ExifOptionStrip removes all Exif information from the output image.
	ExifOptionStrip ExifOption = 1

	// ExifOptionKeepOrientation removes all Exif information
	// except Orientation from the output image.
	ExifOptionKeepOrientation ExifOption = 2
)

type Format

type Format string

Format is the format of the output image.

const (
	// FormatAuto encodes the image by the same format with the input image.
	FormatAuto Format = "auto"

	// FormatJPEG encodes the image as JPEG.
	FormatJPEG Format = "jpg"

	// FormatPNG encodes the image as PNG.
	FormatPNG Format = "png"

	// FormatGIF encodes the image as GIF.
	FormatGIF Format = "gif"

	// FormatWebP encodes the image as WebP.
	FormatWebP Format = "webp"

	// FormatWebPAuto encodes the image as a WebP if the client supports WebP.
	// Otherwise, the image is encoded as the same format with the input image.
	FormatWebPAuto Format = "webp:auto"

	// FormatWebPJPEG encodes the image as a WebP if the client supports WebP.
	// Otherwise, the image is encoded as JPEG.
	FormatWebPJPEG Format = "webp:jpg"

	// FormatWebPPNG encodes the image as a WebP if the client supports WebP.
	// Otherwise, the image is encoded as PNG.
	FormatWebPPNG Format = "webp:png"

	// FormatWebPGIF encodes the image as a WebP if the client supports WebP.
	// Otherwise, the image is encoded as GIF.
	FormatWebPGIF Format = "webp:gif"

	// FormatWebPFromJPEG encodes the image as a WebP.
	//
	// Deprecated: use FormatWebPJPEG instead.
	FormatWebPFromJPEG Format = "webp:jpeg"

	// FormatWebPFromPNG encodes the image as a WebP.
	//
	// Deprecated: use FormatWebPPNG instead.
	FormatWebPFromPNG Format = "webp:png"
)

func (Format) String

func (f Format) String() string

type Image

type Image struct {
	// Path is the path of the image.
	Path string

	// Proxy is the proxy server.
	Proxy *Proxy

	// Config is the configuration of
	// converting the image.
	Config *Config

	// Expires is the expiration time of the url
	// generated by String and SignedURL.
	// If Expires is zero, the url does not expire.
	Expires time.Time
}

Image is an image served via ImageFlux.

func (*Image) Sign

func (img *Image) Sign() string

Sign returns the signature.

func (*Image) SignedURL

func (img *Image) SignedURL() string

SignedURL returns the URL of the image with the signature.

Example
proxy := &imageflux.Proxy{
	Host: "demo.imageflux.jp",
}
cfg := &imageflux.Config{
	// resize the image to 200px width.
	Width: 200,

	// convert the image to WebP format.
	Format: imageflux.FormatWebPAuto,
}
u := proxy.Image("/images/1.jpg", cfg).SignedURL()
fmt.Println(u)
Output:
https://demo.imageflux.jp/c/w=200,f=webp:auto/images/1.jpg
Example (Signed)
proxy := &imageflux.Proxy{
	Host:   "demo.imageflux.jp",
	Secret: "testsigningsecret",
}
cfg := &imageflux.Config{
	// resize the image to 200px width.
	Width: 200,
}
u := proxy.Image("/images/1.jpg", cfg).SignedURL()
fmt.Println(u)
Output:
https://demo.imageflux.jp/c/sig=1.tiKX5u2kw6wp9zDgl1tLiOIi8IsoRIBw8fVgVc0yrNg=,w=200/images/1.jpg

func (*Image) SignedURLWithoutComma added in v1.2.0

func (img *Image) SignedURLWithoutComma() string

SignedURLWithoutComma is same as SignedURL but the url doesn't contain comma. It is useful for srcset of HTML img tag.

Example
proxy := &imageflux.Proxy{
	Host: "demo.imageflux.jp",
}
cfg := &imageflux.Config{
	// resize the image to 200px width.
	Width: 200,

	// convert the image to WebP format.
	Format: imageflux.FormatWebPAuto,
}
u := proxy.Image("/images/1.jpg", cfg).SignedURLWithoutComma()
fmt.Println(u)
Output:
https://demo.imageflux.jp/c/w=200%2Cf=webp:auto/images/1.jpg

func (*Image) String

func (img *Image) String() string

String returns the URL of the image without the signature.

type MaskType added in v1.2.0

type MaskType string

MaskType specifies the area to be treated as a mask.

const (
	// MaskTypeWhite clips the mask image leaving the white parts.
	MaskTypeWhite MaskType = "white"

	// MaskTypeBlack clips the mask image leaving the black parts.
	MaskTypeBlack MaskType = "black"

	// MaskTypeAlpha clips the mask image leaving the opaque parts.
	MaskTypeAlpha MaskType = "alpha"
)

type Origin

type Origin int

Origin is the origin.

const (
	// OriginDefault is default origin.
	OriginDefault Origin = 0

	// OriginTopLeft is top-left
	OriginTopLeft Origin = 1

	// OriginTopCenter is top-center
	OriginTopCenter Origin = 2

	// OriginTopRight is top-right
	OriginTopRight Origin = 3

	// OriginMiddleLeft is middle-left
	OriginMiddleLeft Origin = 4

	// OriginMiddleCenter is middle-center
	OriginMiddleCenter Origin = 5

	// OriginMiddleRight is middle-right
	OriginMiddleRight Origin = 6

	// OriginBottomLeft is bottom-left
	OriginBottomLeft Origin = 7

	// OriginBottomCenter is bottom-center
	OriginBottomCenter Origin = 8

	// OriginBottomRight is bottom-right
	OriginBottomRight Origin = 9
)

func (Origin) String

func (o Origin) String() string

type Overlay

type Overlay struct {
	// Path is a path for overlay image.
	Path string

	// URL is an url for overlay image.
	//
	// Deprecated: Use Path instead.
	URL string

	// Width is width in pixel of the scaled image.
	Width int

	// Height is height in pixel of the scaled image.
	Height int

	// DisableEnlarge disables enlarge.
	DisableEnlarge bool

	// AspectMode is aspect mode.
	AspectMode AspectMode

	// InputClip is a position in pixel of clipping area.
	// This is used for the input image.
	InputClip image.Rectangle

	// InputClipRatio is a position in ratio of clipping area.
	// The coordinates of the rectangle are divided by ClipMax.X or ClipMax.Y.
	// This is used for the input image.
	InputClipRatio image.Rectangle

	// InputOrigin is the position of the input image origin.
	InputOrigin Origin

	// OutputClip is a position in pixel of clipping area.
	// This is used for the output image.
	OutputClip image.Rectangle

	// Clip is an alias of OutputClip.
	// If both Clip and OutputClip are set, OutputClip is used.
	//
	// Deprecated: Use OutputClip instead.
	Clip image.Rectangle

	// OutputClipRatio is a position in ratio of clipping area.
	// The coordinates of the rectangle are divided by ClipMax.X or ClipMax.Y.
	OutputClipRatio image.Rectangle

	// ClipRatio is an alias of OutputClipRatio.
	// If both ClipRatio and OutputClipRatio are set, OutputClipRatio is used.
	//
	// Deprecated: Use OutputClipRatio instead.
	ClipRatio image.Rectangle

	// OutputOrigin is the position of the output image origin.
	OutputOrigin Origin

	// ClipMax is the denominators of ClipRatio.
	ClipMax image.Point

	// Origin is the position of the image origin.
	Origin Origin

	// Background is background color.
	Background color.Color

	// InputRotate rotates the image before processing.
	InputRotate Rotate

	// OutputRotate rotates the image after processing.
	OutputRotate Rotate

	// OutputRotate rotates the image after processing.
	// This is an alias of OutputRotate.
	// If both Rotate and OutputRotate are set, OutputRotate is used.
	//
	// Deprecated: Use OutputRotate instead.
	Rotate Rotate

	// Offset is an offset in pixel of overlay image.
	Offset image.Point

	// OffsetRatio is an offset in ratio of overlay image.
	// The coordinates of the rectangle are divided by OffsetMax.X or OffsetMax.Y.
	OffsetRatio image.Point

	// OffsetMax is the denominators of OffsetRatio.
	OffsetMax image.Point

	// OverlayOrigin is the position of the overlay image origin.
	OverlayOrigin Origin

	// MaskType specifies the area to be treated as a mask.
	MaskType MaskType

	// PaddingMode specifies processing when the specified image is smaller than the input image.
	PaddingMode PaddingMode
}

Overlay is the configure of an overlay image.

func ParseOverlay added in v1.2.0

func ParseOverlay(s string) (*Overlay, error)

ParseOverlay parses an overlay image.

func (Overlay) String

func (o Overlay) String() string

type PaddingMode added in v1.2.0

type PaddingMode int

PaddingMode specifies processing when the specified image is smaller than the input image.

const (
	// PaddingModeDefault makes the part of the image that protrudes from the specified image transparent.
	PaddingModeDefault PaddingMode = 0

	// PaddingModeLeave leaves the overflow area of the specified image as it is.
	PaddingModeLeave PaddingMode = 1
)

type Proxy

type Proxy struct {
	Host string

	// Secret is signing secret.
	Secret string
}

Proxy is a proxy of ImageFlux.

func (*Proxy) Image

func (p *Proxy) Image(path string, config *Config) *Image

Image returns an image served via the proxy.

func (*Proxy) Parse added in v1.2.0

func (p *Proxy) Parse(path string, signature string) (*Image, error)

Parse parses the path and returns the image.

Example
proxy := &imageflux.Proxy{}
image, err := proxy.Parse("/c/w=200/images/1.jpg", "")
if err != nil {
	log.Fatal(err)
}
fmt.Printf("path = %s\n", image.Path)
fmt.Printf("width = %d\n", image.Config.Width)
Output:
path = /images/1.jpg
width = 200
Example (Signed)
proxy := &imageflux.Proxy{
	Secret: "testsigningsecret",
}
image, err := proxy.Parse("/c/w=200/images/1.jpg", "1.tiKX5u2kw6wp9zDgl1tLiOIi8IsoRIBw8fVgVc0yrNg=")
if err != nil {
	log.Fatal(err)
}
fmt.Printf("path = %s\n", image.Path)
fmt.Printf("width = %d\n", image.Config.Width)
Output:
path = /images/1.jpg
width = 200

type Rotate

type Rotate int

Rotate rotates the image.

const (
	// RotateDefault is the default value of Rotate.
	// It is same effect as RotateTopLeft.
	RotateDefault Rotate = 0

	// RotateTopLeft does not anything.
	RotateTopLeft Rotate = 1

	// RotateTopRight flips the image left and right.
	RotateTopRight Rotate = 2

	// RotateBottomRight rotates the image 180 degrees.
	RotateBottomRight Rotate = 3

	// RotateBottomLeft flips the image upside down.
	RotateBottomLeft Rotate = 4

	// RotateLeftTop mirrors the image around the diagonal axis.
	RotateLeftTop Rotate = 5

	// RotateRightTop rotates the image left 90 degrees.
	RotateRightTop Rotate = 6

	// RotateRightBottom rotates the image 180 degrees and mirrors the image around the diagonal axis.
	RotateRightBottom Rotate = 7

	// RotateLeftBottom rotates the image right 90 degrees.
	RotateLeftBottom Rotate = 8

	// RotateAuto parses the Orientation of the Exif information and rotates the image.
	RotateAuto Rotate = -1
)

func (Rotate) String

func (r Rotate) String() string

type Through

type Through int

Through is an image format list for skipping converting.

const (
	// ThroughJPEG skips converting JPEG images.
	ThroughJPEG Through = 1 << iota

	// ThroughPNG skips converting PNG images.
	ThroughPNG

	// ThroughGIF skips converting GIF images.
	ThroughGIF

	// ThroughWebP skips converting WebP images.
	ThroughWebP
)

func (Through) String

func (t Through) String() string

type Unsharp added in v1.1.0

type Unsharp struct {
	Radius    int
	Sigma     float64
	Gain      float64
	Threshold float64
}

Unsharp is an unsharp filter config.

Jump to

Keyboard shortcuts

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