imgur

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2024 License: MIT Imports: 34 Imported by: 0

README

imgur

Download/Install

install is to run go get -u github.com/cnphpbb/imgur.

github.com/cnphpbb/imgur

examples

package main

import (
	"github.com/cnphpbb/imgur"
	"golang.org/x/image/colornames"
)

func main() {
	i := imgur.LoadFromUrl("https://go.dev/blog/go-brand/logos.jpg").Resize(600, 400)
	imgur.Canvas(760, 1440, colornames.Deepskyblue).
		Insert(i, 80, 1440/3).
		Save("out.png")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSourceImageIsNil          = errors.New("source image is nil")
	ErrSourceNotSupport          = errors.New("source not support")
	ErrSourceStringIsEmpty       = errors.New("source string is empty")
	ErrSourceImageNotSupport     = errors.New("source image not support")
	ErrSaveImageFormatNotSupport = errors.New("save image format not support")
)

Functions

func Color2Hex

func Color2Hex(c color.Color) string

Color2Hex converts a color.Color to its hex string representation.

func GetImageType

func GetImageType(bytes []byte) (ext string, mimetype string, decoder func(r io.Reader) (image.Image, error), err error)

GetImageType returns the extension, mimetype and corresponding decoder function of the image. It judges the image by its first few bytes called magic number.

func Image2RGBA

func Image2RGBA(img image.Image) *image.RGBA

Image2RGBA converts an image to RGBA.

func LoadFontFace

func LoadFontFace(path string, points float64) (font.Face, error)

LoadFontFace is a helper function to load the specified font file with the specified point size. Note that the returned `font.Face` objects are not thread safe and cannot be used in parallel across goroutines. You can usually just use the Context.LoadFontFace function instead of this package-level function.

func Radians

func Radians(degrees float64) float64

Types

type Align

type Align int
const (
	AlignLeft Align = iota
	AlignCenter
	AlignRight
)

type Context

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

func NewContext

func NewContext(width, height int) *Context

NewContext creates a new image.RGBA with the specified width and height and prepares a context for rendering onto that image.

func NewContextForImage

func NewContextForImage(im image.Image) *Context

NewContextForImage copies the specified image into a new image.RGBA and prepares a context for rendering onto that image.

func NewContextForRGBA

func NewContextForRGBA(im *image.RGBA) *Context

NewContextForRGBA prepares a context for rendering onto the specified image. No copy is made.

func (*Context) AsMask

func (dc *Context) AsMask() *image.Alpha

AsMask returns an *image.Alpha representing the alpha channel of this context. This can be useful for advanced clipping operations where you first render the mask geometry and then use it as a mask.

func (*Context) Clear

func (dc *Context) Clear()

Clear fills the entire image with the current color.

func (*Context) ClearPath

func (dc *Context) ClearPath()

ClearPath clears the current path. There is no current point after this operation.

func (*Context) Clip

func (dc *Context) Clip()

Clip updates the clipping region by intersecting the current clipping region with the current path as it would be filled by dc.Fill(). The path is cleared after this operation.

func (*Context) ClipPreserve

func (dc *Context) ClipPreserve()

ClipPreserve updates the clipping region by intersecting the current clipping region with the current path as it would be filled by dc.Fill(). The path is preserved after this operation.

func (*Context) ClosePath

func (dc *Context) ClosePath()

ClosePath adds a line segment from the current point to the beginning of the current subpath. If there is no current point, this is a no-op.

func (*Context) CubicTo

func (dc *Context) CubicTo(x1, y1, x2, y2, x3, y3 float64)

CubicTo adds a cubic bezier curve to the current path starting at the current point. If there is no current point, it first performs MoveTo(x1, y1). Because freetype/raster does not support cubic beziers, this is emulated with many small line segments.

func (*Context) DrawArc

func (dc *Context) DrawArc(x, y, r, angle1, angle2 float64)

func (*Context) DrawCircle

func (dc *Context) DrawCircle(x, y, r float64)

func (*Context) DrawEllipse

func (dc *Context) DrawEllipse(x, y, rx, ry float64)

func (*Context) DrawEllipticalArc

func (dc *Context) DrawEllipticalArc(x, y, rx, ry, angle1, angle2 float64)

func (*Context) DrawImage

func (dc *Context) DrawImage(im image.Image, x, y int)

DrawImage draws the specified image at the specified point.

func (*Context) DrawImageAnchored

func (dc *Context) DrawImageAnchored(im image.Image, x, y int, ax, ay float64)

