processor

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2023 License: MIT Imports: 1 Imported by: 0

README

Image Processor for Darkroom

About

This module holds the logic to process images. It is used by the Darkroom Application Server.
You may implement the Processor interface to gain custom functionality while still keeping other Darkroom functionality.

Interface
type Processor interface {
	Crop(img image.Image, width, height int, point CropPoint) image.Image
	Decode(data []byte) (image.Image, string, error)
	Encode(img image.Image, format string) ([]byte, error)
	GrayScale(img image.Image) image.Image
	Resize(img image.Image, width, height int) image.Image
	Watermark(base []byte, overlay []byte, opacity uint8) ([]byte, error)
	Flip(image image.Image, mode string) image.Image
	Rotate(image image.Image, angle float64) image.Image
	FixOrientation(image image.Image, orientation int) image.Image
}

Any struct implementing the above interface can be used with Darkroom.

Example
bp := NewBildProcessor()
srcImgData, _ := ioutil.ReadFile("test.png")
srcImg, _, _ := bp.Decode(srcImgData)
outputImg, err := bp.Resize(srcImg, 500, 500)
outputImgData, _ := bp.Encode(outputImg, "png")
_ = ioutil.WriteFile("output.png", outputImgData, 0644)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OverlayAttrs added in v0.0.10

type OverlayAttrs struct {
	Img              []byte
	Point            Point
	WidthPercentage  float64
	HeightPercentage float64
}

type Point added in v0.0.10

type Point int

Point specifies which focus point in the image should be considered while cropping

const (
	// PointTopLeft crops an image with focus point at top-left
	PointTopLeft Point = 1
	// PointTop crops an image with focus point at top
	PointTop Point = 2
	// PointTopRight crops an image with focus point at top-right
	PointTopRight Point = 3
	// PointLeft crops an image with focus point at left
	PointLeft Point = 4
	// PointCenter crops an image with focus point at center
	PointCenter Point = 5
	// PointRight crops an image with focus point at right
	PointRight Point = 6
	// PointBottomLeft crops an image with focus point at bottom-left
	PointBottomLeft Point = 7
	// PointBottom crops an image with focus point at bottom
	PointBottom Point = 8
	// PointBottomRight crops an image with focus point at bottom-right
	PointBottomRight Point = 9

	ExtensionWebP = "webp"
	ExtensionPNG  = "png"
	ExtensionJPG  = "jpg"
	ExtensionJPEG = "jpeg"
)

type Processor

type Processor interface {
	// Crop takes an image.Image, width, height and a Point and returns the cropped image
	Crop(image image.Image, width, height int, point Point) image.Image
	// Resize takes an image.Image, width and height and returns the re-sized image
	Resize(image image.Image, width, height int) image.Image
	// Scale takes an input image, width and height and returns the re-sized
	// image without maintaining the original aspect ratio
	Scale(image image.Image, width, height int) image.Image
	// GrayScale takes an input byte array and returns the grayscaled byte array or error
	GrayScale(image image.Image) image.Image
	// Blur takes an input byte array and returns the blurred byte array by the specified
	// radius(<=1000) or error radius must be larger than 0
	Blur(image image.Image, radius float64) image.Image
	// Watermark takes an input byte array, overlay byte array and opacity value
	// and returns the watermarked image bytes or error
	Watermark(base []byte, overlay []byte, opacity uint8) ([]byte, error)
	// Flip takes an input image and returns the image flipped. The direction of flip
	// is determined by the specified mode - 'v' for a vertical flip, 'h' for a horizontal flip and
	// 'vh'(or 'hv') for both.
	Flip(image image.Image, mode string) image.Image
	// Rotate takes an input image and returns a image rotated by the specified degrees.
	// The rotation is applied clockwise, and fractional angles are supported.
	Rotate(image image.Image, angle float64) image.Image
	// Decode takes a byte array and returns the image, extension, and error
	Decode(data []byte) (img image.Image, format string, err error)
	// Encode takes an image and extension and return the encoded byte array or error
	Encode(img image.Image, format string) ([]byte, error)
	// FixOrientation takes an image and it's EXIF orientation (if exist)
	// and returns the image with its EXIF orientation fixed
	FixOrientation(img image.Image, orientation int) image.Image
	// Overlay takes an input byte array as the base image and
	// an array of OverlayAttrs to be placed as overlays to the base image
	Overlay(base []byte, overlays []*OverlayAttrs) ([]byte, error)
}

Processor interface for performing operations on image bytes

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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