Documentation
¶
Overview ¶
Package sqip allows SVG-based LQIP image creation
https://github.com/denisbrodbeck/sqip https://godoc.org/github.com/denisbrodbeck/sqip/cmd/sqip
This package is a go implementation of Tobias Baldauf‘s SVG-based LQIP technique (see https://github.com/technopagan/sqip).
SQIP is an evolution of the classic LQIP technique: it makes use of Primitive to generate a SVG consisting of several simple shapes that approximate the main features visible inside the image, optimizes the SVG using minify and adds a Gaussian Blur filter to it. This produces a SVG placeholder which weighs in at only ~800-1000 bytes, looks smooth on all screens and provides an visual cue of image contents to come.
Example ¶
package main import ( "log" "runtime" "github.com/denisbrodbeck/sqip" ) func main() { in := "path/to/image.png" // input file out := "path/to/image.svg" // output file workSize := 256 // large images get resized to this - higher size grants no boons count := 8 // number of primitive SVG shapes mode := 1 // shape type alpha := 128 // alpha value repeat := 0 // add N extra shapes each iteration with reduced search (mostly good for beziers) workers := runtime.NumCPU() // number of parallel workers background := "" // background color (hex) // create a primitive svg svg, width, height, err := sqip.Run(in, workSize, count, mode, alpha, repeat, workers, background) if err != nil { log.Fatal(err) } // save svg to file if err := sqip.SaveFile(out, svg); err != nil { log.Fatal(err) } // create example img tag tag := sqip.ImageTag(out, sqip.Base64(svg), width, height) log.Print(tag) }
Output:
Index ¶
- func Blur(inSVG string, width, height int) (outSVG string, err error)
- func ImageTag(filename string, svg StringBase64, width, height int) string
- func ImageWidthAndHeight(input image.Image) (width, height int)
- func LoadImage(path string) (image.Image, error)
- func Minify(in string) (out string, err error)
- func Primitive(input image.Image, ...) (svg string, err error)
- func Refit(in string, width int, height int) (out string)
- func Run(file string, workSize, count, mode, alpha, repeat, workers int, ...) (out string, width, height int, err error)
- func RunLoaded(image image.Image, workSize, count, mode, alpha, repeat, workers int, ...) (out string, width, height int, err error)
- func SaveFile(path, content string) error
- type StringBase64
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Blur ¶
Blur adds viewbox and preserveAspectRatio attributes as well as a Gaussian Blur filter to the SVG. When no group is found, add group (element with blur applied) using patchSVGGroup().
func ImageTag ¶
func ImageTag(filename string, svg StringBase64, width, height int) string
ImageTag creates an example <img> tag.
func ImageWidthAndHeight ¶
ImageWidthAndHeight returns the width and height of input image.
func Primitive ¶
func Primitive(input image.Image, workSize, outputSize, count, mode, alpha, repeat, workers int, background string) (svg string, err error)
Primitive generates a SVG consisting of several simple shapes that approximate the main features visible inside the image.
All primitive generation related flags are supported. See https://github.com/fogleman/primitive for a list of supported flags and their meaning in respect to the output.
You should seed your rng in your main function, before calling this function.
Try:
rand.Seed(time.Now().UTC().UnixNano())
func Refit ¶ added in v0.7.0
Refit adjusts the width of the background to exactly match the output size This will prevent rounding errors causing the aspect ratio to become incorrect This must be called before minify
func Run ¶
func Run(file string, workSize, count, mode, alpha, repeat, workers int, background string) (out string, width, height int, err error)
Run takes a file and primitve related config properties and creates a SVG-based LQIP image.