PHP code example of thecyrilcril / imagekit-laravel-client
1. Go to this page and download the library: Download thecyrilcril/imagekit-laravel-client 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/ */
thecyrilcril / imagekit-laravel-client example snippets
use Thecyrilcril\ImageKitClient\Contracts\Client;
use Thecyrilcril\ImageKitClient\Facades\ImageKitClient;
final class UploadAvatar
{
public function __construct(private readonly Client $imageKit) {}
public function handle(): void
{
$this->imageKit->files()->delete('file_id'); // also list(), lazy()
$this->imageKit->urls(); // build delivery URLs
}
}
ImageKitClient::files()->delete('file_id');
ImageKitClient::urls();
use Thecyrilcril\ImageKitClient\Exceptions\NotFound;
try {
ImageKitClient::files()->delete($fileId);
} catch (NotFound) {
// Already gone: treat as deleted.
}
use Thecyrilcril\ImageKitClient\Enums\ResponseField;
use Thecyrilcril\ImageKitClient\Facades\ImageKitClient;
use Thecyrilcril\ImageKitClient\Files\UploadRequest;
use Thecyrilcril\ImageKitClient\Files\UploadSource;
$uploaded = ImageKitClient::files()->upload(new UploadRequest(
source: UploadSource::bytes($contents), // or ::dataUri('data:image/png;base64,…') or ::url('https://…')
fileName: 'avatar.jpg',
folder: '/avatars',
useUniqueFileName: false,
overwriteFile: true,
tags: ['avatar', 'user-42'],
isPrivateFile: false,
customMetadata: ['userId' => 42],
responseFields: [ResponseField::Tags, ResponseField::CustomMetadata],
extensions: [['name' => 'remove-bg', 'options' => ['add_shadow' => true]]],
checks: "'file.size' < '5MB'",
));
$uploaded->fileId; // string
$uploaded->url; // string
$uploaded->width; // ?int — null for a non-image
$uploaded->tags; // list<string>
$uploaded->aiTags; // list<AITag>
$uploaded->versionInfo; // ?VersionInfo
$uploaded->extensionStatus?->removeBg; // ?ExtensionState: Success, Pending or Failed
$uploaded->duration; // ?int — the video group: duration, bitRate, audioCodec, videoCodec
use Thecyrilcril\ImageKitClient\Enums\AssetType;
use Thecyrilcril\ImageKitClient\Enums\FileType;
use Thecyrilcril\ImageKitClient\Enums\SortOrder;
use Thecyrilcril\ImageKitClient\Facades\ImageKitClient;
use Thecyrilcril\ImageKitClient\Files\File;
use Thecyrilcril\ImageKitClient\Files\Folder;
use Thecyrilcril\ImageKitClient\Files\ListRequest;
$page = ImageKitClient::files()->list(new ListRequest(
path: '/avatars', // one folder level; see below for sub-folders
type: AssetType::All, // File, FileVersion, Folder, or All (files and folders together)
fileType: FileType::Image, // All, Image, NonImage
sort: SortOrder::CreatedDescending,
tags: ['hero', 'summer sale'], // sent comma-joined
name: 'banner.jpg',
limit: 100, // 1–1000
skip: 0,
));
foreach ($page->files() as $file) {
$file->fileId; $file->filePath; $file->url; $file->size; $file->tags; $file->createdAt; // …
}
foreach ($page->folders() as $folder) {
$folder->folderId; $folder->folderPath;
}
use Thecyrilcril\ImageKitClient\Facades\ImageKitClient;
use Thecyrilcril\ImageKitClient\Files\UploadRequest;
$fake = ImageKitClient::fake();
// ... run the code under test ...
$fake->assertUploaded('photo.jpg'); // by file name
$fake->assertUploaded(fn (UploadRequest $request) => $request->folder === '/avatars'); // or by callback
$fake->assertNotUploaded('draft.jpg');
$fake->assertNothingUploaded();
$fake->assertDeleted('file_123');
$fake->assertListed('/avatars'); // by path, or by callback
$fake = ImageKitClient::fake()->failUploads();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.