npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

super-preview

v1.0.2

Published

Client for compressing and assembling images when creating tiny preview images

Downloads

4

Readme

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:

  1. Scale down the image to a 42x42 pixel image (adding a white area on the side depending on the aspect ratio)
  2. Compress it using a predefined JPEG quantization table
  3. 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.
  4. 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 = require('super-preview-client');
var client = new SuperPreviewClient();

client.compress(
    fs.readFileSync('my-image.jpeg'),
    function(err, compressed) {
        /**
         * compressed contains:
         * {
         *     compressed: '...', // data
         *     width: 1234, // original image width
         *     height: 645, // original image height
         * }
         */
    }
);

Assembling a compressed image

var SuperPreviewClient = require('super-preview-client');
var client = new SuperPreviewClient();

// imageData = { base64: '...' };
var imageData = client.assemble(this.props.compressed);

// Exposing in a react component
() => (
    <img style={{
        height: imageData.height,
        width: imageData.width,
        background: `url(data:image/jpeg;base64,${ imageData.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 [email protected]

Licensed under the MIT License