NAV Navbar
  • API
  • API

    Basics

    URL format

    The TweenPics API is URL-based and every URL is structured as follows:

    https://i.tween.pics/v1/[<manipulation>][/auth:<anthentication>]/<source image>

    where:

    Here are two basic examples:

    Error reporting

    Whenever something goes wrong, TweenPics will issue an HTTP error with a sensible status code and a short explanation in plain text as a body.

    TweenPics will error when:

    Whenever an image does not show in your web page, be sure to check the network tab of your favorite dev tool for those red-colored lines indicative of a network error.

    Manipulations

    As seen previously, a manipulation is a list of transformations that are chained together using the character /. There is no limit to the number of transformations you can chain, save for the limit in size of a URL as enforced by your browser. No matter how complex the manipulation, TweenPics will optimize it for speed and accuracy on the fly.

    Transformations all have the same structure:

    <name>=<parameters>

    where:

    For instance:

    Types of parameters

    The TweenPics API strives to be as consistent as possible and, as such, transformations will use the same format for parameters that represent the same underlying concept.

    number

    Tweenpics numbers can be JSON-encoded number literals or expressions that, when computed, result in an actual number. Expressions are embedded in parenthesis. Operators +, -, * and / are supported for additions, subtractions, multiplications and divisions respectively. Classic algebraic precedence is respected and parenthesis can be used to circumvent it.

    For instance, 50, 7.2, (1/3), (5*(7+2)/3) are all valid numbers.

    length

    TweenPics lengths are equivalent to CSS length values. They consist of a number eventually followed by a unit specifier:

    coordinates

    Coordinates represent a point in an image, specified as a couple of positive lengths separated by the character x:

    TweenPics uses the same coordinate system as CSS: zero-based, left-to-right and top-to-bottom.

    If we take the example of an image that is 640 pixel-wide and 480 pixel-high:

    It is perfectly fine to mix lengths of different units in the same coordinates. For instance, in the context of yet another 640 per 480 sample image, coordinates 100x50p actually translate to 100x240.

    size

    A size represents a 2D area, specified as a couple of strictly positive lengths separated by the character x:

    For instance 640x480 is 640 pixel-wide per 480 pixel-high.

    It is possible to omit one of the dimensions using the character -. In that case, TweenPics will automatically compute the missing dimension so that the size respects the aspect ratio of the source image. For instance, if the source image is 640 pixel-wide per 480 pixel-high, then sizes 320x- and -x240 are both equivalent to 320x240.

    As a shortcut, it is possible to omit the height by specifying just a width. For instance, the size 320 is equivalent to 320x-.

    As for coordinates, it is perfectly fine to mix lengths of different units in the same size. As an example, the size 10px150 is a perfectly valid.

    crop size

    A crop size is a size where omitted dimensions are assumed to be the same as the input image.

    For instance:

    ratio

    A ratio represents the proportional relationship between a width and a height. It is specified as a couple of strictly positive numbers separated by the character ::

    For instance, the ratio 9:3 indicates that the width is 9-units long while the height is 3-units long. In terms of proportions, the width is (9/3=)3 times longer than the height.

    Focus point

    Transformations behave differently depending on which point in the image is the main focus. TweenPics will do its best to keep this focus point as central as possible within the transformed image.

    By default, the focus point is in the middle of the image but you can change its coordinates by using the focus transformation.

    Focus window

    The biggest extractable area of the image that has the focus point as its center is called the focus window. By default, since the focus point is right in the middle of the image, the focus window is actually the image itself.

    Here is, delimited in red, the focus window of a 300 pixel-wide per 188 pixel-high cat face with the focus point set at 85x85:

    Cat focus window

    Chaining transformations

    When adding a transformation to the chain, the parameters given are interpreted as if previous transformations had already been performed (ie. as if the source image was the result of the previous transformations).

    For instance:

    Since TweenPics will optimize the manipulation, be aware that a transformation may shadow what came before it. For instance resize=50p/resize=340 will result in an image that is 340 pixel-wide: TweenPics will simply ignore the first resize.

    Transformations

    contain

    Syntax: contain=<size>

    contain behaves like the CSS background size "contain". It will resize the image so that it completely fits inside the target area while conserving the original aspect ratio. The resulting image will be smaller than a target size which aspect ratio is not the same as the aspect ratio of the input.

    For instance, applying contain=150x100 to our beloved face of a cat will result in the following 150x94 image:

    Result
    contain=150x100 result

    contain-max

    Syntax: contain-max=<pixel size>

    A conditional version of contain that will be applied only when one of the given lengths is larger than the corresponding input image dimension.

    contain-min

    Syntax: contain-min=<pixel size>

    A conditional version of contain that will be applied only when one of the given lengths is smaller than the corresponding input image dimension.

    cover

    Syntax: cover=<size|ratio>

    cover behaves like the CSS background size "cover". It will resize the image so that it completely fills the target area while conserving the original aspect ratio. If some parts of the image end up outside of the covered area, they are cropped.

    So, a cover=100x100 of a 300x188 image would first scale the image down to a height of 100 pixels and then crop along the x-axis as demonstrated below:

    Process Result
    cover=100x100 process cover=100x100 result

    cover will use the focus point as a guide and will crop the image so that the focus point is as central as possible in the resulting image. For instance, focus=85x85/cover=100x100 will behave as follows:

    Process Result
    focus=85x85/cover=100x100 process focus=85x85/cover=100x100 result

    When a ratio is provided, cover will extract the biggest possible area contained within the focus window that satisfies the ratio and is centered on the focus point. For instance, focus=85x85/cover=2:1 will behave as follows:

    Process Result
    focus=85x85/cover=2:1 process focus=85x85/cover=2:1 result

    cover-max

    Syntax: cover-max=<pixel size>

    A conditional version of cover that will be applied only when one of the given lengths is larger than the corresponding input image dimension.

    cover-min

    Syntax: cover-min=<pixel size>

    A conditional version of cover that will be applied only when one of the given lengths is smaller than the corresponding input image dimension.

    crop

    Syntax: crop=<crop size>[,<coordinates>]

    crop will extract a zone from the image which size is the given crop size. If no coordinates are given, the focus point will be used as a guide to determine where to start the extraction. If coordinates are given, they will be used to determine the top-left pixel from which to start the extraction.

    Here are two examples:

    Manipulation Process Result
    crop=200x100 crop=200x100 process crop=200x100 process
    crop=200x100,20x50 crop=200x100,20x50 process crop=200x100,20x50 result

    focus

    Syntax: focus=<coordinates>

    focus will set the focus point coordinates, incidentally changing the focus window. It doesn't modify the output image in any way but will change the behavior of further transformations that take the focus point into account (namely cover, crop and resize).

    format

    Syntax: format=<format expression>

    Specifies the output format. Only the last format in the manipulation expression is taken into account. By default, TweenPics will "smart-guess" the best format for the browser currently requesting the image but you can use format to override this behavior.

    Available formats are:

    max

    Syntax: max=<pixel size>

    Alias of contain-max.

    min

    Syntax: min=<pixel size>

    Alias of contain-min.

    resize

    Syntax: resize=<size|ratio>

    resize will resize the image to the specified size. If only one length is provided, the other dimension will be determined so as to respect the aspect ratio of the input image. If both lengths are provided, the aspect ratio may not be respected.

    Here are three resize operations on the same source image:

    Manipulation Result
    resize=100 resize=100 result
    resize=-x100 resize=-x100 result
    resize=100x100 resize=100x100 result

    If a ratio is provided, the focus window will be resized so that it stays as close as possible to its current surface (i.e number of pixels) while respecting the given aspect ratio.

    For instance, focus=85x85/resize=4:3 will behave as follows:

    Process Result
    focus=85x85/resize=4:3 process focus=85x85/resize=4:3 result

    The 170x170 focus window was resized to a 196x147 image (respecting the 4:3 ratio). The initial surface was (170x170=) 28,900 pixels. The final surface is (196x147=) 28,812 pixels.

    resize-max

    Syntax: resize-max=<pixel size>

    A conditional version of resize that will applied only when one of the given lengths is larger than the corresponding input image dimension.

    resize-min

    Syntax: resize-min=<pixel size>

    A conditional version of resize that will be applied only when one of the given lengths is smaller than the corresponding input image dimension.

    step

    Syntax: step=<pixel size>

    step will round the image width and/or height to the closest multiples of the corresponding pixel lengths. If only one length is provided, the other dimension will be determined so as to respect the aspect ratio of the input image. If both lengths are provided, the aspect ratio may not be respected.

    Let's take the example of a 206 pixel-wide per 103 pixel-high image (aspect ratio 2:1):