PHPackages                             kraken-io/kraken-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. kraken-io/kraken-php

ActiveLibrary[API Development](/categories/api)

kraken-io/kraken-php
====================

Official Kraken.io SDK

1.7(1y ago)1303.7M↓20.8%34[2 issues](https://github.com/kraken-io/kraken-php/issues)10MITPHP

Since Apr 8Pushed 9mo ago17 watchersCompare

[ Source](https://github.com/kraken-io/kraken-php)[ Packagist](https://packagist.org/packages/kraken-io/kraken-php)[ Docs](https://kraken.io)[ RSS](/packages/kraken-io-kraken-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (8)Used By (10)

PHP library for Kraken.io Image Optimizer API
=============================================

[](#php-library-for-krakenio-image-optimizer-api)

This is the official Kraken.io PHP library, which should help users very quickly integrate our Image Optimization into their PHP projects. [Kraken.io](http://kraken.io/) Image Optimizer.

- [Installation](#installation)
- [Getting Started](#getting-started)
- [Downloading Images](#downloading-images)
- [How To Use](#how-to-use)
- [Wait and Callback URL](#wait-and-callback-url)
    - [Wait Option](#wait-option)
    - [Callback URL](#callback-url)
- [Authentication](#authentication)
- [Usage - Image URL](#usage---image-url)
- [Usage - Image Upload](#usage---image-upload)
- [Lossy Optimization](#lossy-optimization)
- [Image Resizing](#image-resizing)
- [WebP Compression](#webp-compression)
- [Image Type Conversion](#image-type-conversion)
- [Preserving Metadata](#preserving-metadata)
- [External Storage](#external-storage)
    - [Amazon S3](#amazon-s3)
    - [Rackspace Cloud Files](#rackspace-cloud-files)
    - [Microsoft Azure](#microsoft-azure)
    - [SoftLayer Object Storage](#softlayer-object-storage)

Installation
------------

[](#installation)

### Composer

[](#composer)

If you're using [Composer](http://getcomposer.org/), you can simply add a dependency on `kraken-io/kraken-php` to your project's `composer.json` file. Here's an example of a dependency on version 1.2:

```
{
    "require": {
        "kraken-io/kraken-php": "^1.2"
    }
}
```

Alternatively you can also execute the following command in your project's root directory:

```
composer require kraken-io/kraken-php

```

### Git

[](#git)

If you already have git, the easiest way to download the Kraken-PHP library is with the git command:

```
git clone git://github.com/kraken-io/kraken-php.git /path/to/include/kraken

```

### By Hand

[](#by-hand)

Alternatively, you may download the PHP files from GitHub and place them within your PHP project:

```
https://github.com/kraken-io/kraken-php/archive/master.zip

```

Getting Started
---------------

[](#getting-started)

First you need to sign up for the [Kraken.io API](http://kraken.io/plans/) and obtain your unique **API Key** and **API Secret**. You will find both under [API Credentials](http://kraken.io/account/api-credentials). Once you have set up your account, you can start using Kraken.io API in your applications.

Downloading Images
------------------

[](#downloading-images)

Remember - never link to optimized images offered to download. You have to download them first, and then replace them in your websites or applications. Due to security reasons optimized images are available on our servers **for one hour** only.

How to use
----------

[](#how-to-use)

You can optimize your images in two ways - by providing an URL of the image you want to optimize or by uploading an image file directly to Kraken.io API.

The first option (image URL) is great for images that are already in production or any other place on the Internet. The second one (direct upload) is ideal for your deployment process, build script or the on-the-fly processing of your user's uploads where you don't have the images available online yet.

Wait and Callback URL
---------------------

[](#wait-and-callback-url)

Kraken.io gives you two options for fetching optimization results. With the `wait` option set the results will be returned immediately in the response. With the `callback_url` option set the results will be posted to the URL specified in your request.

### Wait option

[](#wait-option)

With the `wait` option turned on for every request to the API, the connection will be held open until the image has been optimized. Once this is done you will get an immediate response with a JSON object containing your optimization results. To use this option simply set `"wait": true` in your request.

**Request:**

```
{
    "auth": {
        "api_key": "your-api-key",
        "api_secret": "your-api-secret"
    },
    "url": "http://image-url.com/file.jpg",
    "wait": true
}
```

**Response**

```
{
    "success": true,
    "file_name": "file.jpg",
    "original_size": 324520,
    "kraked_size": 165358,
    "saved_bytes": 159162,
    "kraked_url": "http://dl.kraken.io/d1aacd2a2280c2ffc7b4906a09f78f46/file.jpg"
}
```

### Callback URL

[](#callback-url)

With the Callback URL the HTTPS connection will be terminated immediately and a unique `id` will be returned in the response body. After the optimization is over Kraken.io will POST a message to the `callback_url` specified in your request. The ID in the response will reflect the ID in the results posted to your Callback URL.

We recommend [RequestBin](https://requestbin.com/) as an easy way to capture optimization results for initial testing.

**Request:**

```
{
    "auth": {
        "api_key": "your-api-key",
        "api_secret": "your-api-secret"
    },
    "url": "http://image-url.com/file.jpg",
    "callback_url": "http://awesome-website.com/kraken_results"
}
```

**Response:**

```
{
    "id": "18fede37617a787649c3f60b9f1f280d"
}
```

**Results posted to the Callback URL:**

```
{
    "id": "18fede37617a787649c3f60b9f1f280d"
    "success": true,
    "file_name": "file.jpg",
    "original_size": 324520,
    "kraked_size": 165358,
    "saved_bytes": 159162,
    "kraked_url": "http://dl.kraken.io/18fede37617a787649c3f60b9f1f280d/file.jpg"
}
```

Authentication
--------------

[](#authentication)

The first step is to authenticate to Kraken.io API by providing your unique API Key and API Secret while creating a new Kraken.io instance:

```
