ShortPixel.com PHP SDK
PHP
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
LICENSE
README.md
composer.json
phpunit.xml

README.md

Build Status

ShortPixel SDK and API client for PHP

PHP client for the ShortPixel API, used for ShortPixel ShortPixel optimizes your images and improves website performance by reducing images size. Read more at http://shortpixel.com.

Documentation

Go to the documentation for the PHP client.

Installation

Install the API client with Composer. Add this to your composer.json:

{
  "require": {
    "shortpixel/shortpixel-php": "*"
  }
}

Then install with:

composer install

Use autoloading to make the client available in PHP:

require_once("vendor/autoload.php");

Alternatively, if you don't use Composer, add the following require to your PHP code:

require_once("lib/shortpixel-php-req.php");_

Get your API Key from https://shortpixel.com/free-sign-up

Usage

// Set up the API Key. 
ShortPixel\setKey("YOUR_API_KEY");

// Compress with default settings
ShortPixel\fromUrls("https://your.site/img/unoptimized.png")->toFile("/path/to/save/to");
// Compress with default settings but specifying a different file name
ShortPixel\fromUrls("https://your.site/img/unoptimized.png")->toFile("/path/to/save/to", "optimized.png");

// Compress with default settings from a local file
ShortPixel\fromFile("/path/to/your/local/unoptimized.png")->toFile("/path/to/save/to");
// Compress with default settings from several local files
ShortPixel\fromFiles(array("/path/to/your/local/unoptimized1.png", "/path/to/your/local/unoptimized2.png"))->toFile("/path/to/save/to");

// Compress and resize
ShortPixel\fromUrls("https://your.site/img/unoptimized.png")->resize(100, 100)->toFile("/path/to/save/to", "optimized.png");
// Keep the exif when compressing
ShortPixel\fromUrls("https://your.site/img/unoptimized.png")->keepExif()->toFile("/path/to/save/to", "optimized.png");
// Also generate and save a WebP version of the file - the WebP file will be saved next to the optimized file, with  same basename and .webp extension
ShortPixel\fromUrls("https://your.site/img/unoptimized.png")->keepExif()->toFile("/path/to/save/to", "optimized.png");

//Compress from a folder - the status of the compressed images is saved in a text file named .shortpixel in each image folder
\ShortPixel\ShortPixel::setOptions(array("persist_type" => "text"));
//Each call will optimize up to 10 images from the specified folder and mark in the .shortpixel file. 
//It automatically recurses a subfolder when finds it
//Set wait time to 300 to allow enough time for the images to be processed
$ret = ShortPixel\fromFolder("/path/to/your/local/folder")->wait(300)->toFile("/path/to/save/to");
//A simple loop to optimize all images from a folder
$stop = false;
while(!$stop) {
    $ret = ShortPixel\fromFolder("/path/to/your/local/folder")->wait(300)->toFile("/path/to/save/to");
    if(count($ret->->succeeded) + count($ret->failed) + count($ret->same) + count($ret->pending) == 0) {
        $stop = true;
    }
}
//Alternatively, you might want to add the call to a cron job

Running tests

composer install
vendor/bin/phpunit

Integration tests

composer install
SHORTPIXEL_KEY=$YOUR_API_KEY vendor/bin/phpunit --no-configuration test/integration.php

License

This software is licensed under the MIT License. View the license.