pixelbin/pixelbin

Pixelbin Backend SDK for PHP helps you integrate the core Pixelbin features with your application.

1.0.0 2025-02-03 07:48 UTC

This package is auto-updated.

Last update: 2025-02-03 07:53:15 UTC


README

Pixelbin Backend SDK for PHP helps you integrate the core Pixelbin features with your application.

Getting Started

Getting started with Pixelbin Backend SDK for PHP

Installation

composer require pixelbin/pixelbin

Usage

Quick Example

<?php

// Import the PixelbinConfig and PixelbinClient
use Pixelbin\Platform\PixelbinClient;
use Pixelbin\Platform\PixelbinConfig;

// Create a config with your API_TOKEN
$config = new PixelbinConfig([
    "domain" => "https://api.pixelbin.io",
    "apiSecret" => "API_TOKEN",
    "integrationPlatform"=> "YourAppName/1.0 (AppPlatform/2.0)", // this is optional
]);

// Create a pixelbin instance
$pixelbin = new PixelbinClient($config);

// Method call
try {
    // List the assets stored on your organization's Pixelbin Storage
    $result = $pixelbin->assets->listFiles();
    # Use result
    print_r($result);
} catch (Exception $e) {
    print_r($e->getMessage());
}

Uploader

upload

Uploads a file to PixelBin with greater control over the upload process.

Arguments

Returns

array: On success, returns a dictionary containing the details of the uploaded file.

Example Usage

Uploading a Buffer
<?php

require_once(__DIR__ . "/vendor/autoload.php");
use Pixelbin\Platform\PixelbinClient;
use Pixelbin\Platform\PixelbinConfig;
use Pixelbin\Platform\Enums\AccessEnum;

// Create a config with your API_TOKEN
$config = new PixelbinConfig([
    "domain" => "https://api.pixelbin.io",
    "apiSecret" => "API_TOKEN",
    "integrationPlatform"=> "YourAppName/1.0 (AppPlatform/2.0)", // this is optional
]);

// Create a PixelBin client instance
$pixelbin = new PixelbinClient($config);

// Sync method call
try {
    // Read the file into a buffer
    $buffer = file_get_contents("myimage.png");

    $result = $pixelbin->uploader->upload(
        file: $buffer,
        name: 'myimage',
        path: 'folder',
        format: 'png',
        access: AccessEnum::PUBLIC_READ,
        tags: [],
        metadata: (object) [],
        overwrite: true,
        filenameOverride: true,
        expiry: 1500,
        uploadOptions: [
            'chunkSize' => 5 * 1024 * 1024,  // 5MB
            'concurrency' => 3,              // 2 concurrent chunk uploads
            'maxRetries' => 2,               // 1 retry for errors that can be retried
            'exponentialFactor' => 2,        // Exponential factor for retries
        ]
    );

    print_r($result["url"]);
    // "https://cdn.pixelbin.io/v2/mycloudname/original/folder/myimage.png"
} catch (Exception $e) {
    print_r($e->getMessage());
}
Uploading a Stream
<?php

require_once(__DIR__ . "/vendor/autoload.php");
use Pixelbin\Platform\PixelbinClient;
use Pixelbin\Platform\PixelbinConfig;
use Pixelbin\Platform\Enums\AccessEnum;
use GuzzleHttp\Psr7\Stream;

// Create a config with your API_TOKEN
$config = new PixelbinConfig([
    "domain" => "https://api.pixelbin.io",
    "apiSecret" => "API_TOKEN",
    "integrationPlatform"=> "YourAppName/1.0 (AppPlatform/2.0)", // this is optional
]);

// Create a PixelBin client instance
$pixelbin = new PixelbinClient($config);

// Sync method call
try {
    if ($stream = fopen('../2.jpeg', 'r')) {
        // Read the file into a stream
        $fileStream = new Stream($stream);

        $result = $pixelbin->uploader->upload(
            file: $fileStream,
            name: 'myimage',
            path: 'folder',
            format: 'png',
            access: AccessEnum::PUBLIC_READ,
            tags: [],
            metadata: (object) [],
            overwrite: true,
            filenameOverride: true,
            expiry: 1500,
            uploadOptions: [
                'chunkSize' => 5 * 1024 * 1024,  // 5MB
                'concurrency' => 3,              // 2 concurrent chunk uploads
                'maxRetries' => 2,               // 1 retry for errors that can be retried
                'exponentialFactor' => 2,        // Exponential factor for retries
            ]
        );

        print_r($result["url"]);
        // "https://cdn.pixelbin.io/v2/mycloudname/original/folder/myimage.png"
        fclose($stream);
    }
} catch (Exception $e) {
    print_r($e->getMessage());
}

Security Utils

For generating Signed URLs

Generate a signed Pixelbin URL

Example:

<?php

use Pixelbin\Utils\Security;

$signedUrl = Security::signURL(
    "https://cdn.pixelbin.io/v2/dummy-cloudname/original/__playground/playground-default.jpeg", // url
    20, // expirySeconds
    "0b55aaff-d7db-45f0-b556-9b45a6f2200e", // accessKey
    "dummy-token", // token
);
// signedUrl
// https://cdn.pixelbin.io/v2/dummy-cloudname/original/__playground/playground-default.jpeg?pbs=8eb6a00af74e57967a42316e4de238aa88d92961649764fad1832c1bff101f25&pbe=1695635915&pbt=0b55aaff-d7db-45f0-b556-9b45a6f2200e

Usage with custom domain url:

<?php

use Pixelbin\Utils\Security;

