kraken-io/kraken-php is a PHP client library that provides a convenient way to interact with the Kraken.io image optimization and manipulation API. Kraken.io is a powerful, cloud-based service designed to reduce the file size of images without sacrificing quality, thereby improving website performance and user experience.
The `kraken-php` library allows PHP applications to integrate seamlessly with the Kraken.io service, enabling functionalities such as:
* Image Optimization: Perform both lossy (maximum compression with slight quality reduction) and lossless (retaining original quality) optimization on various image formats including JPEG, PNG, GIF, and SVG.
* Image Manipulation: Resize, crop, watermark, and convert images on the fly. You can specify precise dimensions, aspect ratios, and watermarking options.
* Source Flexibility: Optimize images from various sources:
* Direct Upload: Upload local files directly from your server.
* URL Fetching: Provide a URL, and Kraken.io will fetch and optimize the image.
* Destination Options: Define where the optimized images should be stored:
* Return URL: Get a temporary URL to the optimized image.
* Cloud Storage: Integrate with popular cloud storage services like Amazon S3, Azure Blob Storage, Google Cloud Storage, Rackspace Cloud Files, and more, allowing Kraken.io to store the optimized image directly into your buckets.
* Asynchronous Processing: For large batches or background tasks, the API supports asynchronous processing with webhooks, notifying your application when the optimization is complete.
* Metadata Control: Options to strip or preserve EXIF data, copyright information, and other image metadata.
How it works:
1. Initialize the Client: You instantiate the `Kraken` client using your API Key and Secret (obtained from your Kraken.io account).
2. Define Optimization Parameters: You specify the desired optimization type (lossy/lossless), target dimensions, output format, and any other manipulation options within an array.
3. Send Image: You send the image to Kraken.io, either by providing its URL or uploading a local file.
4. Process and Return: Kraken.io processes the image according to your parameters.
5. Retrieve Result: The library receives the response, which typically includes a URL to the optimized image, its original and optimized file sizes, and other relevant information.
By using `kraken-io/kraken-php`, developers can easily automate image optimization processes, significantly reducing bandwidth usage and improving page load times for their web applications.
Example Code
<?php
require __DIR__ . '/vendor/autoload.php';
use Kraken\Kraken;
use Kraken\Exception\KrakenException;
// --- Configuration ---
// Replace with your actual API Key and Secret from Kraken.io dashboard
define('KRAKEN_API_KEY', 'your_api_key');
define('KRAKEN_API_SECRET', 'your_api_secret');
// --- 1. Initialize the Kraken Client ---
try {
$kraken = new Kraken(KRAKEN_API_KEY, KRAKEN_API_SECRET);
echo "Kraken client initialized successfully.\n";
} catch (KrakenException $e) {
die("Error initializing Kraken client: " . $e->getMessage() . "\n");
}
// --- 2. Optimize an image from a URL ---
echo "\n--- Optimizing image from URL ---\n";
$imageUrl = 'https://kraken.io/images/kraken-logo.png'; // Example image URL
$urlParams = array(
'url' => $imageUrl,
'wait' => true, // Wait for the optimization to complete synchronously
'lossy' => true, // Use lossy optimization for maximum compression
'resize' => array(
'width' => 500,
'height' => 300,
'strategy' => 'auto' // 'auto', 'exact', 'portrait', 'landscape', 'crop', 'fit'
),
'quality' => 80 // Quality for JPEG images (0-100)
// 's3_store' => array( // Example for storing to S3 (requires S3 config)
// 'key' => 'YOUR_S3_KEY',
// 'secret' => 'YOUR_S3_SECRET',
// 'bucket' => 'your-s3-bucket',
// 'path' => 'optimized_images/kraken-logo.png'
// )
);
try {
$data = $kraken->url($urlParams);
if ($data['success']) {
echo "Image optimization from URL successful:\n";
echo " Optimized URL: " . $data['kraked_url'] . "\n";
echo " Original size: " . $data['original_size'] . " bytes\n";
echo " Kraked size: " . $data['kraked_size'] . " bytes\n";
echo " Saved bytes: " . $data['saved_bytes'] . " bytes\n";
echo " Reduction: " . round($data['saved_bytes'] / $data['original_size'] * 100, 2) . "%\n";
} else {
echo "Image optimization from URL failed: " . $data['message'] . "\n";
}
} catch (KrakenException $e) {
echo "Error during URL optimization: " . $e->getMessage() . "\n";
}
// --- 3. Optimize an image from a local file ---
echo "\n--- Optimizing image from local file ---\n";
$localImagePath = __DIR__ . '/test_image.jpg'; // Ensure this file exists for testing
// Create a dummy image for testing if it doesn't exist
if (!file_exists($localImagePath)) {
echo "Creating a dummy test_image.jpg...\n";
$image = imagecreatetruecolor(800, 600);
$bgColor = imagecolorallocate($image, 200, 200, 255); // Light blue
imagefill($image, 0, 0, $bgColor);
$textColor = imagecolorallocate($image, 0, 0, 0); // Black
imagestring($image, 5, 50, 250, 'Hello from Kraken-PHP!', $textColor);
imagejpeg($image, $localImagePath);
imagedestroy($image);
echo "Dummy image created at: {$localImagePath}\n";
}
$fileParams = array(
'file' => $localImagePath,
'wait' => true,
'lossy' => true,
'convert' => 'png', // Convert to PNG format
'metadata' => ['exif', 'icc'] // Preserve EXIF and ICC data
);
try {
$data = $kraken->upload($fileParams);
if ($data['success']) {
echo "Image optimization from local file successful:\n";
echo " Optimized URL: " . $data['kraked_url'] . "\n";
echo " Original size: " . $data['original_size'] . " bytes\n";
echo " Kraked size: " . $data['kraked_size'] . " bytes\n";
echo " Saved bytes: " . $data['saved_bytes'] . " bytes\n";
echo " Reduction: " . round($data['saved_bytes'] / $data['original_size'] * 100, 2) . "%\n";
} else {
echo "Image optimization from local file failed: " . $data['message'] . "\n";
}
} catch (KrakenException $e) {
echo "Error during local file upload optimization: " . $e->getMessage() . "\n";
}
// --- Installation via Composer (for reference) ---
/*
To install this library, navigate to your project directory in the terminal
and run the following command:
composer require kraken-io/kraken-php
*/
?>








kraken-io/kraken-php