sqip

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2019 License: MIT Imports: 17 Imported by: 0

README


Image of a blurred gopher
SQIP does SVG-based LQIP image creation

… because even blurred preview images need to look good :godmode:

GoDoc Go Report Card

Overview

SQIP is a go implementation of Tobias Baldauf‘s SVG-based LQIP technique.

LQIP (Low Quality Image Placeholders) boils down to this:

  • load the page initially with low quality images
  • once the page loaded (e.g. in the onload event), replace them with full quality images

So instead of waiting for the final image to be rendered, we can serve a highly compressed image first, and then switch to the large one.

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.

Tobias Baldauf‘s project is written in js and depends on an installed version of node, go, primitive and multiple npm modules. This project aims to minimize the dependencies down to exactly one — this go package.

Installation

Get the cli app directly to your $GOPATH/bin with

go get -u github.com/denisbrodbeck/sqip/cmd/sqip

Import the library with

import "github.com/denisbrodbeck/sqip"

CLI usage

# Generate a SVG placeholder and print an example <img> tag to stdout
sqip input.png

# Save the placeholder SVG to a file instead of printing the <img> to stdout
sqip -o output.svg input.png

# Customize the number of primitive SVG shapes (default=8) to influence bytesize or level of detail
sqip -n 4 input.jpg

All available flags:

sqip [-n <int>] [-o <path>] [options...] <file>
Flags:
  -n  <int>     number of primitive SVG shapes (default: 8)
  -o  <path>    save the placeholder SVG to a file (default: empty)
Options:
  -mode  <int>  shape type (default: 0)
  -alpha <int>  color alpha (use 0 to let the algorithm choose alpha for each shape) (default: 128)
  -bg    <hex>  background color as hex (default: avg)

API usage

Here is an example app:

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)
}

Credits

The Go gopher was created by Denis Brodbeck, based on original artwork from Renee French and Takuya Ueda.

License

The MIT License (MIT) — Denis Brodbeck. Please have a look at the LICENSE for more details.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Blur

func Blur(inSVG string, width, height int) (outSVG string, err error)

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

func ImageWidthAndHeight(input image.Image) (width, height int)

ImageWidthAndHeight returns the width and height of input image.

func LoadImage

func LoadImage(path string) (image.Image, error)

LoadImage takes a path and returns the content as an image.

func Minify

func Minify(in string) (out string, err error)

Minify takes a svg and returns a minified version of the input.

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

func Refit(in string, width int, height int) (out string)

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.

func RunLoaded added in v0.7.0

func RunLoaded(image image.Image, workSize, count, mode, alpha, repeat, workers int, background string) (out string, width, height int, err error)

RunLoaded takes an already loaded image and config properties to generate an SVG LQIP

func SaveFile

func SaveFile(path, content string) error

SaveFile creates the named file with mode 0666 (before umask), truncating it if it already exists, and writes the content to it.

Types

type StringBase64

type StringBase64 string

StringBase64 is a base64 encoded string.

func Base64

func Base64(src string) StringBase64

Base64 returns the base64 encoding of src.

Directories

Path Synopsis
cmd
sqip
Package main provides the command line app for SVG-based LQIP image creation.
Package main provides the command line app for SVG-based LQIP image creation.

Jump to

Keyboard shortcuts

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