DrawImageAnchored draws the specified image at the specified anchor point. The anchor point is x - w * ax, y - h * ay, where w, h is the size of the image. Use ax=0.5, ay=0.5 to center the image at the specified point.

func (*Context) DrawLine

func (dc *Context) DrawLine(x1, y1, x2, y2 float64)

func (*Context) DrawPoint

func (dc *Context) DrawPoint(x, y, r float64)

DrawPoint is like DrawCircle but ensures that a circle of the specified size is drawn regardless of the current transformation matrix. The position is still transformed, but not the shape of the point.

func (*Context) DrawRectangle

func (dc *Context) DrawRectangle(x, y, w, h float64)

func (*Context) DrawRegularPolygon

func (dc *Context) DrawRegularPolygon(n int, x, y, r, rotation float64)

func (*Context) DrawRoundedRectangle

func (dc *Context) DrawRoundedRectangle(x, y, w, h, r float64)

func (*Context) DrawString

func (dc *Context) DrawString(s string, x, y float64)

DrawString draws the specified text at the specified point.

func (*Context) DrawStringAnchored

func (dc *Context) DrawStringAnchored(s string, x, y, ax, ay float64)

DrawStringAnchored draws the specified text at the specified anchor point. The anchor point is x - w * ax, y - h * ay, where w, h is the size of the text. Use ax=0.5, ay=0.5 to center the text at the specified point.

func (*Context) DrawStringWrapped

func (dc *Context) DrawStringWrapped(s string, x, y, ax, ay, width, lineSpacing float64, align Align)

DrawStringWrapped word-wraps the specified string to the given max width and then draws it at the specified anchor point using the given line spacing and text alignment.

func (*Context) Fill

func (dc *Context) Fill()

Fill fills the current path with the current color. Open subpaths are implicity closed. The path is cleared after this operation.

func (*Context) FillPreserve

func (dc *Context) FillPreserve()

FillPreserve fills the current path with the current color. Open subpaths are implicity closed. The path is preserved after this operation.

func (*Context) FontHeight

func (dc *Context) FontHeight() float64

func (*Context) GetCurrentPoint

func (dc *Context) GetCurrentPoint() (Point, bool)

GetCurrentPoint will return the current point and if there is a current point. The point will have been transformed by the context's transformation matrix.

func (*Context) Height

func (dc *Context) Height() int

Height returns the height of the image in pixels.

func (*Context) Identity

func (dc *Context) Identity()

Identity resets the current transformation matrix to the identity matrix. This results in no translating, scaling, rotating, or shearing.

func (*Context) Image

func (dc *Context) Image() image.Image

Image returns the image that has been drawn by this context.

func (*Context) InvertMask

func (dc *Context) InvertMask()

InvertMask inverts the alpha values in the current clipping mask such that a fully transparent region becomes fully opaque and vice versa.

func (*Context) InvertY

func (dc *Context) InvertY()

InvertY flips the Y axis so that Y grows from bottom to top and Y=0 is at the bottom of the image.

func (*Context) LineTo

func (dc *Context) LineTo(x, y float64)

LineTo adds a line segment to the current path starting at the current point. If there is no current point, it is equivalent to MoveTo(x, y)

func (*Context) LoadFontFace

func (dc *Context) LoadFontFace(path string, points float64) error

func (*Context) MeasureMultilineString

func (dc *Context) MeasureMultilineString(s string, lineSpacing float64) (width, height float64)

func (*Context) MeasureString

func (dc *Context) MeasureString(s string) (w, h float64)

MeasureString returns the rendered width and height of the specified text given the current font face.

func (*Context) MoveTo

func (dc *Context) MoveTo(x, y float64)

MoveTo starts a new subpath within the current path starting at the specified point.

func (*Context) NewSubPath

func (dc *Context) NewSubPath()

NewSubPath starts a new subpath within the current path. There is no current point after this operation.

func (*Context) Pop

func (dc *Context) Pop()

Pop restores the last saved context state from the stack.

func (*Context) Push

func (dc *Context) Push()

Push saves the current state of the context for later retrieval. These can be nested.

func (*Context) QuadraticTo

func (dc *Context) QuadraticTo(x1, y1, x2, y2 float64)

QuadraticTo adds a quadratic bezier curve to the current path starting at the current point. If there is no current point, it first performs MoveTo(x1, y1)

func (*Context) ResetClip

func (dc *Context) ResetClip()

ResetClip clears the clipping region.

func (*Context) Rotate

func (dc *Context) Rotate(angle float64)

Rotate updates the current matrix with a anticlockwise rotation. Rotation occurs about the origin. Angle is specified in radians.

