SMARTRESIZE API Endpoint

Version 2.197 (Release Notes ↗)

Description

Proportional and intelligent scaling of images with the SMARTRESIZE API, ensuring optimal image quality and performance for developers and creators. Proportional & intelligent scaling of a given image.

HTTP Methods

GET, POST

HTTP Parameters

Required

Fields Type Description
img URL Input media URL. If you want to upload your image directly from your app, then submit a multipart/form-data POST request.
width Integer Desired new width. If this field is missing, then the height field is applied to this one.
height Integer Desired new height. If this field is missing, then the width field is applied to this one.
key String Your PixLab API Key ↗. You can also embed your key in the WWW-Authenticate: HTTP header and omit this parameter if you want to.

Optional

Fields Type Description
blob Boolean By default, this API endpoint return a JSON Object holding the link to the image output. But, if this parameter is set to true then the image binary contents is returned instead.

POST Request Body

This section details the requirements for using a POST request instead of a simple GET request.

Allowed Content-Types:

multipart/form-data
application/json

Use multipart/form-data if you want to upload your media file directly (refer to the REST API code samples or The PixLab Github Repository↗ for a working example). If you are using JSON, then the media file must be already uploaded somewhere. Call store if you want to upload an image for example before invoking this endpoint.

HTTP Response

Fields Type Description
status Integer Status code 200 indicates success, any other code indicates failure.
link URL Link to the media output which is usually stored on the pixlab.xyz storage server unless you set your own S3 keys (refer to your dashboard ↗ on how to do that).
id String Unique media ID.
error String Error message if status != 200.

The API returns application/json if the optional blob parameter is not set.

This endpoint returns a JSON Object after each call only if the optional blob parameter is omitted. Otherwise, the raw media binary content is returned. The JSON response structure is documented above.

Code Samples


import requests

def resize_image(image_url: str, width: int, height: int, api_key: str) -> str:
    """Resize an image using Pixlab API and return the processed image URL."""
    params = {
        'img': image_url,
        'key': api_key,
        'width': width,
        'height': height
    }
    
    try:
        response = requests.get('https://api.pixlab.io/smartresize', params=params, timeout=10)
        response.raise_for_status()
        data = response.json()
        
        if data['status'] != 200:
            raise ValueError(data.get('error', 'Unknown error occurred'))
            
        return data['link']
    
    except requests.exceptions.RequestException as e:
        raise ConnectionError(f"API request failed: {e}") from e
    except (KeyError, ValueError) as e:
        raise ValueError(f"API response error: {e}") from e

if __name__ == "__main__":
    try:
        image_url = "http://7-themes.com/data_images/out/3/6777986-blue-bird-pictures.jpg"
        resized_url = resize_image(image_url, 640, 400, "My_PIXLAB_API_KEY")
        print(f"Resized image location: {resized_url}")
    except Exception as e:
        print(f"Error: {e}")
← Return to API Endpoint Listing