5 Best Image Optimization APIs (with PHP Examples)

If you run a website with lots of images, you already know how much they impact performance.
High-quality visuals make your site look great, but they can also slow it down, hurting SEO, user experience, and conversions. That’s where image optimization APIs come in.
These APIs can automatically compress, resize, and convert images to modern formats (like WebP or AVIF), so you get faster page loads without losing visual quality.
Let’s look at a few popular options, how to use them, and why they’re worth integrating into your workflow.
ShortPixel API
ShortPixel API is one of the most developer-friendly image optimization services. It supports lossy, glossy, and lossless compression, plus it can convert images to WebP or AVIF.
Key features
- Proprietary SmartCompress technology
- Compress images using Lossy, Glossy, or Lossless modes
- Resize images automatically by width and height
- Convert to modern formats like WebP or AVIF
- Optionally remove backgrounds or preserve EXIF data
- Works with both URLs and file uploads (via POST Reducer API)
PHP example
<?php
$URL = "https://api.shortpixel.com/v2/reducer.php";
$APIKey = "YOUR_API_KEY";
$images = ["https://example.com/image1.jpg", "https://example.com/image2.png"];
$data = json_encode([
"plugin_version" => "MY123",
"key" => $APIKey,
"lossy" => 1, // 1=Lossy, 2=Glossy, 0=Lossless
"resize" => 1,
"resize_width" => 1024,
"resize_height" => 1024,
"convertto" => "+webp",
"urllist" => $images
]);
$options = [
'http' => [
'method' => 'POST',
'header' => "Content-Type: application/json\r\nAccept: application/json\r\n",
'content' => $data
]
];
$context = stream_context_create($options);
$response = file_get_contents($URL, false, $context);
$result = json_decode($response, true);
if (isset($result[0]['Status']['Code']) && $result[0]['Status']['Code'] == 2) {
echo "Optimized image URL: " . $result[0]['LossyURL'];
} else {
echo "Optimization pending or failed.";
}
?>
TinyPNG API
TinyPNG API (also known as Tinify API) provides an easy-to-use REST service for compressing and optimizing images. It supports AVIF, WebP, JPEG, and PNG, and can also handle resizing, format conversion, and metadata preservation.
Key features
- Option to save optimized images directly to Amazon S3 or Google Cloud Storage
- Smart lossy compression for AVIF, WebP, PNG, and JPEG
- Resize and crop with methods like fit, cover, and thumb
- Convert between image formats (e.g., PNG → WebP)
- Preserve important metadata, such as copyright or GPS
PHP example
<?php
// Install via composer: composer require tinify/tinify
require_once("vendor/autoload.php");
\Tinify\setKey("YOUR_API_KEY");
// Compress a local image
$source = \Tinify\fromFile("unoptimized.jpg");
$source->toFile("optimized.jpg");
// Or compress and resize
$resized = \Tinify\fromFile("unoptimized.jpg")->resize([
"method" => "fit",
"width" => 800,
"height" => 600
]);
$resized->toFile("optimized_resized.jpg");
?>
Imagify API
Imagify offers a simple and powerful way to optimize and resize images through its API. It supports multiple compression levels and resizing options, making it easy to integrate image optimization into any PHP workflow.
Key features
- Three compression levels: normal, aggressive, and ultra
- Optional image resizing (width, height, or percentage)
- Option to keep EXIF metadata
- Quick integration via Composer (
wp-media/imagify-php)
<?php
require('class-imagify.php');
// Initialize Imagify with your API key
$imagify = new Imagify\Optimizer('YOUR_API_KEY');
// Set optimization parameters
$params = array(
'level' => 'ultra', // compression level: normal, aggressive, or ultra
'resize' => array('width' => 800) // resize image to 800px width
);
// Optimize image
$image = 'example.jpg';
$result = $imagify->optimize($image, $params);
// Display results
if ($result['success']) {
echo "Optimized image URL: " . $result['image'];
echo "\nSaved: " . $result['percent'] . "%";
} else {
echo "Optimization failed (code: " . $result['code'] . ")";
}
?>
Kraken.io API
Kraken.io API provides a powerful image optimization API that supports JPEG, PNG, WebP (including animated), GIF, SVG, AVIF, HEIC, and PDF formats.
It allows developers to compress and resize images efficiently through HTTPS requests. The API offers both lossy and lossless optimization, making it suitable for web applications, automated image workflows, or deployment pipelines.
Key features
- Supports both lossy and lossless compression
- Image resizing with different fit strategies
- Option to preserve metadata and auto-correct orientation
- Can send results via callback URL for async processing
- Integration with cloud storage such as Amazon S3 or Google Cloud
Example:
<?php
require_once("Kraken.php");
$kraken = new Kraken("YOUR_API_KEY", "YOUR_API_SECRET");
$params = array(
"wait" => true,
"lossy" => true,
"resize" => array(
"width" => 800,
"height" => 600,
"strategy" => "fit"
),
"file" => "/path/to/image.jpg"
);
$data = $kraken->upload($params);
if ($data["success"]) {
echo "Optimized image URL: " . $data["kraked_url"];
} else {
echo "Error: " . $data["message"];
}
?>
Cloudinary API
Cloudinary API is one of the most complete image and video optimization platforms available. It works as a cloud-based API that manages your entire media workflow, from uploading and transforming to optimizing and delivering assets across a global CDN.
Unlike simple compression services, Cloudinary gives developers full control over media delivery using smart automation and on-the-fly transformations.
Key features
- Smart image optimization using
f_autoandq_autoto automatically choose the best format and compression level for each device. - On-demand resizing, cropping, rotation, overlays, and filters through simple URL parameters or SDK methods.
- Global CDN delivery
- Seamless integration via SDKs in PHP, Node.js, Python, Ruby, and more.
- Asset management tools to organize, tag, and analyze images and videos in one place.
PHP example
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Cloudinary\Configuration\Configuration;
use Cloudinary\Cloudinary;
use Cloudinary\Transformation\Resize;
use Cloudinary\Transformation\Quality;
use Cloudinary\Transformation\Format;
// Configure Cloudinary globally
Configuration::instance([
'cloud' => [
'cloud_name' => 'your_cloud_name',
'api_key' => 'your_api_key',
'api_secret' => 'your_api_secret'
],
'url' => [
'secure' => true
]
]);
// Initialize a Cloudinary instance
$cloudinary = new Cloudinary();
// Generate an optimized image URL
$optimizedUrl = $cloudinary->image('sample.jpg')
->resize(Resize::scale()->width(800))
->delivery(Quality::auto())
->delivery(Format::auto())
->toUrl();
echo "Optimized image URL: " . $optimizedUrl;