func (*Context) RotateAbout

func (dc *Context) RotateAbout(angle, x, y float64)

RotateAbout updates the current matrix with a anticlockwise rotation. Rotation occurs about the specified point. Angle is specified in radians.

func (*Context) Scale

func (dc *Context) Scale(x, y float64)

Scale updates the current matrix with a scaling factor. Scaling occurs about the origin.

func (*Context) ScaleAbout

func (dc *Context) ScaleAbout(sx, sy, x, y float64)

ScaleAbout updates the current matrix with a scaling factor. Scaling occurs about the specified point.

func (*Context) SetColor

func (dc *Context) SetColor(c color.Color)

SetColor sets the current color(for both fill and stroke).

func (*Context) SetDash

func (dc *Context) SetDash(dashes ...float64)

SetDash sets the current dash pattern to use. Call with zero arguments to disable dashes. The values specify the lengths of each dash, with alternating on and off lengths.

func (*Context) SetDashOffset

func (dc *Context) SetDashOffset(offset float64)

SetDashOffset sets the initial offset into the dash pattern to use when stroking dashed paths.

func (*Context) SetFillRule

func (dc *Context) SetFillRule(fillRule FillRule)

func (*Context) SetFillRuleEvenOdd

func (dc *Context) SetFillRuleEvenOdd()

func (*Context) SetFillRuleWinding

func (dc *Context) SetFillRuleWinding()

func (*Context) SetFontFace

func (dc *Context) SetFontFace(fontFace font.Face)

func (*Context) SetLineCap

func (dc *Context) SetLineCap(lineCap LineCap)

func (*Context) SetLineCapButt

func (dc *Context) SetLineCapButt()

func (*Context) SetLineCapRound

func (dc *Context) SetLineCapRound()

func (*Context) SetLineCapSquare

func (dc *Context) SetLineCapSquare()

func (*Context) SetLineJoin

func (dc *Context) SetLineJoin(lineJoin LineJoin)

func (*Context) SetLineJoinBevel

func (dc *Context) SetLineJoinBevel()

func (*Context) SetLineJoinRound

func (dc *Context) SetLineJoinRound()

func (*Context) SetLineWidth

func (dc *Context) SetLineWidth(lineWidth float64)

func (*Context) SetMask

func (dc *Context) SetMask(mask *image.Alpha) error

SetMask allows you to directly set the *image.Alpha to be used as a clipping mask. It must be the same size as the context, else an error is returned and the mask is unchanged.

func (*Context) SetPixel

func (dc *Context) SetPixel(x, y int)

SetPixel sets the color of the specified pixel using the current color.

func (*Context) SetRGB

func (dc *Context) SetRGB(r, g, b float64)

SetRGB sets the current color. r, g, b values should be between 0 and 1, inclusive. Alpha will be set to 1 (fully opaque).

func (*Context) SetRGB255

func (dc *Context) SetRGB255(r, g, b int)

SetRGB255 sets the current color. r, g, b values should be between 0 and 255, inclusive. Alpha will be set to 255 (fully opaque).

func (*Context) SetRGBA

func (dc *Context) SetRGBA(r, g, b, a float64)

SetRGBA sets the current color. r, g, b, a values should be between 0 and 1, inclusive.

func (*Context) SetRGBA255

func (dc *Context) SetRGBA255(r, g, b, a int)

SetRGBA255 sets the current color. r, g, b, a values should be between 0 and 255, inclusive.

func (*Context) SetStrokeStyle

func (dc *Context) SetStrokeStyle(pattern Pattern)

SetStrokeStyle sets current stroke style

func (*Context) Shear

func (dc *Context) Shear(x, y float64)

Shear updates the current matrix with a shearing angle. Shearing occurs about the origin.

func (*Context) ShearAbout

func (dc *Context) ShearAbout(sx, sy, x, y float64)

ShearAbout updates the current matrix with a shearing angle. Shearing occurs about the specified point.

func (*Context) Stroke

func (dc *Context) Stroke()

Stroke strokes the current path with the current color, line width, line cap, line join and dash settings. The path is cleared after this operation.

func (*Context) StrokePreserve

func (dc *Context) StrokePreserve()

StrokePreserve strokes the current path with the current color, line width, line cap, line join and dash settings. The path is preserved after this operation.

func (*Context) TransformPoint

func (dc *Context) TransformPoint(x, y float64) (tx, ty float64)

TransformPoint multiplies the specified point by the current matrix, returning a transformed position.

func (*Context) Translate

func (dc *Context) Translate(x, y float64)

