Super preview
Super preview takes the work out of creating beautify, tiny thumbnails that's small enough to be embedded in responses, which can in turn be easily assembled to a proper image on the client.
This contains both compression and image assembler, and can be used client or server side.
Inspiration
The package was inspired by a blog post on the Facebook Code blog explaining the tech behind their tiny, blurry and beautify cover photos.
How does it work?
In a nutshell it does the following on the server side:
- Scale down the image to a 42x42 pixel image (adding a white area on the side depending on the aspect ratio)
- Compress it using a predefined JPEG quantization table
- Strip away image headers, huffman tables and other data that will be the same because we're using the same Q-table for all images.
- Prepend the width and height of the image.
The buffer returned from the compress()
function is small enough to be returned inline in markup or as part of an API response.
Example
Compressing an image
var SuperPreviewClient = ;var client = ; client;
Assembling a compressed image
var SuperPreviewClient = ;var client = ; // imageData = { base64: '...' };var imageData = client; // Exposing in a react component <img style= height: imageDataheight width: imageDatawidth background: `url(data:image/jpeg;base64,)` />
Dependencies
Compressing images
Super preview depends on the gm
(GraphicsMagick) package when scaling images.
Assembling images
Assembling images has no other dependencies than lodash.merge
, and will work both on the client and server.
Response format
The assemble()
function takes care of parsing the data string returned from the server. The underlying data is composed from three different segments, structured in the following way:
42:33:<image data as hexadecimal values>
License
Copyright (c) 2015-2016, Kristoffer Brabrand kristoffer@brabrand.no
Licensed under the MIT License