Scaling

Image Operations / Image Scaling

On this page:
Intro ▾
{{activeSubMenu.text}} ▾

Image scaling allows you to transform the dimensions of your images by a given scale factor. Unlike image resizing, image scaling only accepts a floating point size parameter by which the image will be proportionally transformed. For example, if you'd like to have your images 50% smaller, you would specify 0.5 as a size value.

By default, image scaling will transform images according to their aspect ratio so they'll come out with perfect proportions. You may however, alter the ratio parameter and specify a floating point value for the aspect ratio. The aspect ratio value is the height divided by the width. The default ratio value is 1.0.

{
    "scale": {
        "size": 0.5,
        "ratio": 1.0
    }
}
Given the input image 750px × 400px
Scale it down by 50% by setting "size" to 0.5
The resulting image is 50% of the input image: 375px × 200px

Code Examples for Image Scaling

Below, you'll find examples of how to use Image Scaling from the Pixaven API:

<?php

/**
* Instantiate new `$pix` by calling a constructor
*/

$pix = new Pixaven\Pixaven('your-api-key');


/**
* Provide a publicly available image URL with fetch(string) method
* and scale it down by setting "size" parameter to 0.5
*/

$pix
    ->fetch('https://www.website.com/image.jpg')
    ->scale(array(
        'size' => 0.5
    ))
    ->toJSON(function ($error, $meta) {
        if (!empty($error)) {
            throw new Exception($error);
        }

        /**
        * You'll find the full JSON metadata array within the `$meta` variable.
        * Remember to always check if the `success` property is set to `true`.
        */

        if ($meta['success'] == true) {
            print ($meta['output']['url']);
        } else {
            print ($meta['message']);
        }
    });
/**
* Module dependencies
*/

const Pixaven = require("pixaven");


/**
* Instantiate new `pix` by calling a constructor
*/

const pix = new Pixaven("your-api-key");


/**
* Provide a publicly available image URL with fetch(string) method
* and scale it down by setting "size" parameter to 0.5
*/

pix
    .fetch("https://www.website.com/image.jpg")
    .scale({
        size: 0.5
    })
    .toJSON((err, meta) => {
        if (err) {
            return console.log(err);
        }

        /**
        * You'll find the full JSON metadata hash within the `meta` variable.
        * Remember to always check if the `success` property is set to `true`.
        */

        if (meta.success) {
            console.log(meta.output.url);
        } else {
            console.log(meta.message);
        }
    });
##
# Require dependencies

require "pixaven"


##
# Instantiate new `pix` by calling a constructor

pix = Pixaven.new("your-api-key")


##
# Provide a publicly available image URL with fetch(string) method
# and scale it down by setting "size" parameter to 0.5

err, meta = pix
    .fetch("https://www.website.com/image.jpg")
    .scale(
        size: 0.5
    )
    .to_json

abort(err) if err


##
# You'll find the full JSON metadata hash within the `meta` variable.
# Remember to always check if the `success` property is set to `true`.

if meta["success"]
    puts meta["output"]["url"]
else
    puts meta["message"]
end
package main

import (
    "fmt"
    "github.com/pixaven/pixaven-go"
)

func main() {

    /**
    * Instantiate new `pix` by calling a constructor
    */

    pix, err := pixaven.NewClient("your-api-key")

    if err != nil {
        panic(err)
    }


    /**
    * Provide a publicly available image URL with Fetch(string) method
    * and scale it down by setting "size" parameter to 0.5
    */

    meta, err := pix.
        Fetch("https://www.website.com/image.jpg").
        Scale(pixaven.P{
            "size": 0.5
        }).
        ToJSON()

    if err != nil {
        panic(err)
    }


    /**
    * You'll find the full JSON metadata hash within the `meta` variable.
    * Remember to always check if the `success` property is set to `true`.
    */

    if meta["success"].(map[string]interface{})["success"].(bool) {
        fmt.Println(meta["output"].(map[string]interface{})["url"])
    } else {
        fmt.Println(meta["message"])
    }
}
##
# Import dependencies

from pixaven import pixaven


##
# Instantiate new `client` by calling a constructor

client = pixaven('your-api-key')


##
# Provide a publicly available image URL with fetch(string) method
# and scale it down by setting "size" parameter to 0.5

err, meta = (
    client
        .fetch('https://www.website.com/image.jpg')
        .scale({
            'size': 0.5
        })
        .toJSON()
)

if err is not None:
    raise StandardError(err)


##
# You'll find the full JSON metadata hash within the `meta` variable.
# Remember to always check if the `success` property is set to `true`.

if meta['success'] is True:
    print (meta['output']['url'])
else:
    print (meta['message'])
package com.pixaven.examples;

/**
* Import dependencies
*/

import static com.pixaven.OperationConfiguration.settings;
import com.pixaven.Pixaven;
import com.pixaven.Fetch;
import com.pixaven.Response;

public class Scaling {
    public static void main(String[] args) {

        /**
        * Instantiate new `pix` by calling a constructor
        */

        Pixaven pix = new Pixaven("your-api-key");

        /**
        * Provide a publicly available image URL with fetch(string) method
        * and scale it down by setting "size" parameter to 0.5
        */

        final Fetch fetch = pix.fetch("https://www.website.com/image.jpg");

        fetch.scale(
            settings().set("size", 0.5)
        );

        final Response response = fetch.toJson();

        /**
        * You'll find the full JSON metadata hash within the `Response#getMetadata()` property.
        * Remember to always check if the `Response#successful` property is set to `true`.
        */

        if (response.isSuccessful()) {
            System.out.println(response.getMetadata().getOutput().get("url"));
        } else {
            System.out.println(response.getMessage());
        }
    }
}

We use both proprietary and third-party cookies to improve your browsing experience and to provide you with a better service. By continuing to use this site you consent our use of cookies as described in our Cookie Policy×