Translate updates the current matrix with a translation.

func (*Context) Width

func (dc *Context) Width() int

Width returns the width of the image in pixels.

func (*Context) WordWrap

func (dc *Context) WordWrap(s string, w float64) []string

WordWrap wraps the specified string to the given max width and current font face.

type FillRule

type FillRule int
const (
	FillRuleWinding FillRule = iota
	FillRuleEvenOdd
)

type FlipType

type FlipType int

Flip Type

const (
	Vertical FlipType = iota
	Horizontal
)

type Image

type Image struct {
	Error error
	// contains filtered or unexported fields
}

func Canvas

func Canvas(width, height int, fillColor ...color.Color) *Image

Canvas create a new empty image.

func Load

func Load(source interface{}) *Image

Load an image from source. source can be a file path, a URL, a base64 encoded string, an *os.File, an image.Image or a byte slice.

func LoadFromBase64

func LoadFromBase64(base64Str string) (i *Image)

LoadFromBase64 loads an image from a base64 encoded string.

func LoadFromFile

func LoadFromFile(file *os.File) (i *Image)

LoadFromFile loads an image from a file.

func LoadFromImage

func LoadFromImage(img image.Image) (i *Image)

LoadFromImage loads an image from an instance of image.Image.

func LoadFromImgo

func LoadFromImgo(i *Image) *Image

LoadFromImgo loads an image from an instance of Image.

func LoadFromPath

func LoadFromPath(path string) (i *Image)

LoadFromPath loads an image from a path.

func LoadFromUrl

func LoadFromUrl(url string) (i *Image)

LoadFromUrl loads an image when the source is an url.

func (*Image) Blur

func (i *Image) Blur(ksize int) *Image

Blur returns a blurred image. ksize is filter kernel size, it must be a odd number.

func (*Image) BorderRadius

func (i *Image) BorderRadius(radius float64) *Image

BorderRadius draws rounded corners on the image with given radius.

func (Image) Bounds

func (i Image) Bounds() image.Rectangle

Bounds returns the bounds of the image.

func (*Image) Circle

func (i *Image) Circle(x, y, radius int, c color.Color) *Image

Circle draws a circle at given center coordinate (x, y) with given radius and color.

func (*Image) Crop

func (i *Image) Crop(x, y, width, height int) *Image

Crop Cut out a rectangular part of the current image with given width and height.

func (*Image) Ellipse

func (i *Image) Ellipse(x, y, width, height int, c color.Color) *Image

Ellipse draws an ellipse at given center coordinate (x, y) with given width and height and color.

func (Image) Extension

func (i Image) Extension() string

Extension returns the extension of the image.

func (Image) Filesize

func (i Image) Filesize() int64

Filesize returns the filesize of the image, if instance is initiated from an actual file.

func (*Image) Flip

func (i *Image) Flip(flipType FlipType) *Image

Flip mirror the image vertically or horizontally.

func (*Image) GaussianBlur

func (i *Image) GaussianBlur(ksize int, sigma float64) *Image

GaussianBlur returns a blurred image. ksize is Gaussian kernel size sigma is Gaussian kernel standard deviation.

func (*Image) Grayscale

func (i *Image) Grayscale() *Image

Grayscale converts the image to grayscale.

func (Image) Height

func (i Image) Height() int

Height returns the height of the image.

func (Image) HttpHandler

func (i Image) HttpHandler(w http.ResponseWriter, r *http.Request)

HttpHandler responds the image as an HTTP handler.

func (*Image) Insert

func (i *Image) Insert(source interface{}, x, y int) *Image

Insert inserts source into the image at given (x, y) coordinate. source can be a file path, a URL, a base64 encoded string, an *os.File, an image.Image, a byte slice or an *Image.

func (*Image) Line

func (i *Image) Line(x1, y1, x2, y2 int, c color.Color, width ...int) *Image

Line draws a line from (x1, y1) to (x2, y2) with given color. TODO: width is not working yet.

func (*Image) MainColor

func (i *Image) MainColor() (res color.RGBA)

MainColor returns the main color of the image

func (Image) Mimetype

func (i Image) Mimetype() string

Mimetype returns the mimetype of the image.

func (*Image) Mosaic

func (i *Image) Mosaic(size, x1, y1, x2, y2 int) *Image

Mosaic apply mosaic filter to the image in given rectangle that (x1, y1) and (x2, y2) are the top-left and bottom-right coordinates of the rectangle. size is the size of the pixel.

func (Image) PickColor

func (i Image) PickColor(x, y int) (res color.RGBA)

