API Reference and Developer Documentation

Specifying a Custom Image Quality

Kraken.io's allows you to override the default lossy optimization and compression offered in so far as you can specify a custom image quality for JPGs, PNGs and even GIF images (animated GIFs included).

While our default "lossy": true mode (without specifying a custom quality) will attempt to arrive at the perfect balance between quality and file weight reduction, some users might wish to specify the final quality level themselves.

In all cases, a value of between 1 and 100 (inclusive) is accepted.

For the JPEG image format, specifying a quality level of 1 will produce the lowest image quality and highest compression level, whereas a quality of 100 will give you the best quality but the least compression. We estimate that JPGs with a quality lower than 25 will, in many cases, be unusable.

Using a custom quality with the PNG and GIF formats work similarly for the most part, however, since both are lossless image formats, a range of techniques including colour palette quantization (reducing the colour palette) and dithering (the use of adjacent pixels of different colors to give the appearance of, or "simulate" a third colour) are used to facilitate the compression. So, a lower quality value will mean fewer colours and more dithering, whereas the highest quality will preserve the most colours and make the least use of dithering.

To use our lossy optimization and compression algorithms simply set the "quality" property in your request JSON, together with the "lossy": true flag as shown below:

{
    "auth": {
        "api_key": "your_api_key",
        "api_secret": "your_api_secret"
    },
    "url": "https://awesome-website.com/images/example.jpg",
    "wait": true,
    "lossy": true,
    "quality": 50
}
<?php

require_once("Kraken.php");

$kraken = new Kraken("your_api_key", "your_api_secret");

$params = array(
    "url" => "https://awesome-website.com/images/grumpy-cat.gif",
    "wait" => true,
    "lossy" => true,
    "quality" => 70

);

$data = $kraken->url($params);

if ($data["success"]) {
    echo "Success. Optimized image URL: " . $data["kraked_url"];
} else {
    echo "Fail. Error message: " . $data["message"];
}
var Kraken = require("kraken");

var kraken = new Kraken({
    "api_key": "your_api_key",
    "api_secret": "your_api_secret"
});

var params = {
    url: "https://example.com/image.png",
    wait: true,
    lossy: true,
    quality: 40
};

kraken.url(params, function(status) {
    if (status.success) {
        console.log("Success. Optimized image URL: %s", status.kraked_url);
    } else {
        console.log("Fail. Error message: %s", status.message);
    }
});
require 'rubygems'
require 'kraken-io'

kraken = Kraken::API.new(
    :api_key => 'your_api_key',
    :api_secret => 'your_api_secret'
)

params = {
    :wait => true,
    :lossy => true,
    :quality => 80
}

data = kraken.url('https://example.com/image.png', params)

if data.success
    puts 'Success! Optimized image URL: ' + data.kraked_url
else
    puts 'Fail. Error message: ' + data.message
end
package main

import (
    "log"
    "github.com/kraken-io/kraken-go"
)

func main() {
    kr, err := kraken.New("your_api_key", "your_api_secret")

    if err != nil {
        log.Fatal(err)
    }

    params := map[string]interface {} {
        "url": "https://example.com/image.png"
        "wait": true,
        "lossy": true,
        "quality": 85
    }

    data, err := kr.URL(params)

    if err != nil {
        log.Fatal(err)
    }

    if data["success"] != true {
        log.Println("Failed, error message ", data["message"])
    } else {
        log.Println("Success, Optimized image URL: ", data["kraked_url"])
    }
}
using Kraken;
using Kraken.Http;
using Kraken.Model;

var connection = Connection.Create("your_api_key", "your_api_secret");
var client = new Client(connection);
var response = client.OptimizeWait(
    new OptimizeWaitRequest(new Uri("https://example.com/image.png"))
    {
        Lossy = true,
        Quality = 75
    }
);

if (response.Result.StatusCode == HttpStatusCode.OK) {
    var url = response.Result.Body.KrakedUrl;
}
from krakenio import Client

api = Client('your_api_key', 'your_api_secret')

data = {
    'wait': True,
    'lossy': True,
    'quality': 50
}

result = api.url('https://example.com/image.png', data);

if result.get('success'):
    print(result.get('kraked_url'))
else:
    print(result.get('message'))

Read more on: