Search by

smallpict / smallpict-php

4IP

Official PHP SDK for SmallPict Image Optimization API

Package info

github.com/tuxnoob/smallpict-php

Homepage

pkg:composer/smallpict/smallpict-php

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.0.2 2026-09-06 03:23 UTC

This package is auto-updated.

Last update: 2026-09-06 03:24:00 UTC


README

Official PHP SDK for the SmallPict Image Optimization API โ€” high-performance next-gen image transcoding (AVIF, WebP), smart compression, Edge CDN delivery, and cache purging.

Latest Version on Packagist Software License PHP Version

โšก Features

  • ๐Ÿ˜ Broad PHP Compatibility: Supports PHP 7.4 through PHP 8.4 seamlessly.
  • ๐Ÿ”Œ Zero-Dependency Default: Built-in high-performance native cURL client; PSR-18 compatible for modern stacks.
  • ๐Ÿ“ฆ Optional Laravel Auto-Discovery: Instant integration via SmallPictServiceProvider and SmallPict Facade.
  • ๐Ÿ›ก๏ธ Secure HMAC-SHA256 & Bearer Auth: Tamper-proof payload verification.
  • โœจ 4 Core Unified Methods: optimize(), getQuota(), purgeCdn(), and validateKey().
  • ๐Ÿ”„ Resilience & Fault Tolerance: Automatic Idempotency-Key UUID injection, 30s timeouts, and exponential backoff with jitter on HTTP 429/5xx.
  • ๐Ÿ”’ Zero-Leak Privacy: API keys and credentials are automatically redacted from error traces and exception logs.

๐Ÿ“ฅ Installation

composer require smallpict/smallpict-php

๐Ÿš€ Quick Start

1. Standalone PHP Example

<?php

require_once __DIR__ . '/vendor/autoload.php';

use SmallPict\Client;
use SmallPict\Models\OptimizeOptions;
use SmallPict\Models\ImageFormat;

$client = new Client(
    getenv('SMALLPICT_API_KEY'),
    getenv('SMALLPICT_SECRET_KEY') // Optional HMAC Secret Key
);

$imageContent = file_get_contents('hero-banner.png');

$result = $client->optimize(
    $imageContent,
    new OptimizeOptions(
        ImageFormat::AVIF, // Target format: 'avif', 'webp', 'auto', etc.
        80,                // Quality: 1-100
        1920               // Max width in pixels
    )
);

echo "Optimized CDN URL: " . $result->getUrl() . "\n";
echo "Saved: " . $result->getSavingsPercentage() . "% (" . $result->getBytesSaved() . " bytes)\n";

2. Laravel Integration (Laravel 10 / 11)

In config/services.php:

'smallpict' => [
    'api_key' => env('SMALLPICT_API_KEY'),
    'secret_key' => env('SMALLPICT_SECRET_KEY'),
    'fallback_mode' => 'passthrough', // 'throw' | 'passthrough'
],

In your Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SmallPict\Laravel\Facades\SmallPict;
use SmallPict\Models\OptimizeOptions;
use SmallPict\Models\ImageFormat;

class MediaController extends Controller
{
    public function store(Request $request)
    {
        $file = $request->file('avatar');
        
        $result = SmallPict::optimize(
            $file->getRealPath(),
            new OptimizeOptions(ImageFormat::AUTO, 85)
        );

        return response()->json([
            'cdn_url' => $result->getUrl(),
            'format' => $result->getFormat(),
            'savings' => "{$result->getSavingsPercentage()}%",
        ]);
    }
}

3. WordPress Plugin / Theme Integration

use SmallPict\Client;
use SmallPict\Models\OptimizeOptions;

function my_custom_image_upload_handler($file_path) {
    $client = new Client(get_option('smallpict_api_key'));
    
    try {
        $result = $client->optimize($file_path, new OptimizeOptions());
        return $result->getUrl();
    } catch (\SmallPict\Exceptions\SmallPictException $e) {
        // Fallback safely to original image on error
        error_log("SmallPict optimization error: " . $e->getMessage());
        return $file_path;
    }
}

๐Ÿ“Š Account Quota & Edge CDN Purge

use SmallPict\Client;

$client = new Client(getenv('SMALLPICT_API_KEY'));

// 1. Check real-time quota
$quota = $client->getQuota();
echo "Plan: {$quota->getPlan()}, Quota used: {$quota->getQuotaPercentage()}%\n";

// 2. Invalidate CDN cache
$client->purgeCdn(['https://cdn.smallpict.app/opt/hero-banner.avif']);
echo "CDN cache invalidated successfully.\n";

๐Ÿงช Testing

composer test

๐Ÿ“„ License

MIT ยฉ SmallPict Engineering