PickColor returns the color of the pixel at (x, y).

func (*Image) Pixel

func (i *Image) Pixel(x, y int, c color.Color) *Image

Pixel draws a pixel at given (x, y) coordinate with given color.

func (*Image) Pixelate

func (i *Image) Pixelate(size int) *Image

Pixelate apply pixelation filter to the image. size is the size of the pixel.

func (*Image) Rectangle

func (i *Image) Rectangle(x, y, width, height int, c color.Color) *Image

Rectangle draws a rectangle at given coordinate (x, y) with given width and height and color.

func (*Image) Resize

func (i *Image) Resize(width, height int) *Image

Resize resizes the image to the specified width and height.

func (*Image) Rotate

func (i *Image) Rotate(angle int) *Image

Rotate rotates the image clockwise by the specified angle.

func (*Image) Save

func (i *Image) Save(path string, quality ...int) *Image

Save saves the image to the specified path. Only png, jpeg, jpg, tiff and bmp extensions are supported. path is the path the image will be saved to. quality is the quality of the image, between 1 and 100, default is 100, and is only used for jpeg images.

func (Image) String

func (i Image) String() string

String returns the image as a string.

func (*Image) Text

func (i *Image) Text(label string, x, y int, fontPath string, fontColor color.Color, fontSize float64, dpi float64) *Image

Text write a text string to the image at given (x, y) coordinate.

func (*Image) TextWordWrap

func (i *Image) TextWordWrap(label string, x, y float64, fontPath string, fontColor color.Color, fontSize float64, align string) *Image

TextWordWrap write a text string to the image at given (x, y) coordinate.

func (*Image) Thumbnail

func (i *Image) Thumbnail(width, height int) *Image

Thumbnail returns a thumbnail of the image with given width and height.

func (Image) ToBase64

func (i Image) ToBase64() string

ToBase64 returns the base64 encoded string of the image.

func (Image) ToImage

func (i Image) ToImage() image.Image

ToImage returns the instance of image.Image of the image.

func (Image) Width

func (i Image) Width() int

Width returns the width of the image.

type ImageManager

type ImageManager struct {
}

type LineCap

type LineCap int
const (
	LineCapRound LineCap = iota
	LineCapButt
	LineCapSquare
)

type LineJoin

type LineJoin int
const (
	LineJoinRound LineJoin = iota
	LineJoinBevel
)

type Matrix

type Matrix struct {
	XX, YX, XY, YY, X0, Y0 float64
}

func Identity

func Identity() Matrix

func Rotate

func Rotate(angle float64) Matrix

func Scale

func Scale(x, y float64) Matrix

func Shear

func Shear(x, y float64) Matrix

func Translate

func Translate(x, y float64) Matrix

func (Matrix) Multiply

func (a Matrix) Multiply(b Matrix) Matrix

func (Matrix) Rotate

func (a Matrix) Rotate(angle float64) Matrix

func (Matrix) Scale

func (a Matrix) Scale(x, y float64) Matrix

func (Matrix) Shear

func (a Matrix) Shear(x, y float64) Matrix

func (Matrix) TransformPoint

func (a Matrix) TransformPoint(x, y float64) (tx, ty float64)

func (Matrix) TransformVector

func (a Matrix) TransformVector(x, y float64) (tx, ty float64)

func (Matrix) Translate

func (a Matrix) Translate(x, y float64) Matrix

type Pattern

type Pattern interface {
	ColorAt(x, y int) color.Color
}

func NewSolidPattern

func NewSolidPattern(color color.Color) Pattern

func NewSurfacePattern

func NewSurfacePattern(im image.Image, op RepeatOp) Pattern

type Point

type Point struct {
	X, Y float64
}

func CubicBezier

func CubicBezier(x0, y0, x1, y1, x2, y2, x3, y3 float64) []Point

func QuadraticBezier

func QuadraticBezier(x0, y0, x1, y1, x2, y2 float64) []Point

func (Point) Distance

func (a Point) Distance(b Point) float64

func (Point) Fixed

func (a Point) Fixed() fixed.Point26_6

func (Point) Interpolate

func (a Point) Interpolate(b Point, t float64) Point

type Radius

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

func (*Radius) At

func (c *Radius) At(x, y int) color.Color

func (*Radius) Bounds

func (c *Radius) Bounds() image.Rectangle

func (*Radius) ColorModel

func (c *Radius) ColorModel() color.Model

type RepeatOp

type RepeatOp int
const (
	RepeatBoth RepeatOp = iota
	RepeatX
	RepeatY
	RepeatNone
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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