Giter Site home page Giter Site logo

go-thumber's Introduction

go-thumber

go-thumber is a dynamic JPEG thumbnailing proxy designed for speed. It implements JPEG -> JPEG thumbnailing only.

Features:

  • Input: JPEG (YCbCr 4:4:4, 4:4:0, 4:2:2, 4:2:0, and greyscale modes)
  • Output: JPEG (YCbCr 4:4:4 or greyscale)
  • No color conversion: data is kept in direct planar YCbCr buffers for efficiency and quality
  • Optimized JPEG decoding: decodes only as much data as necessary for a particular resolution
  • Uses libswscale for very fast but high quality scaling (lanczos)

Unsupported:

  • RGB or CMYK modes. The input images are assumed to have been transcoded to a sane format.
  • Color-subsampled output. The assumption is that a low-res thumbnail can benefit more from full chroma, so this has not been implemented for simplicity.
  • Progressive decode/buffering. While the JPEG encoded data is streamed to/from the network, currently the entire raw YCbCr image is buffered before and after scaling. This could be changed to work in slices, saving memory.
  • Other image formats
  • Cropping

Dependencies

  • Go 1.3 (needed for http.Client.Timeout and certain cgo features)
  • libswscale (from ffmpeg or libav)
  • libjpeg (preferably libjpeg-turbo)

Build

$ sudo apt-get install libswscale-dev libjpeg-dev
$ mkdir -p "${GOPATH}/src/github.com/pixiv"
$ cd "${GOPATH}/src/github.com/pixiv"
$ git clone <repo URL>
$ go install github.com/pixiv/go-thumber/thumberd

On hardened setups which default to PIC builds, the following flag is required:

$ go install -ldflags '-extldflags=-fno-PIC' github.com/pixiv/go-thumber/thumberd

And the versioning is possible on build-time.

$ go install -ldflags '-X main.version v1.3' github.com/pixiv/go-thumber/thumberd

Usage

thumberd is a FastCGI server. You can also run it as a standalone HTTP server like this:

$ thumberd -local localhost:8080

And then access a URL of the form:

http://localhost:8080/w=128,h=128,a=0,q=95/upstream-host.com/some-image.jpg

Parameters:

w: thumbnail width (required)
h: thumbnail height (required)
q: JPEG quality (default 90)
u: upscale if the source is smaller (default 1)
a: force thumbnail aspect ratio. If 0, keep aspect (default 1)
o: optimize JPEG (default 0)
p: Factor to use when loading downsampled JPEGs. See below for explanation (default 2)

While uncompressing the source JPEG, the JPEG format allows direct loading of a downscaled version (by partially decoding only enough data from the JPEG to reconstruct a lower-resolution version). This built-in downscaling is pretty decent, but not as good as the main lanczos rescaling algorithm used in go-thumber. The factor parameter allows control over how this feature is used.

Setting the factor to 0 disables this feature and always loads the full-resolution JPEG and then downscales it to the target size. Setting the factor to 1 picks the same or higher resolution as the requested target size. 1.5 multiplies the requested size by 1.5, and so on, such that 2 will always load at least twice the resolution required and then downsample it. Values between 0 and 1 make no sense, since they will load an image smaller than requested (but you can try them if you want to see what happens). The JPEG format and libjpeg only allow resolutions of 1/8 through 8/8 (in units of 1/8) of the original resolution, so this defines a lower bound; the actual resolution loaded from the JPEG will usually be rounded up.

In practice, 0 provides the highest quality but is not very efficient, 1 is fastest but noticeably lower quality, and 2 provides practically the same quality as 0 while still being faster when the image is being scaled down to ~40% or less of its original dimensions. For comparison, ImageMagick seems to behave as if p=1.

go-thumber's People

Contributors

cubicdaiya avatar harukasan avatar marcan avatar walf443 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.