# HTTP Thumbnailer Stateless HTTP API server for image thumbnailing, editing and format conversion. It is using [ImageMagick](http://www.imagemagick.org) or [GraphicsMagick](http://www.graphicsmagick.org) via [RMagick](http://rmagick.rubyforge.org) gem as the image processing library. ## Features * thumbnailing images with different aspect ratio keeping methods * applying image edits like rotate, crop, blur, pixelate, etc. * identification of image format and size * support of many input and output formats * efficient API for generating multiple thumbnails from single input image with just one request * completely stateless * many image scaling and loading performance optimizations * efficient memory usage * memory limits and disk memory offloading support * custom plugin support * based on [Unicorn HTTP server](http://unicorn.bogomips.org) with UNIX socket communication support ## Changelog ### 1.3.0 * added edits support * added performance statistics * added plugin support * switches to control optimizations * using downsampling by default when image gets upscaled on load ### 1.2.0 * added `float-x` and `float-y` option support * added `interlace` option support * syslog logging * transaction ID tracking ### 1.1.0 * added identification API for image mime type and size identification * stripping user meta data from input image further reducing output image size * providing image size headers for input image and all generated thumbnails * X-Input-Image-Content-Type header is now deprecated in favour of X-Input-Image-Content-Type * not using [ImageMagick](http://www.imagemagick.org) for input image mime type resolution since it is accessing disk and behaves very inefficiently ## Installing You will need the following system packages installed: `imagemagick`, `pkg-config` and `make`. For PNG support install `libpng`. You may want to consult [ImageMagick](http://www.imagemagick.org) installation guide for more information on supported formats and required libraries. For Arch Linux you can use this commands: ```bash pacman -S imagemagick pacman -S libpng pacman -S pkg-config pacman -S make ``` Then you can install the gem as usual: ```bash gem install httpthumbnailer ``` Optionally install Ruby client library and tool: ```bash gem install httpthumbnailer-client ``` ## Usage ### Getting started ```bash # install httpthumbnailer (see above) # install httpthumbnailer-client gem install httpthumbnailer-client # start thumbnailing server in foreground (to stop hit Ctlr-C) httpthumbnailer --foreground --verbose # in another console thumbnail to standard output cat ~/Pictures/compute.jpg | httpthumbnailer-client -t crop,100,200,png > thumbnail.png # generate multiple thumbnails cat ~/Pictures/compute.jpg | httpthumbnailer-client -t crop,100,200,jpeg,quality:100 -t pad,200,200,png thumbnail1.jpg thumbnail2.png # applay edits to the image and thumbnail - pixelate middle of the image and draw blue rectangle at the bottom of it cat ~/Pictures/compute.jpg | httpthumbnailer-client -t 'fit,280,280,png!pixelate,0.3,0.2,0.4,0.4!rectangle,0.04,0.8,0.92,0.17,color:blue' > thumbnail.png ``` ### Ruby API client In this example we use [httpthumbnailer-client](http://github.com/jpastuszek/httpthumbnailer-client) gem CLI tool that will use HTTP API of the server to generate thumbnails. ### Running the server HTTP Thumbnailer uses worker based server model thanks to [Unicorn HTTP server](http://unicorn.bogomips.org) gem. To start the thumbnailer use `httpthumbnailer` command. By default it will start in background and will spawn CPU core number + 1 number of worker processes. It will be listening on **localhost** port **3100**. To start in foreground with verbose output use `httpthumbnailer --verbose --foreground`. To see available switches and options use `httpthumbnailer --help`. When running in background the master server process will store it's PID in `httpthumbnailer.pid` file. You can change pid file location with `--pid-file` option. If running as root you can use `--user` option to specify user with whose privileges the worker processes will be running. ### Logging `httpthumbnailer` logs to `httpthumbnailer.log` file in current directory by default. You can change log file location with `--log-file` option and verbosity with `--verbose` or `--debug` switch. Additionally `httpthumbnailer` will log requests in [common NCSA format](http://en.wikipedia.org/wiki/Common_Log_Format) to `httpthumbnailer_access.log` file. Use `--access-log-file` option to change location of access log. Syslog logging can be enabled with `--syslog-facility` option followed by name of syslog facility to use. When enabled log files are not created and both application logs and access logs are sent to syslog. Access logs will gain meta information that will include `"type":"http-access"` that can be used to filter access log entries out from application log entries. With `--xid-header` option name of HTTP request header can be specified. Value of this header will be logged in meta information tag `xid` along side all request related log entries. Using `--perf-stats` switch will enable logging of performance statistics. This log entries will be logged with `"type":"perf-stats"`. ### Thumbnailing methods For method you can use one of the following value: * `fit` - fit image within given dimensions keeping aspect ratio * `crop` - cut image to fit within given dimensions keeping aspect ratio * `pad` - fit resize image and pad image with background colour to given dimensions keeping aspect ratio * `limit` - fit resize image to given dimensions if it is larger than that dimensions ### Supported formats List of supported formats can be displayed with `httpthumbnailer --formats`. Optionally format `input` can be used to use the same thumbnail format as input image. ### Thumbnail width and height Width and height values are in pixels and are interpreted depending on method used. `input` string can be used for width and/or height to use input image width or height. ### Thumbnail options Following options can be used with thumbnail specification: * `quality` - set output image quality; this is format specific: for JPEG 0 is maximum compression and 100 is maximum quality, for PNG first digit is zlib compression level and second one is filter level * `background-color` - color in HTML notation or textual description ('red', 'green' etc.) used for background when processing transparent images or padding; by default white background is used * `float-x` and `float-y` - value between 0.0 and 1.0; can be used with `crop` and `pad` methods to move cropping view or image over background left to right or top to bottom (0.0 to 1.0); both default to 0.5 centering the view or image * `interlace` - one of `UndefinedInterlace`, `NoInterlace`, `LineInterlace`, `PlaneInterlace`, `PartitionInterlace`, `GIFInterlace`, `JPEGInterlace`, `PNGInterlace`; some formats support interlaced output format; use `JPEGInterlace` or `LineInterlace` or `PlaneInterlace` with `jpeg` format to produce progressive JPEG; defaults to `NoInterlace` ### Edits Edits are applied on input image (after possibly being downsampled) and before the final thumbnail is generated. Relative vector values are relative to input image dimensions (width and hight), scalar values are relative to input image diagonal. This way edits will look more or less the same no matter what the input or output image resolution is. Also client does not need to know the resolution of input image. One or more edits can be used with thumbnail specification: * `resize_crop` - cut image to fit within given dimensions keeping aspect ratio * arguments: width, height - dimensions to resize and crop image to in pixels * options: * `float-x`, `float-y` - value between 0.0 and 1.0; move cropping view left to right or top to bottom (0.0 to 1.0); default: 0.5 (center) * `resize_fit` - fit image within given dimensions keeping aspect ratio * arguments: width, height in pixels * `resize_limit` - same as `resize_fit` but applied only if image is larger than given dimensions * arguments: width, height - dimension to limit image to in pixels * `crop` * arguments: x, y, width, height - values between 0.0 and 1.0; region of image starting from relative position to image width from left (x) and height from top (y) with relative width and height * `pixelate` - pixelate (sample) given region of image * arguments: box_x, box_y, box_widthd, box_height - values between 0.0 and 1.0; region of image starting from relative position to image width from left (x) and height from top (y) with relative width and height * options: * `size` - size of the pixel (relative to imgae diagonal); default: 0.01 * `blur` - blur region of image * arguments: x, y, width, height - values between 0.0 and 1.0; region of image starting from relative position to image width from left (x) and height from top (y) with relative width and height * options: * `sigma` - amount of blur (relative to imgae diagonal); resulting value is capped to 50 pixels * `radius` - radius of the blur (0.0 - calculated for given sigma) (relative to image diagonal); resulting value is capped to 50 pixels; default: 0.0 * `rectangle` - draw rectangle over image * arguments: box_x, box_y, box_width, box_height - values between 0.0 and 1.0; rectangle over image starting from relative position to image width from left (x) and height from top (y) with relative width and height * options: * `color` - color of the rectangle; default: black * `rotate` - rotate image clockwise by given angle filling any new image surface with color * arguments: angle - rotation angle in degree * options: * `background-color` - color of the background (when not 90x rotation is used); default: thumbnail `background-color` option or transparent ## API ### Single thumbnail API To generate single thumbnail send input image with **PUT** request to URI in format: /thumbnail/,,,[,