PHPackages                             shortpixel/shortpixel-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. shortpixel/shortpixel-php

ActiveLibrary[API Development](/categories/api)

shortpixel/shortpixel-php
=========================

ShortPixel PHP SDK. Read more at https://shortpixel.com/api-tools

v1.11.0(9mo ago)37531.5k—0.2%10[1 issues](https://github.com/short-pixel-optimizer/shortpixel-php/issues)10MITPHPPHP &gt;=5.3.0CI failing

Since Jul 21Pushed 9mo ago7 watchersCompare

[ Source](https://github.com/short-pixel-optimizer/shortpixel-php)[ Packagist](https://packagist.org/packages/shortpixel/shortpixel-php)[ Docs](https://shortpixel.com/api)[ RSS](/packages/shortpixel-shortpixel-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (19)Used By (10)

[![Build Status](https://camo.githubusercontent.com/27bbb12f83204490e703c7f5a1866a93afd5908aafa99cdace02b338513347e9/68747470733a2f2f7472617669732d63692e6f72672f73686f72742d706978656c2d6f7074696d697a65722f73686f7274706978656c2d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/short-pixel-optimizer/shortpixel-php)

ShortPixel SDK and API client for PHP
=====================================

[](#shortpixel-sdk-and-api-client-for-php)

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

Documentation
-------------

[](#documentation)

[Go to the documentation for the PHP client](https://shortpixel.com/api-tools).

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

[](#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

Usage
-----

[](#usage)

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

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

// Compress with default settings from a local file
ShortPixel\fromFile("/path/to/your/local/unoptimized.png")->toFiles("/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"))->toFiles("/path/to/save/to");
//Compres and rename each file
\ShortPixel\fromFiles(array("/path/to/your/local/unoptimized1.png", "/path/to/your/local/unoptimized2.png"))->toFiles("/path/to/save/to", ['renamed-one.png', 'renamed-two.png']);

// Compress with a specific compression level: 0 - lossless, 1 - lossy (default), 2 - glossy
ShortPixel\fromFile("/path/to/your/local/unoptimized.png")->optimize(2)->toFiles("/path/to/save/to");

// Compress and resize - image is resized to have the either width equal to specified or height equal to specified
//   but not LESS (with settings below, a 300x200 image will be resized to 150x100)
ShortPixel\fromUrls("https://your.site/img/unoptimized.png")->resize(100, 100)->toFiles("/path/to/save/to");
// Compress and resize - have the either width equal to specified or height equal to specified
//   but not MORE (with settings below, a 300x200 image will be resized to 100x66)
ShortPixel\fromUrls("https://your.site/img/unoptimized.png")->resize(100, 100, true)->toFiles("/path/to/save/to");

// Keep the exif when compressing
ShortPixel\fromUrls("https://your.site/img/unoptimized.png")->keepExif()->toFiles("/path/to/save/to");

// 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")->generateWebP()->toFiles("/path/to/save/to");

//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
//Save to the same folder, 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)->toFiles("/path/to/your/local/folder");
//Save to a different folder. CURRENT LIMITATION: When using the text persist type and saving to a different folder, you also need to specify the destination folder as the fourth parameter to fromFolder ( it indicates where the persistence files should be created)
$ret = ShortPixel\fromFolder("/path/to/your/local/folder", 0, array(), "/different/path/to/save/to")->wait(300)->toFiles("/different/path/to/save/to");
//use a URL to map the folder to a WEB path in order for our servers to download themselves the images instead of receiving them via POST - faster and less exposed to connection timeouts
$ret = ShortPixel\fromWebFolder("/path/to/your/local/folder", "http://web.path/to/your/local/folder")->wait(300)->toFiles("/path/to/save/to");
//let ShortPixel back-up all your files, before overwriting them (third parameter of toFiles).
$ret = ShortPixel\fromFolder("/path/to/your/local/folder")->wait(300)->toFiles("/path/to/save/to", null, "/back-up/path");
//Recurse only $N levels down into the subfolders of the folder ( N == 0 means do not recurse )
$ret = ShortPixel\fromFolder("/path/to/your/local/folder", 0, array(), false, ShortPixel::CLIENT_MAX_BODY_SIZE, $N)->wait(300)->toFiles("/path/to/save/to");

//Set custom cURL options (proxy)
\ShortPixel\setCurlOptions(array(CURLOPT_PROXY => '66.96.200.39:80', CURLOPT_REFERER => 'https://shortpixel.com/'));

//A simple loop to optimize all images from a folder
\ShortPixel\ShortPixel::setOptions(array("persist_type" => "text"));
$stop = false;
while(!$stop) {
    $ret = ShortPixel\fromFolder("/path/to/your/local/folder")->wait(300)->toFiles("/path/to/save/to");
    if(count($ret->succeeded) + count($ret->failed) + count($ret->same) + count($ret->pending) == 0) {
        $stop = true;
    }
}

//Compress from an image in memory
$myImage = file_get_contents($pathTo_shortpixel.png);
$ret = \ShortPixel\fromBuffer('shortpixel.png', $myImage)->wait(300)->toFiles(self::$tempDir);

//Compress to an image in memory
$ret = \ShortPixel\fromFiles(array("/path/to/your/local/unoptimized1.png", "/path/to/your/local/unoptimized2.png"))->wait(300)->toBuffers();
file_put_contents($pathTo_optimized1.png, $ret->succeeded[0]->Buffer); //the optimized image in memory

//Get account status and credits info:
$ret = \ShortPixel\ShortPixel::getClient()->apiStatus(YOUR_API_KEY);
```

There are more code examples in the [examples/integration.php](https://github.com/short-pixel-optimizer/shortpixel-php/blob/master/examples/integration.php) file.

Alternatively, you might want to add the call to a cron job using the cmdShortPixelOptimizer.php script found in lib/. More details about its usage here: [ShortPixel CLI](https://shortpixel.com/cli-docs)

Running tests
-------------

[](#running-tests)

```
composer install
vendor/bin/phpunit

```

### Integration tests

[](#integration-tests)

Currently the integration tests were taken out in a separate project, at github's request (contained more than 200Mb test images). If you want to run the integration tests by yourself, please [contact us](https://shortpixel.com/contact) and will provide the integration tests.

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

```

License
-------

[](#license)

This software is licensed under the MIT License. [View the license](LICENSE).

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance56

Moderate activity, may be stable

Popularity48

Moderate usage in the ecosystem

Community26

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 94.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~151 days

Recently: every ~337 days

Total

18

Last Release

287d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4bcfc40ce0fdf840fd28ad727cbdc3fee4a9831e30b8ab1a1ffed7470639ac60?d=identicon)[shortpixel](/maintainers/shortpixel)

---

Top Contributors

[![simondud](https://avatars.githubusercontent.com/u/12509668?v=4)](https://github.com/simondud "simondud (154 commits)")[![titzu](https://avatars.githubusercontent.com/u/1998760?v=4)](https://github.com/titzu "titzu (7 commits)")[![JamesFreeman](https://avatars.githubusercontent.com/u/916500?v=4)](https://github.com/JamesFreeman "JamesFreeman (1 commits)")[![troyxmccall](https://avatars.githubusercontent.com/u/129784?v=4)](https://github.com/troyxmccall "troyxmccall (1 commits)")

---

Tags

apicompressimagesoptimizeshortpixel

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/shortpixel-shortpixel-php/health.svg)

```
[![Health](https://phpackages.com/badges/shortpixel-shortpixel-php/health.svg)](https://phpackages.com/packages/shortpixel-shortpixel-php)
```

###  Alternatives

[tinify/tinify

PHP client for the Tinify API. Tinify compresses your images intelligently. Read more at https://tinify.com.

2515.2M71](/packages/tinify-tinify)[corsinvest/cv4pve-api-php

Corsinvest Proxmox VE Client API PHP

801.4M](/packages/corsinvest-cv4pve-api-php)[tinify/magento2

Make your web shop faster by compressing your JPEG and PNG images. This plugin automatically optimizes your images by integrating with the popular image compression services TinyJPG and TinyPNG.

4345.8k](/packages/tinify-magento2)[glooby/pexels

Pexels API Client for www.pexels.com

2731.6k1](/packages/glooby-pexels)[xchimx/laravel-unsplash

Laravel package for easy integration with the Unsplash API. It allows you to use the Unsplash API in your Laravel applications to fetch photos, collections, and user data.

246.6k](/packages/xchimx-laravel-unsplash)[armorpayments/armorpayments-php

A PHP client library for interacting with the Armor Payments API

153.9k](/packages/armorpayments-armorpayments-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
