Optimizing local images

https://im2.io/username/Options

If the image you want to optimize is not available on the Internet (e.g. it's on localhost), you can POST the image file to the API instead.

Files are sent using the standard multipart/form-data form format. One image per request. The response will contain the optimized image.

Files sent using this method are not hosted on ImageOptim API servers and have no permanent URLs.

Examples

NodeJS

npm install --save superagent
const request = require('superagent');

request.post('https://im2.io/eX4mp1E4pI/full')
    .attach('file', 'images/example.png')
    .then(function(result){
        result.body // a Buffer that contains the image
    });

PHP

You can use ImageOptim PHP library:

php composer.phar require imageoptim/imageoptim
<?php
require "vendor/autoload.php";
$api = new ImageOptim\API("eX4mp1E4pI");

$imageData = $api->imageFromPath('images/example.png')->getBytes();

Alternatively, if you don't want to use the library, you can make your own request using the curl extension:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://im2.io/eX4mp1E4pI/full");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'file' => curl_file_create('images/example.png'),
]);
$imageData = curl_exec($ch);
curl_close($ch);

Bash/curl

curl -L -F file=@'images/example.png' -o 'optimized.png' 'https://im2.io/eX4mp1E4pI/full'

‹ Back