$signedUrl = Security::signURL(
    "https://krit.imagebin.io/v2/original/__playground/playground-default.jpeg", // url
    30, // expirySeconds
    "0b55aaff-d7db-45f0-b556-9b45a6f2200e", // accessKey
    "dummy-token", // token
);
// signedUrl
// https://krit.imagebin.io/v2/original/__playground/playground-default.jpeg?pbs=1aef31c1e0ecd8a875b1d3184f324327f4ab4bce419d81d1eb1a818ee5f2e3eb&pbe=1695705975&pbt=0b55aaff-d7db-45f0-b556-9b45a6f2200e

URL Utils

Pixelbin provides url utilities to construct and deconstruct Pixelbin urls.

url_to_obj

Deconstruct a pixelbin url

Returns:

Example:

<?php

use Pixelbin\Utils\Url;

$pixelbinUrl = "https://cdn.pixelbin.io/v2/your-cloud-name/z-slug/t.resize(h:100,w:200)~t.flip()/path/to/image.jpeg?dpr=2.0&f_auto=true"
$obj = Url::url_to_obj($pixelbinUrl);
// obj
// {
//     "cloudName": "your-cloud-name",
//     "zone": "z-slug",
//     "version": "v2",
//     "options": {
//         "dpr": 2.0,
//         "f_auto": true,
//     },
//     "transformations": [
//         {
//             "plugin": "t",
//             "name": "resize",
//             "values": [
//                 {
//                     "key": "h",
//                     "value": "100"
//                 },
//                 {
//                     "key": "w",
//                     "value": "200"
//                 }
//             ]
//         },
//         {
//             "plugin": "t",
//             "name": "flip",
//         }
//     ],
//     "filePath": "path/to/image.jpeg",
//     "baseUrl": "https://cdn.pixelbin.io"
// }
<?php

use Pixelbin\Utils\Url;

$customDomainUrl = "https://xyz.designify.media/v2/z-slug/t.resize(h:100,w:200)~t.flip()/path/to/image.jpeg";
$obj = Url::url_to_obj(customDomainUrl, opts={ is_custom_domain: True })
// obj
// {
//     "zone": "z-slug",
//     "version": "v2",
//     "transformations": [
//         {
//             "plugin": "t",
//             "name": "resize",
//             "values": [
//                 {
//                     "key": "h",
//                     "value": "100"
//                 },
//                 {
//                     "key": "w",
//                     "value": "200"
//                 }
//             ]
//         },
//         {
//             "plugin": "t",
//             "name": "flip",
//         }
//     ],
//     "filePath": "path/to/image.jpeg",
//     "baseUrl": "https://xyz.designify.media",
//     "wrkr": False,
//     "workerPath": "",
//     "options": {}
// }
<?php

use Pixelbin\Utils\Url;

$workerUrl = "https://cdn.pixelbin.io/v2/your-cloud-name/z-slug/wrkr/resize:h100,w:200/folder/image.jpeg";

$obj = Url::url_to_obj(workerUrl)
// obj
// {
//     "cloudName": "your-cloud-name",
//     "zone": "z-slug",
//     "version": "v2",
//     "transformations": [],
//     "filePath": "",
//     "worker": True,
//     "workerPath": "resize:h100,w:200/folder/image.jpeg",
//     "baseUrl": "https://cdn.pixelbin.io"
//     "options": {}
// }

obj_to_url

Converts the extracted url obj to a Pixelbin url.

<?php

use Pixelbin\Utils\Url;

$obj = [
    "cloudName" => "your-cloud-name",
    "zone" => "z-slug",
    "version" => "v2",
    "options" => [
        "dpr" => 2.0,
        "f_auto" => true,
    ],
    "transformations" => [
        [
            plugin: "t",
            name: "resize",
            values: [
                [
                    "key" => "h",
                    "value" => "100",
                ],
                [
                    "key" => "w",
                    "value" => "200",
                ],
            ],
        ],
        [
            "plugin" => "t",
            "name" => "flip",
        ],
    ],
    "filePath" => "path/to/image.jpeg",
    "baseUrl" => "https://cdn.pixelbin.io",
];

$url = Url::obj_to_url($obj);
// url
// https://cdn.pixelbin.io/v2/your-cloud-name/z-slug/t.resize(h:100,w:200)~t.flip()/path/to/image.jpeg?dpr=2.0&f_auto=true

Usage with custom domain

<?php

use Pixelbin\Utils\Url;

$obj = [
    "zone" => "z-slug",
    "version" => "v2",
    "transformations" => [
        [
            "plugin" => "t",
            "name" => "resize",
            "values" => [
                [
                    "key" => "h",
                    "value" => "100",
                ],
                [
                    "key" => "w",
                    "value" => "200",
                ],
            ],
        ],
        [
            "plugin" => "t",
            "name" => "flip",
        ],
    ],
    "filePath" => "path/to/image.jpeg",
    "baseUrl" => "https://xyz.designify.media",
    "isCustomDomain" => True,
];

$url = Url::obj_to_url($obj); // obj is as shown above
// url
// https://xyz.designify.media/v2/z-slug/t.resize(h:100,w:200)~t.flip()/path/to/image.jpeg

Usage with URL Translation Worker

<?php

use Pixelbin\Utils\Url;

$obj = {
    "cloudName" => "your-cloud-name",
    "zone" => "z-slug",
    "version" => "v2",
    "transformations" => [],
    "filePath" => "",
    "worker" => True,
    "workerPath" => "resize:h100,w:200/folder/image.jpeg",
    "baseUrl" => "https://cdn.pixelbin.io",
};

$url = Url::obj_to_url($obj); // obj is as shown above
// url
// https://cdn.pixelbin.io/v2/your-cloud-name/z-slug/wrkr/resize:h100,w:200/folder/image.jpeg

Documentation