Documentation
¶
Overview ¶
Package image reads, modifies, encodes, and serves images in Laravel for Go applications. It brings Intervention Image to Laravel for Go in pure Go.
Index ¶
- Variables
- func Response(img *Image, format Format, opts ...Option) *web.Response
- type Alignment
- type Bezier
- type Border
- type Circle
- type Color
- type Config
- type Direction
- type Ellipse
- type Encoded
- type Font
- type Format
- type Frame
- type Image
- func (img *Image) At(x, y int) color.Color
- func (img *Image) Background() Color
- func (img *Image) Blur(level int) *Image
- func (img *Image) Bounds() stdimage.Rectangle
- func (img *Image) Brightness(level int) *Image
- func (img *Image) Clone() *Image
- func (img *Image) ColorAt(x, y int) Color
- func (img *Image) ColorModel() color.Model
- func (img *Image) Colorize(red, green, blue int) *Image
- func (img *Image) Contain(width, height int, position Position) *Image
- func (img *Image) ContainDown(width, height int, position Position) *Image
- func (img *Image) Contrast(level int) *Image
- func (img *Image) Cover(width, height int, position Position) *Image
- func (img *Image) CoverDown(width, height int, position Position) *Image
- func (img *Image) Crop(width, height int, position Position) *Image
- func (img *Image) Draw(shapes ...Shape) *Image
- func (img *Image) DrawPixel(x, y int, value string) *Image
- func (img *Image) Encode(format Format, opts ...Option) (*Encoded, error)
- func (img *Image) Err() error
- func (img *Image) Exif() map[string]any
- func (img *Image) Fill(value string) *Image
- func (img *Image) FillAt(value string, x, y int) *Image
- func (img *Image) FillTransparentAreas() *Image
- func (img *Image) Flip(direction Direction) *Image
- func (img *Image) Frames() []Frame
- func (img *Image) Gamma(gamma float64) *Image
- func (img *Image) Grayscale() *Image
- func (img *Image) Height() int
- func (img *Image) Insert(source *Image, position Position) *Image
- func (img *Image) InsertWithOpacity(source *Image, position Position, opacity int) *Image
- func (img *Image) Invert() *Image
- func (img *Image) IsAnimated() bool
- func (img *Image) Loops() int
- func (img *Image) Orient() *Image
- func (img *Image) Origin() Origin
- func (img *Image) Pixelate(size int) *Image
- func (img *Image) ReduceColors(limit int) *Image
- func (img *Image) RemoveAnimation(index int) *Image
- func (img *Image) Resize(width, height int) *Image
- func (img *Image) ResizeCanvas(width, height int, position Position) *Image
- func (img *Image) ResizeCanvasRelative(width, height int, position Position) *Image
- func (img *Image) ResizeDown(width, height int) *Image
- func (img *Image) Rotate(angle float64) *Image
- func (img *Image) Save(path string, opts ...Option) error
- func (img *Image) Scale(width, height int) *Image
- func (img *Image) ScaleDown(width, height int) *Image
- func (img *Image) SetBackground(value string) *Image
- func (img *Image) SetLoops(loops int) *Image
- func (img *Image) Sharpen(level int) *Image
- func (img *Image) Size() stdimage.Point
- func (img *Image) SliceAnimation(offset, length int) *Image
- func (img *Image) Store(ctx context.Context, disk *storage.Disk, path string, opts ...Option) error
- func (img *Image) Text(text string, x, y int, font Font) *Image
- func (img *Image) Trim(tolerance int) *Image
- func (img *Image) Width() int
- type Line
- type Manager
- func (m *Manager) Base64(encoded string) (*Image, error)
- func (m *Manager) Canvas(width, height int) *Image
- func (m *Manager) Config() Config
- func (m *Manager) DataURI(uri string) (*Image, error)
- func (m *Manager) Decode(data []byte) (*Image, error)
- func (m *Manager) FromImage(source stdimage.Image) *Image
- func (m *Manager) Load(ctx context.Context, disk *storage.Disk, path string) (*Image, error)
- func (m *Manager) Open(path string) (*Image, error)
- func (m *Manager) Read(r io.Reader) (*Image, error)
- func (m *Manager) Register(app *laravel.Registrar) error
- func (m *Manager) Upload(upload *web.Upload) (*Image, error)
- type Option
- type Origin
- type Point
- type Polygon
- type Position
- type Rectangle
- type Shape
Constants ¶
This section is empty.
Variables ¶
var ( // ErrDecoder is the error for data that isn't an image this package can // decode. Handlers that pass it to web.Fail answer 422. ErrDecoder error = &statusError{"image: the data isn't a decodable image", http.StatusUnprocessableEntity} // ErrNotSupported is the error for a format this package can't decode or // encode, such as AVIF. Handlers that pass it to web.Fail answer 415. ErrNotSupported error = &statusError{"image: the format isn't supported", http.StatusUnsupportedMediaType} )
Functions ¶
func Response ¶
Response encodes img and returns it as a 200 OK *web.Response with the format's Content-Type. Format "" keeps the format the image was decoded from, or JPEG for an image created in code:
return image.Response(img, image.WEBP, image.Quality(65))
When encoding fails, the response renders the error as web.Fail does.
Types ¶
type Alignment ¶
type Alignment int
Alignment names the point of an image that another image or area lines up with, such as its top-left corner or its center. The zero Alignment is Center, and values other than the named constants act as Center.
The alignments, from the image's center to its corners and edges.
func ParseAlignment ¶
ParseAlignment reads an alignment's name, such as "top-left", "top_left", "left-top", or "center-top", in any case. "", "center", and "middle" are Center.
type Bezier ¶
Bezier is a Bézier curve: three points draw a quadratic curve, four a cubic one, and more a curve of higher degree. The border draws the curve, and the background fills the area between it and the line that joins its ends.
type Border ¶
Border is the outline of a shape: its color and its width in pixels, 1 when left at 0.
type Circle ¶
Circle is a circle centered on the pixel at X and Y. Its border is drawn centered on its outline.
type Color ¶
type Color struct {
R, G, B, A uint8
}
Color is a color with straight, non-premultiplied alpha. It implements color.Color.
func ParseColor ¶
ParseColor reads a color written as hex (f00, #ff0000, #ff000080), rgb() or rgba(), hsl() or hsla(), a CSS color name, or transparent.
type Config ¶
type Config struct {
// AutoOrientation rotates decoded images by their Exif orientation.
// IMAGE_AUTO_ORIENTATION sets it, and it is on unless set.
AutoOrientation bool
// DecodeAnimation keeps every frame of animated images instead of the first.
// IMAGE_DECODE_ANIMATION sets it, and it is on unless set.
DecodeAnimation bool
// BackgroundColor fills new areas and replaces transparency in formats
// without it. IMAGE_BACKGROUND_COLOR sets it, and it is ffffff unless set.
BackgroundColor string
}
Config is the package's settings, applied to every image the Manager reads or creates.
type Ellipse ¶
Ellipse is an ellipse centered on the pixel at X and Y. Its border is drawn centered on its outline.
type Encoded ¶
type Encoded struct {
// contains filtered or unexported fields
}
Encoded is an image encoded in a format, ready to send or store.
type Font ¶
type Font struct {
// File is a TrueType or OpenType font file, or "" for Go Regular.
File string
// Size is the font size in pixels, 12 when left at 0, up to 4096.
Size float64
// Color is the text color, written as ParseColor reads it, black when
// left at "".
Color string
// Align is the point of the text block that lines up with the position
// given to Text, such as image.TopLeft. It also aligns the lines of the
// block: left, centered, or right.
Align Alignment
// LineHeight is the distance between baselines as a multiple of Size,
// 1.25 when left at 0.
LineHeight float64
// Wrap is the widest a line may be in pixels before it breaks between
// words, or 0 to break only at newlines.
Wrap int
}
Font describes how Image.Text writes text. Its zero value writes 12 pixel black Go Regular centered on the point.
type Format ¶
type Format string
Format is an image file format. The zero Format stands for the format an image was decoded from.
const ( JPEG Format = "jpeg" PNG Format = "png" GIF Format = "gif" WEBP Format = "webp" BMP Format = "bmp" TIFF Format = "tiff" AVIF Format = "avif" HEIC Format = "heic" )
The formats this package knows. AVIF and HEIC are recognized but can't be decoded or encoded in pure Go.
func FormatOf ¶
FormatOf returns the format named by a format, file extension, media type, or file path, such as "webp", ".jpg", "image/png", or "photos/cat.gif".
type Image ¶
type Image struct {
// contains filtered or unexported fields
}
Image is an image with one frame, or several for an animation. Its modifiers change it in place and return it so calls chain, and the first error a modifier meets is kept and returned by Image.Err. An Image implements the standard library's image.Image with its first frame. Get images from a Manager; the zero Image has no pixels and isn't usable.
func (*Image) Background ¶
Background returns the color that fills new areas and replaces transparency in formats without it.
func (*Image) Blur ¶
Blur softens the image with a Gaussian blur applied level times, from 0 to 100.
func (*Image) Brightness ¶
Brightness lightens the image by level percent, from -100, which turns it black, to 100, which turns it white.
func (*Image) Clone ¶
Clone returns a copy of the image that changes independently of it, with its frames, Exif tags, background, and error.
func (*Image) ColorAt ¶
ColorAt returns the color of the first frame's pixel at x and y, or transparent outside the image.
func (*Image) ColorModel ¶
ColorModel returns the color model of the image's pixels.
func (*Image) Colorize ¶
Colorize shifts the image's red, green, and blue channels by -100 to 100 percent each.
func (*Image) Contain ¶
Contain scales the image to fit within width and height, and places it at position on a canvas of exactly width and height filled with the image's background color.
func (*Image) ContainDown ¶
ContainDown places the image like Image.Contain, but never makes it larger than it is.
func (*Image) Contrast ¶
Contrast raises the difference between light and dark by level percent, from -100, which turns the image an even gray, to 100.
func (*Image) Cover ¶
Cover crops the largest area with the aspect ratio of width and height at position, and scales it to exactly width and height. An offset slides the area, but never past the image's edges.
func (*Image) CoverDown ¶
CoverDown crops like Image.Cover, but never makes the cropped area larger than it is.
func (*Image) Crop ¶
Crop cuts an area of width and height out of the image at position, such as image.TopLeft.Offset(10, 20). An offset moves away from the edges the alignment names, so image.BottomRight.Offset(10, 10) ends 10 pixels left of and above the bottom-right corner. Parts of the area outside the image get the image's background color.
func (*Image) Draw ¶
Draw draws the shapes on every frame, in order, blending translucent colors over the image:
img.Draw(image.Rectangle{X: 10, Y: 10, Width: 200, Height: 50, Background: "#f005"})
func (*Image) DrawPixel ¶
DrawPixel blends a color, written as ParseColor reads it, over the pixel at x and y on every frame. A pixel outside the image is skipped.
func (*Image) Encode ¶
Encode encodes the image in format, or in the format it was decoded from when format is "", or as JPEG for an image created in code. The format may also be any name FormatOf reads, such as "jpg". Formats other than GIF encode only the first frame of an animation. AVIF and HEIC return an error that matches ErrNotSupported.
func (*Image) Exif ¶
Exif returns the image's Exif tags keyed by section and name, such as IFD0.Orientation or EXIF.DateTimeOriginal. Numbers are ints, and text and fractions are strings.
func (*Image) Fill ¶
Fill paints the whole image with a color written as ParseColor reads it, blending a translucent color over the image.
func (*Image) FillAt ¶
FillAt paints a color, written as ParseColor reads it, over the area around x and y that shares its pixel's color, as a paint bucket does.
func (*Image) FillTransparentAreas ¶
FillTransparentAreas fills the image's transparent and translucent pixels with its background color.
func (*Image) Flip ¶
Flip mirrors the image across direction: image.Horizontal swaps its left and right, and image.Vertical its top and bottom.
func (*Image) Gamma ¶
Gamma corrects the image's gamma: values above 1 lighten its midtones and values below 1 darken them.
func (*Image) Grayscale ¶
Grayscale turns the image's colors into shades of gray by their luminance, truncated as GD does.
func (*Image) Insert ¶
Insert draws the first frame of source over every frame of the image at position, such as image.BottomRight.Offset(10, 10) for a watermark.
func (*Image) InsertWithOpacity ¶
InsertWithOpacity inserts source like Image.Insert, at opacity percent from 0, which leaves the image as it is, to 100.
func (*Image) Invert ¶
Invert replaces the image's colors with their opposites, keeping their transparency.
func (*Image) IsAnimated ¶
IsAnimated reports whether the image has more than one frame.
func (*Image) Loops ¶
Loops returns how often an animation repeats, as image/gif's LoopCount does: 0 repeats forever and -1 plays it once.
func (*Image) Orient ¶
Orient turns the image upright by its Exif orientation and sets IFD0.Orientation to 1, so calling it again changes nothing. Decoding already orients images unless Config AutoOrientation is off.
func (*Image) Pixelate ¶
Pixelate replaces each size by size block of the image, from its top-left corner, with the block's average color.
func (*Image) ReduceColors ¶
ReduceColors limits each frame to at most limit colors, from 1 to 256, keeping transparency. Frames with fewer colors stay as they are.
func (*Image) RemoveAnimation ¶
RemoveAnimation keeps only the frame at index, counting from 0, so the image is no longer animated.
func (*Image) Resize ¶
Resize changes the image's size to width and height, stretching it. A width or height of 0 keeps that side as it is.
func (*Image) ResizeCanvas ¶
ResizeCanvas changes the image's size to width and height without scaling it, placing it at position. New areas get the image's background color. A width or height of 0 keeps that side as it is.
func (*Image) ResizeCanvasRelative ¶
ResizeCanvasRelative grows the image's canvas by width and height, or shrinks it for negative values, placing the image at position. New areas get the image's background color.
func (*Image) ResizeDown ¶
ResizeDown changes the image's size like Image.Resize, but never makes a side larger than it is.
func (*Image) Rotate ¶
Rotate turns the image clockwise by angle degrees, or counterclockwise for a negative angle. Quarter turns move pixels exactly; other angles grow the image to hold the turned image and fill the corners with the background color.
func (*Image) Save ¶
Save encodes the image in the format of path's extension and writes it to path. When path is "", it writes to the file the image was opened from, in that file's extension's format or else the format it was decoded from.
func (*Image) Scale ¶
Scale changes the image's size and keeps its aspect ratio. Given one side and 0 for the other, it scales to that side; given both, it scales to fit within width and height.
func (*Image) ScaleDown ¶
ScaleDown scales the image like Image.Scale, but never makes it larger than it is.
func (*Image) SetBackground ¶
SetBackground changes the image's background color, written as ParseColor reads it.
func (*Image) SetLoops ¶
SetLoops sets how often an animation repeats, as Image.Loops reports it: 0 repeats forever and -1 plays it once.
func (*Image) SliceAnimation ¶
SliceAnimation keeps length frames from offset, counting from 0, or every frame from offset when length is 0. A length past the last frame stops there.
func (*Image) Store ¶
Store encodes the image in the format of path's extension and writes it to path on a storage disk, such as storage.From(r) for the default disk.
func (*Image) Text ¶
Text writes text on every frame, aligning the text block to x and y by the font's Align. Newlines, including \r\n, start new lines. To place text in a corner, align it to that corner:
img.Text("© Laravel", img.Width()-10, img.Height()-10, image.Font{Size: 14, Color: "fff", Align: image.BottomRight})
func (*Image) Trim ¶
Trim removes the borders that match the average color of the image's corners. Tolerance is how far, from 0 to 100 percent of the largest RGBA distance, a color may differ from the corners' color and still be trimmed; 0 trims only near-identical colors, and 100 trims every color, leaving a 1x1 transparent image. Animations can't be trimmed.
type Line ¶
Line is a straight line through the centers of two pixels, Width pixels wide, 1 when left at 0, in Color, black when left at "". Its ends are square and cover both end pixels.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager reads and creates images with the application's Config. Install it with laravel.Options.Packages and reach it with From:
laravel.Options{
Packages: []laravel.Package{image.New()},
}
func New ¶
New returns the package. Each configure function changes the settings that the package read from the environment. Outside an application, the manager uses the defaults changed by configure.
func (*Manager) DataURI ¶
DataURI reads an image from a data URI, such as data:image/png;base64,iVBORw0KGgo...
func (*Manager) Decode ¶
Decode reads an image from its encoded bytes, such as a JPEG or PNG file's contents. Data that isn't an image returns an error that matches ErrDecoder, and AVIF or HEIC data one that matches ErrNotSupported.
func (*Manager) Load ¶
Load reads the image at path on a storage disk, such as storage.From(r) for the default disk.
func (*Manager) Open ¶
Open reads the image file at path. A missing file returns an error that matches fs.ErrNotExist.
type Origin ¶
type Origin struct {
// Format is the format the image was decoded from, or "" for an image
// created in code.
Format Format
// Path is the file the image was opened from, or "" for other sources.
Path string
}
Origin describes where an image was decoded from.
type Polygon ¶
Polygon is a closed shape through three or more points. A polygon that crosses itself is filled by the nonzero rule, so a pentagram's center is filled.
type Position ¶
type Position interface {
// contains filtered or unexported methods
}
Position places an area inside another: an Alignment alone, such as image.Center, or an alignment with an offset, such as image.BottomRight.Offset(10, 10). A nil Position is Center.
type Rectangle ¶
Rectangle is a rectangle whose top-left corner is at X and Y. Its border is drawn inside it.
type Shape ¶
type Shape interface {
// contains filtered or unexported methods
}
Shape is something Image.Draw draws. The set is closed: Rectangle, Ellipse, Circle, Line, Polygon, and Bezier, passed by value. Colors are written as ParseColor reads them, and an empty background or border color draws nothing.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
codec
Package codec holds the format work the image package builds on: sniffing formats by their leading bytes and turning GIF frames into full frames and back.
|
Package codec holds the format work the image package builds on: sniffing formats by their leading bytes and turning GIF frames into full frames and back. |
|
colors
Package colors parses the color notations the image package accepts.
|
Package colors parses the color notations the image package accepts. |
|
exif
Package exif reads Exif tags from JPEG and TIFF data.
|
Package exif reads Exif tags from JPEG and TIFF data. |
|
geometry
Package geometry computes the sizes and anchor points that resizing, cropping, and placing images use.
|
Package geometry computes the sizes and anchor points that resizing, cropping, and placing images use. |
|
jpeg
Package jpeg implements a JPEG image decoder and encoder, as defined in ITU-T T.81: https://www.w3.org/Graphics/JPEG/itu-t81.pdf.
|
Package jpeg implements a JPEG image decoder and encoder, as defined in ITU-T T.81: https://www.w3.org/Graphics/JPEG/itu-t81.pdf. |
|
png
Package png implements a PNG image decoder, as defined at https://www.w3.org/TR/PNG/.
|
Package png implements a PNG image decoder, as defined at https://www.w3.org/TR/PNG/. |
|
raster
Package raster changes the pixels of NRGBA frames: converting, copying, filling, flattening, scaling, orienting, and finding trim bounds.
|
Package raster changes the pixels of NRGBA frames: converting, copying, filling, flattening, scaling, orienting, and finding trim bounds. |
|
typography
Package typography loads fonts and lays out and draws blocks of text.
|
Package typography loads fonts and lays out and draws blocks of text. |