ImgLess compress image API

Compress image uploaded, return URL of compressed image

7 days Free trial for signed up user


API URL

https:/imgless.com/api/imgless


API Information

HTTP request POST
Response format JSON
Requires authentication? Yes

Request Parameters

Name Required Value Type Description
api_key required string api_key
img_data required binary binary data of image, etc. img_data = open('z.png', 'rb').read()
img_url optional string url of image, one of img_data and img_url must be required
quality optional int the quality of compressed image you want. its range is 0-100, larger is better quality.
default value is 70, which is a good balance between file size and quality.

Response Parameters

Name Value Type Description
error int 0 is success, others means failure
mesage string error message. when failed, it is not empty.
meta dict "insize", file size of input image.
"outsize", file size of compressed image.
"rate", compressed percent.
imglss_url string URL of compressd image saved on ImgLess server.
It's kept on the server for only one hour, please download it ASAP

Example Requests


                        
                        
import requests

api_key = 'your-api-key'
api_url = 'https://imgless.com/api/imgless'


def imgless_compress(file_name, quality=70):
    img_data = open(file_name, 'rb').read()
    print('img_data:', len(img_data))
    post_data = {
        'api_key': api_key,
        'img_data': img_data,
        'quality': quality,
    }
    r = requests.post(api_url, data=post_data)
    return r.json()


if __name__ == '__main__':
    import json
    from sys import argv
    fn = argv[1]
    result = imgless_compress(fn, 75)
    print(json.dumps(result, indent=4))
                        
                        

                        
                        
<?php
$api_key = "your-api-key";
$api_url = "https://imgless.com/api/imgless";

function imgless_compress($img_file_name, $quality=70) {
    global $api_key, $api_url;
    $file = fopen($img_file_name, 'r');
    $size = filesize($img_file_name);
    $img_data = fread($file, $size);
    echo strlen($img_data);
    $post_data = array(
        "api_key" => $api_key,
        "img_data" => $img_data,
        "quality" => $quality
    );
    $post_data = http_build_query($post_data);
    $opts = array(
        "http" => array(
            "method" => "POST",
            "header" => "Content-type: application/x-www-form-urlencoded\r\n" .
                "Content-Length: " . strlen($post_data) . "\r\n",
            "content" => $post_data
        )
    );

    $context = stream_context_create($opts);
    $response = file_get_contents($api_url, false, $context);
    return $response;
}


$response = imgless_compress('zin.png');
echo $response;
?>
                        
                        

                        
                        
require 'net/https'
require 'uri'
$api_key = 'your-api-key'
$api_url = 'https://imgless.com/api/imgless'


def imgless_compress(filename, quality=70)
    afile = File.open(filename, 'rb')
    img_data = afile.read()
    puts "img_data: #{img_data.length}"
    uri = URI($api_url)
    
    req = Net::HTTP::Post.new(uri)
    req.set_form_data('api_key' => $api_key, 'img_data' => img_data, 'quality' => quality)
    res = Net::HTTP.start(uri.hostname, uri.port,
                         :use_ssl => uri.scheme == 'https') do |http|
        http.request(req)
    end

    case res
    when Net::HTTPSuccess
        return res.body
    else 
        res.value
    end
end

res = imgless_compress('zin.png')
puts res
                        
                        

Example Response


                
{
    "meta": {
                "insize": 21527,
                "outsize": 10254,
                "rate": -52.37
    },
    "imgless_url": "/static/works/imgless-2f40a28138a0f4b75e7575c889a3e1ef.png",
    "error": 0,
    "message": ""
}