comprestack / sdk
Official PHP SDK for the Comprestack image optimization API
Requires
- php: ^8.1
- ext-curl: *
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^10.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Official PHP SDK for the Comprestack image optimization API.
Compress or convert images from Laravel, WordPress, and plain PHP with a small client. Same REST surface as the API docs and OpenAPI.
Install
composer require comprestack/sdk
Requirements: PHP 8.1+ with curl and json extensions.
Create an API key in the dashboard, then set:
export COMPRESTACK_API_KEY=csk_live_…
Security
API keys must stay on the server. Do not put them in themes, public repos, or browser JavaScript.
Quick start
<?php use Comprestack\Client; $client = new Client([ 'apiKey' => getenv('COMPRESTACK_API_KEY'), ]); $job = $client->convert( ['photo.jpg'], 'webp', [ 'quality' => 'balanced', 'maxWidth' => 1600, ], ); file_put_contents('photo.webp', $client->download($job)); echo "Saved {$job['files'][0]['saved_percent']}% · {$job['usage']['remaining']} left\n";
Compress without changing format:
$job = $client->compress(['hero.png'], ['quality' => 'high']);
Laravel tip
Resolve the client in a controller or job — keep COMPRESTACK_API_KEY in .env:
$client = new Client(['apiKey' => config('services.comprestack.key')]);
What you can do
| Goal | Method |
|---|---|
| Shrink, keep format | compress($files, $options) |
| Change format + shrink | convert($files, $format, $options) |
| Re-fetch job metadata | getJob($id) |
| Download ZIP / result | download($job) or download($downloadUrl) |
| Download one file in a batch | downloadFile($jobId, $index) |
| Check quota | usage() |
| White-label billing report | roomReport($roomId) |
Jobs finish in the same request. Results expire after about one hour.
Inputs
| Input | Example |
|---|---|
| File path | 'photo.jpg' |
SplFileInfo |
new SplFileInfo('photo.jpg') |
| Stream resource | fopen('photo.jpg', 'rb') |
| Named payload | ['contents' => $bytes, 'filename' => 'hero.png'] |
| PSR-7 upload / stream | Duck-typed UploadedFileInterface / StreamInterface |
Options
Available on both compress and convert:
| Option | Description |
|---|---|
quality |
'balanced' (default), 'high', 'smallest', or 1–100 on Pro/Business |
format |
convert only — passed as the second argument |
maxWidth / maxHeight |
Downscale to fit (never enlarges) |
target_kb |
Aim for a max file size (Pro/Business) |
lossless |
Pixel-perfect where the codec allows (Pro/Business) |
keep_metadata |
Keep EXIF / color profile (default strips metadata, including GPS) |
Client configuration
$client = new Client([ 'apiKey' => getenv('COMPRESTACK_API_KEY'), // 'baseUrl' => 'https://api.comprestack.com/api/v1', // 'timeout' => 300, // 'maxRetries' => 2, // extra tries on 429 / 503 (honours Retry-After) // 'onProgress' => fn (array $e) => error_log($e['phase'].' '.$e['path']), // 'headers' => ['X-Request-Id' => 'order-4417'], ]);
Errors
Failed requests throw Comprestack\ComprestackException:
use Comprestack\Client; use Comprestack\ComprestackException; try { $client->compress(['huge.tiff']); } catch (ComprestackException $err) { // $err->status, $err->errorCode, $err->getMessage(), $err->retryAfter throw $err; }
Common codes: unauthorized, limit_reached, file_too_large, rate_limited, busy, plan_required.
Links
License
MIT © Comprestack