CloudinaryService
in package
CloudinaryService - Image Upload and Management Service
Handles all Cloudinary operations for image uploads, transformations, and deletions. Provides validation and error handling for file uploads.
Features:
- Single and multiple image uploads
- Automatic image optimization and resizing
- File validation (type, size, MIME type)
- Image deletion from Cloudinary
- Comprehensive error logging
Tags
Table of Contents
Properties
- $uploadApi : UploadApi
Methods
- __construct() : mixed
- Constructor - Initialize Cloudinary service
- deleteImage() : bool
- Delete a single image from Cloudinary
- deleteMultipleImages() : bool
- Delete multiple images from Cloudinary
- getTransformedURL() : string
- Generate a transformed image URL
- uploadImage() : array{url: string, public_id: string}|null
- Upload a single image to Cloudinary
- uploadMultipleImages() : array<int, array{url: string, public_id: string}>
- Upload multiple images to Cloudinary
- getUploadErrorMessage() : string
- Get human-readable upload error message
- validateImageFile() : bool
- Validate an image file before upload
Properties
$uploadApi
private
UploadApi
$uploadApi
Methods
__construct()
Constructor - Initialize Cloudinary service
public
__construct() : mixed
Loads Cloudinary SDK and configuration, initializes API client.
Tags
deleteImage()
Delete a single image from Cloudinary
public
deleteImage(string $publicId) : bool
Removes an image from Cloudinary storage using its public ID.
Parameters
- $publicId : string
-
The Cloudinary public_id of the image to delete
Return values
bool —True if deletion successful, false otherwise
deleteMultipleImages()
Delete multiple images from Cloudinary
public
deleteMultipleImages(array<int, string> $publicIds) : bool
Removes multiple images from Cloudinary storage. Iterates through each public ID and deletes individually.
Parameters
- $publicIds : array<int, string>
-
Array of Cloudinary public_ids to delete
Return values
bool —True if all deletions successful, false if any failed
getTransformedURL()
Generate a transformed image URL
public
getTransformedURL(string $url[, array<string, mixed> $transformations = [] ]) : string
Creates a Cloudinary URL with transformation parameters. Can be used to generate thumbnails, crops, or other transformations on-the-fly.
Example transformations:
- ['width' => 400, 'height' => 300, 'crop' => 'fill']
- ['quality' => 'auto:low', 'format' => 'webp']
Parameters
- $url : string
-
Original Cloudinary image URL
- $transformations : array<string, mixed> = []
-
Transformation parameters
Return values
string —Transformed image URL (currently returns original URL - to be implemented)
uploadImage()
Upload a single image to Cloudinary
public
uploadImage(array{name: string, type: string, tmp_name: string, error: int, size: int} $file[, string $folder = 'events' ]) : array{url: string, public_id: string}|null
Validates the file, uploads it to Cloudinary with automatic optimization, and returns the URL and public ID.
Transformations applied:
- Max dimensions: 1920x1080 (limit crop)
- Quality: auto:good (automatic optimization)
- Unique filename generation
Parameters
- $file : array{name: string, type: string, tmp_name: string, error: int, size: int}
-
File from $_FILES
- $folder : string = 'events'
-
Cloudinary folder path (default: 'events')
Return values
array{url: string, public_id: string}|null —Array with URL and public_id on success, null on failure
uploadMultipleImages()
Upload multiple images to Cloudinary
public
uploadMultipleImages(array{name: array, type: array, tmp_name: array, error: array, size: array} $files[, string $folder = 'events' ]) : array<int, array{url: string, public_id: string}>
Processes an array of files from a multi-file input field. Each file is validated and uploaded individually. Skips files with upload errors.
Parameters
-
$files
: array{name: array
, type: array , tmp_name: array , error: array , size: array } -
Files array from $_FILES
- $folder : string = 'events'
-
Cloudinary folder path (default: 'events')
Return values
array<int, array{url: string, public_id: string}> —Array of successfully uploaded images
getUploadErrorMessage()
Get human-readable upload error message
private
getUploadErrorMessage(int $errorCode) : string
Converts PHP upload error codes to descriptive messages.
Parameters
- $errorCode : int
-
PHP upload error code (UPLOAD_ERR_* constants)
Return values
string —Human-readable error description
validateImageFile()
Validate an image file before upload
private
validateImageFile(array{name: string, type: string, tmp_name: string, error: int, size: int} $file) : bool
Performs comprehensive validation:
- Checks for upload errors
- Verifies file exists in tmp directory
- Checks file size (max 10MB)
- Validates MIME type (jpeg, jpg, png, gif, webp)
Parameters
- $file : array{name: string, type: string, tmp_name: string, error: int, size: int}
-
File array from $_FILES
Return values
bool —True if file is valid, false otherwise