Documentation ¶
Overview ¶
github.com/disintegration/imaging
Example ¶
package main import ( "fmt" "io" "log" "github.com/sunshineplan/imgconv" ) func main() { // Open a test image. src, err := imgconv.Open("testdata/video-001.png") if err != nil { log.Fatalf("failed to open image: %v", err) } // Resize the image to width = 200px preserving the aspect ratio. mark := imgconv.Resize(src, &imgconv.ResizeOption{Width: 200}) // Add random watermark set opacity = 128. dst := imgconv.Watermark(src, &imgconv.WatermarkOption{Mark: mark, Opacity: 128, Random: true}) // Write the resulting image as TIFF. if err := imgconv.Write(io.Discard, dst, &imgconv.FormatOption{Format: imgconv.TIFF}); err != nil { log.Fatalf("failed to write image: %v", err) } // Split the image into 3 parts horizontally. imgs, err := imgconv.Split(src, 3, imgconv.SplitHorizontalMode) if err != nil { log.Fatalf("failed to split image: %v", err) } fmt.Print(len(imgs)) }
Output: 3
Index ¶
- func Decode(r io.Reader, opts ...DecodeOption) (image.Image, error)
- func DecodeConfig(r io.Reader) (image.Config, string, error)
- func Open(file string, opts ...DecodeOption) (image.Image, error)
- func Resize(base image.Image, option *ResizeOption) image.Image
- func Save(output string, base image.Image, option *FormatOption) error
- func Split(base image.Image, n int, mode SplitMode) (imgs []image.Image, err error)
- func SplitHorizontal(base image.Image, n int) ([]image.Image, error)
- func SplitVertical(base image.Image, n int) ([]image.Image, error)
- func ToGray(img image.Image) image.Image
- func Watermark(base image.Image, option *WatermarkOption) image.Image
- func Write(w io.Writer, base image.Image, option *FormatOption) error
- type DecodeOption
- type EncodeOption
- func BackgroundColor(color color.Color) EncodeOption
- func GIFDrawer(drawer draw.Drawer) EncodeOption
- func GIFNumColors(numColors int) EncodeOption
- func GIFQuantizer(quantizer draw.Quantizer) EncodeOption
- func PNGCompressionLevel(level png.CompressionLevel) EncodeOption
- func Quality(quality int) EncodeOption
- func TIFFCompressionType(compressionType TIFFCompression) EncodeOption
- type Format
- type FormatOption
- type Options
- func (opts *Options) Convert(w io.Writer, base image.Image) error
- func (opts *Options) ConvertExt(filename string) string
- func (opts *Options) SetFormat(f Format, options ...EncodeOption) *Options
- func (opts *Options) SetGray(gray bool) *Options
- func (opts *Options) SetResize(width, height int, percent float64) *Options
- func (opts *Options) SetWatermark(mark image.Image, opacity uint) *Options
- type ResizeOption
- type SplitMode
- type TIFFCompression
- type WatermarkOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode reads an image from r. If want to use custom image format packages which were registered in image package, please make sure these custom packages imported before importing imgconv package.
func DecodeConfig ¶
DecodeConfig decodes the color model and dimensions of an image that has been encoded in a registered format. The string returned is the format name used during format registration.
func Open ¶
func Open(file string, opts ...DecodeOption) (image.Image, error)
Open loads an image from file.
func Save ¶
func Save(output string, base image.Image, option *FormatOption) error
Save saves image according format option
func Split ¶ added in v1.1.11
Split splits an image into n smaller images based on the specified split mode. If n is less than 1, or the image cannot be split, it returns an error.
func SplitHorizontal ¶ added in v1.1.11
SplitHorizontal splits an image into n parts horizontally.
func SplitVertical ¶ added in v1.1.11
SplitVertical splits an image into n parts vertically.
Types ¶
type DecodeOption ¶ added in v1.0.7
type DecodeOption func(*decodeConfig)
DecodeOption sets an optional parameter for the Decode and Open functions.
func AutoOrientation ¶ added in v1.0.7
func AutoOrientation(enabled bool) DecodeOption
AutoOrientation returns a DecodeOption that sets the auto-orientation mode. If auto-orientation is enabled, the image will be transformed after decoding according to the EXIF orientation tag (if present). By default it's enabled.
type EncodeOption ¶
type EncodeOption func(*encodeConfig)
EncodeOption sets an optional parameter for the Encode and Save functions. https://github.com/disintegration/imaging
func BackgroundColor ¶ added in v1.1.1
func BackgroundColor(color color.Color) EncodeOption
BackgroundColor returns an EncodeOption that sets the background color.
func GIFDrawer ¶
func GIFDrawer(drawer draw.Drawer) EncodeOption
GIFDrawer returns an EncodeOption that sets the drawer that is used to convert the source image to the desired palette of the GIF-encoded image.
func GIFNumColors ¶
func GIFNumColors(numColors int) EncodeOption
GIFNumColors returns an EncodeOption that sets the maximum number of colors used in the GIF-encoded image. It ranges from 1 to 256. Default is 256.
func GIFQuantizer ¶
func GIFQuantizer(quantizer draw.Quantizer) EncodeOption
GIFQuantizer returns an EncodeOption that sets the quantizer that is used to produce a palette of the GIF-encoded image.
func PNGCompressionLevel ¶
func PNGCompressionLevel(level png.CompressionLevel) EncodeOption
PNGCompressionLevel returns an EncodeOption that sets the compression level of the PNG-encoded image. Default is png.DefaultCompression.
func Quality ¶
func Quality(quality int) EncodeOption
Quality returns an EncodeOption that sets the output JPEG or PDF quality. Quality ranges from 1 to 100 inclusive, higher is better.
func TIFFCompressionType ¶
func TIFFCompressionType(compressionType TIFFCompression) EncodeOption
TIFFCompressionType returns an EncodeOption that sets the compression type of the TIFF-encoded image. Default is tiff.Deflate.
type Format ¶
type Format int
Format is an image file format.
func FormatFromExtension ¶
FormatFromExtension parses image format from filename extension: "jpg" (or "jpeg"), "png", "gif", "tif" (or "tiff"), "bmp" and "pdf" are supported.
func (Format) MarshalText ¶ added in v1.1.8
func (*Format) UnmarshalText ¶ added in v1.1.8
type FormatOption ¶
type FormatOption struct { Format Format EncodeOption []EncodeOption }
FormatOption is format option
type Options ¶
type Options struct { Watermark *WatermarkOption Resize *ResizeOption Format *FormatOption Gray bool }
Options represents options that can be used to configure a image operation.
func (*Options) ConvertExt ¶
ConvertExt convert filename's ext according image format.
func (*Options) SetFormat ¶
func (opts *Options) SetFormat(f Format, options ...EncodeOption) *Options
SetFormat sets the value for the Format field.
type ResizeOption ¶
ResizeOption is resize option
type SplitMode ¶ added in v1.1.11
type SplitMode int
SplitMode defines the mode in which the image will be split
type TIFFCompression ¶ added in v1.0.2
type TIFFCompression int
TIFFCompression describes the type of compression used in Options.
const ( TIFFUncompressed TIFFCompression = iota TIFFDeflate )
Constants for supported TIFF compression types.
func (TIFFCompression) MarshalText ¶ added in v1.1.8
func (c TIFFCompression) MarshalText() (b []byte, err error)
func (*TIFFCompression) UnmarshalText ¶ added in v1.1.8
func (c *TIFFCompression) UnmarshalText(text []byte) error
type WatermarkOption ¶
WatermarkOption is watermark option
func (*WatermarkOption) SetOffset ¶
func (w *WatermarkOption) SetOffset(offset image.Point) *WatermarkOption
SetOffset sets the option for the Watermark offset base center when adding fixed watermark.
func (*WatermarkOption) SetRandom ¶
func (w *WatermarkOption) SetRandom(random bool) *WatermarkOption
SetRandom sets the option for the Watermark position random or not.