Guides

Iminify PHP client

Compress images from PHP with the open source Iminify client, with typed errors, a fake for your tests and an optimizer for Spatie's image packages.

Iminify PHP is the PHP client for the Iminify image optimization API. It sends an image, waits for the result and saves the smaller file in a few lines of code, and it covers the rest of the API too: upload groups, page scans, site crawls, weekly site checks, your account and webhook signatures. It also ships an optimizer for Spatie's image packages.

Using Laravel? Iminify Laravel puts this client in your app's container behind an Iminify facade, and plugs Iminify into Spatie's Media Library and laravel-image-optimizer.

What you get

The API works in the background, so every long-running record has a wait() that backs off between reads, waits out a short rate limit and stops at the timeout you give it.

A refusal throws its own exception. A spent daily allowance throws DailyLimitReachedException, a short rate limit RateLimitedException with its $retryAfter, a bad field ValidationException with its $errors, and every one carries the API's error code.

A file goes up through a temporary stream and comes down the same way, so a large one doesn't have to fit in memory. A saved file lands in one step, so a broken transfer leaves whatever was at the path untouched.

For your own tests, FakeClient answers from a queue through the real resources, so your code reads the same objects and catches the same exceptions as it does in production. The client is built on PSR-18, so it uses Guzzle, Symfony's client or whatever your project already has.

Installation

Requires PHP 8.2+ - see all version requirements

Shell
composer require mozex/iminify-php

The client needs a PSR-18 HTTP client and PSR-17 factories, and finds what your project has. A project with neither needs one:

Shell
composer require guzzlehttp/guzzle

You also need an API key: make one under Settings, API Tokens on www.iminify.com. The account needs a verified email address.

Quick start

PHP
use Mozex\Iminify\Iminify;

$client = Iminify::factory()
    ->withApiKey(getenv('IMINIFY_API_KEY'))
    ->withTimeout(300)
    ->make();

$image = $client->images()->upload('photos/team.jpg', ['format' => 'webp']);
$image = $client->images()->wait($image);

if ($image->hasResult()) {
    $client->images()->download($image)->saveTo('photos/team.webp');
}

echo "{$image->status->value}, saved {$image->savedPercent}%";

Iminify::client($key) is the short way to make a client, with no timeout on its requests. Configuration explains why you want one and what else you can set.

Compressions and scans count against the same daily and per-minute limits as the website. Your plan's are on the pricing page, and your account tells you what's left today.

Where to go next

The API's own reference, with every field and error code, is at www.iminify.com/docs.