PHP code example of shardimage / shardimage-php

1. Go to this page and download the library: Download shardimage/shardimage-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shardimage / shardimage-php example snippets


use shardimage\shardimagephp\auth\Client;

$client = new Client([
    'apiKey' => '<apiKey>',             //key to use the API or the image serving
    'apiSecret' => '<apiSecret>',       //secret to use the API
    'imageSecret' => '<imageSecret>',    //secret to gain more security on image serving
    'cloudId' => '<cloudId>',           //default configuration for cloud ID, it can be overwritten in later usage
]);

use shardimage\shardimagephp\models\accesstoken\ImageUrlAccessToken;

$accessToken = new ImageUrlAccessToken();
$accessToken->expiry = time() + 3600;
$accessToken->limit = 1000;
$accessToken->extra = [
    'secret' => 'secretString', //<apiAccessTokenSecret>
];

$accessToken = $client->getAccessTokenService()->create($accessToken);
if ($accessToken instanceof ImageUrlAccessToken) {
    echo $accessToken->id; //<apiAccessToken>
}

use shardimage\shardimagephp\auth\Client;

$tokenClient = new Client([
    'apiAccessToken' => <apiAccessToken>,
    'apiAccessTokenSecret' => <apiAccessTokenSecret>, //optional, 
]);

use shardimage\shardimagephp\models\cloud\Cloud;
use shardimage\shardimagephp\services\CloudService;

$cloud = new Cloud([
    'name' => 'First Cloud',
    'description' => 'My first Shardimage cloud ever. Awesome!',
]);

$cloud->settings = [
    //...
    "deliverySecureUrl" => [
        "status" => true    //securing image hosting
    ],
    //...
];

$cloud = $client->getCloudService()->create($cloud);

use shardimage\shardimagephp\models\cloud\IndexParams;

$indexParams = new IndexParams();
$indexParams->nextPageToken = 0;
$indexParams->projections = [   //with projections parameter, we have the chance to narrow down the returning data.
    IndexParams::PROJECTION_NO_BACKUP,
    IndexParams::PROJECTION_NO_FIREWALL,
];

$response = $client->getCloudService()->index($indexParams);

$file = __DIR__ . '/' . $file;
$fileName = 'Example';
$result = $client->getUploadService()->upload([
    'file' => $file,
    'cloudId' => <cloudId>,
    'publicId' => $fileName,
], [    //optional parameters
    'tags' => [
        'example',
        'dump'
    ],
]);

use shardimage\shardimagephp\helpers\UploadHelper;

$files = [
    'file1.jpg',
    'file2.jpg',
    'file3.png',
    'file4.webp',
    'file5.gif',
];
$client->defer(true); //turning on
foreach ($files as $file) {
    $client->getUploadService()->upload([
        'file' => $file,
        'publicId' => UploadHelper::generateRandomPublicId(32),
    ], [
        'tags' => [
            'batch',
            'examples',
        ],
    ]);
}
$result = $client->defer(false); //without turning off, nothing will happen

$client->getImageService()->delete([
    'publicId' => <publicId>,
    'cloudId' => <cloudId>,
]);

$client->getImageService()->delete([
    'cloudId' => <cloudId>,
    'tag' => '<tag>',
]);

use shardimage\shardimagephp\builders\UploadBuilder;

$builder = (new UploadBuilder())
    ->withPrefix('SDK-TEST-')
    ->withRandomPublicId(16)
    ->withTags(['tag1'])
    ->withAddedTags(['added-tag'])
    ->withFilePath($filePath);
$result = $client->getUploadService()->upload($builder->build());

$transformation = Transformation::create();
$transformation->width(200)->height(200)->group();
$url = $client->getUrlService()->create([
    'cloudId' => <cloudId>,
    'publicId' => <publicId>,
], [
    'transformation' => $transformation,
    'security' => 'basic',
]);
echo $url; //https://img.shardimage.com/<cloudId>/s-b3:<securehash>/w:200_h:200/i/<publicId>
bash
composer 
json
shardimage/shardimage-php:"^1.0"