### Install Cloudinary PHP SDK with Composer Source: https://github.com/cloudinary/cloudinary_php/blob/master/README.md This command installs the Cloudinary PHP SDK using Composer, the dependency manager for PHP. Ensure Composer is installed on your system before running this command. ```bash composer require "cloudinary/cloudinary_php" ``` -------------------------------- ### Admin API Operations with Cloudinary PHP SDK Source: https://context7.com/cloudinary/cloudinary_php/llms.txt Provides examples for managing assets, folders, transformations, and cloud resources using Cloudinary's Admin API. Operations include listing and retrieving asset details, managing assets by tag, updating metadata, deleting assets (individually, by prefix), renaming, creating and deleting folders, managing transformations, creating upload presets, and retrieving usage statistics. Requires the Cloudinary PHP SDK. ```php adminApi(); // List assets $result = $adminApi->assets([ 'resource_type' => 'image', 'type' => 'upload', 'max_results' => 50, 'prefix' => 'folder/', 'tags' => true, 'context' => true ]); foreach ($result['resources'] as $asset) { echo $asset['public_id'] . ": " . $asset['format'] . "\n"; } // Get asset details $result = $adminApi->asset('sample.jpg', [ 'colors' => true, 'faces' => true, 'quality_analysis' => true ]); // List assets by tag $result = $adminApi->assetsByTag('featured', [ 'resource_type' => 'image', 'max_results' => 30 ]); // Update asset metadata $result = $adminApi->update('my_image', [ 'tags' => ['updated', 'processed'], 'context' => ['caption' => 'Updated caption'], 'metadata' => ['category' => 'nature'] ]); // Delete assets $result = $adminApi->deleteAssets(['image1', 'image2', 'image3']); // Delete by prefix $result = $adminApi->deleteAssetsByPrefix('old_folder/'); // Rename asset $result = $adminApi->rename('old_name', 'new_name', [ 'overwrite' => true, 'invalidate' => true ]); // Folder operations $result = $adminApi->createFolder('new_folder'); $result = $adminApi->subFolders('parent_folder'); $result = $adminApi->deleteFolder('folder_to_delete'); // Transformations $result = $adminApi->transformation('w_300,h_200,c_fill'); $result = $adminApi->transformations(['max_results' => 50]); $result = $adminApi->deleteTransformation('w_100,h_100,c_fill'); // Upload presets $result = $adminApi->createUploadPreset([ 'name' => 'my_preset', 'folder' => 'uploads', 'tags' => ['auto_upload'], 'unsigned' => true ]); // Usage statistics $result = $adminApi->usage([ 'date' => '2024-01-01' ]); echo "Bandwidth: " . $result['bandwidth']['usage'] . " bytes\n"; echo "Storage: " . $result['storage']['usage'] . " bytes\n"; ``` -------------------------------- ### Cloudinary PHP Upload Widget Integration Source: https://context7.com/cloudinary/cloudinary_php/llms.txt Illustrates how to generate Cloudinary upload widget configuration and HTML tags using the PHP SDK. Examples include creating a basic upload tag, an unsigned upload tag with a preset, and a more complex upload tag with various customization options such as sources, file size limits, allowed formats, cropping, folder, and tags. Requires the Cloudinary PHP SDK. ```php // Unsigned upload tag with preset $uploadTag = UploadTag::unsigned('image', 'my_upload_preset'); echo $uploadTag; // Upload tag with callback $uploadTag = new UploadTag('profile_image', [ 'sources' => ['local', 'url', 'camera'], 'multiple' => false, 'max_file_size' => 5000000, 'client_allowed_formats' => ['jpg', 'png', 'gif'], 'cropping' => 'server', 'cropping_aspect_ratio' => 1, 'folder' => 'profiles', 'tags' => ['user_profile'] ]); echo $uploadTag; ``` -------------------------------- ### Rebasing Contributions with Upstream Master Source: https://github.com/cloudinary/cloudinary_php/blob/master/CONTRIBUTING.md Guides on how to rebase your local branch with the latest changes from the upstream master branch, useful when working on a change for an extended period. ```git git fetch upstream git rebase upstream/master git push origin my-feature-branch -f ``` -------------------------------- ### Configure and Initialize Cloudinary SDK in PHP Source: https://context7.com/cloudinary/cloudinary_php/llms.txt Demonstrates multiple ways to initialize the Cloudinary PHP SDK using cloud credentials. This includes array configuration, environment variables, Cloudinary URL strings, and setting a global singleton instance. It also shows how to validate the configuration. ```php [ 'cloud_name' => 'your_cloud_name', 'api_key' => 'your_api_key', 'api_secret' => 'your_api_secret' ] ]); // Method 2: Using CLOUDINARY_URL environment variable // Set env: CLOUDINARY_URL=cloudinary://api_key:api_secret@cloud_name $cloudinary = new Cloudinary(); // Automatically reads from env // Method 3: Using Cloudinary URL string $cloudinary = new Cloudinary('cloudinary://api_key:api_secret@cloud_name'); // Method 4: Global singleton instance Configuration::instance([ 'cloud' => [ 'cloud_name' => 'demo', 'api_key' => 'test_key', 'api_secret' => 'test_secret' ] ]); // Validate configuration $cloudinary->configuration->validate(); ``` -------------------------------- ### Initialize Cloudinary PHP SDK Source: https://github.com/cloudinary/cloudinary_php/blob/master/README.md This PHP code snippet demonstrates how to instantiate the Cloudinary SDK. This object is used to interact with Cloudinary's services for asset management and transformation. ```php use Cloudinary\Cloudinary; $cloudinary = new Cloudinary(); ``` -------------------------------- ### Image Transformations and URL Generation with Cloudinary PHP Source: https://context7.com/cloudinary/cloudinary_php/llms.txt This snippet demonstrates how to apply various transformations to images using the Cloudinary PHP SDK and generate delivery URLs. It covers resizing, format conversion, quality adjustment, chaining multiple transformations, applying overlays, cropping with gravity, and creating responsive images with format conversion. No external dependencies beyond the Cloudinary SDK are required. ```php image('sample.jpg') ->resize(Resize::fill()->width(300)->height(250)) ->format(Format::auto()) ->quality('auto') ->toUrl(); // Chain multiple transformations $url = $cloudinary->image('woman.jpg') ->resize(Resize::scale(500)) ->rotate(17) ->effect(Effect::sepia()) ->toUrl(); // Advanced transformation with overlay use Cloudinary\Transformation\Overlay; use Cloudinary\Transformation\Position; use Cloudinary\Transformation\Compass; use Cloudinary\Transformation\Source; $url = $cloudinary->image('sample.jpg') ->resize(Resize::fill()->width(400)->height(400)) ->overlay( Source::image('logo')->resize(Resize::scale(50)), Position::absolute()->gravity(Compass::northEast())->offsetX(10)->offsetY(10) ) ->toUrl(); // Crop with gravity $url = $cloudinary->image('face.jpg') ->resize(Resize::crop()->width(200)->height(200)->gravity('face')) ->toUrl(); // Responsive image with format conversion $url = $cloudinary->image('photo.jpg') ->resize(Resize::fit()->width(800)->height(600)) ->format(Format::webp()) ->quality(80) ->fetchFormat('auto') ->toUrl(); ``` -------------------------------- ### Search Assets with Cloudinary PHP SDK Source: https://context7.com/cloudinary/cloudinary_php/llms.txt Demonstrates various ways to search and filter assets using Cloudinary's Search API. It covers basic expression searches, filtering with sorting, searching by tags, complex criteria, pagination, asynchronous searching, and generating signed search URLs. Dependencies include the Cloudinary PHP SDK. ```php searchApi(); // Basic search by expression $results = $searchApi ->expression('resource_type:image AND format:jpg') ->execute(); foreach ($results['resources'] as $asset) { echo $asset['public_id'] . "\n"; } // Search with filters and sorting $results = $searchApi ->expression('folder:products/*') ->sortBy('created_at', 'desc') ->maxResults(50) ->execute(); // Search by tags $results = $searchApi ->expression('tags:featured') ->withField('context') ->withField('tags') ->execute(); // Complex search with multiple criteria $results = $searchApi ->expression('resource_type:image') ->addWhere('format', 'jpg') ->addWhere('width', ['gte' => 1000]) ->addWhere('created_at', ['gte' => '7d']) ->aggregate('format') ->sortBy('public_id', 'asc') ->maxResults(100) ->execute(); // Pagination $results = $searchApi ->expression('folder:archive') ->maxResults(20) ->nextCursor($previousCursor) ->execute(); // Async search $promise = $searchApi ->expression('tags:landscape') ->executeAsync(); $results = $promise->wait(); // Generate signed search URL for client-side use $searchUrl = $searchApi ->expression('resource_type:image') ->maxResults(30) ->toUrl(3600); // TTL in seconds ``` -------------------------------- ### Forking and Cloning the Cloudinary PHP Project Source: https://github.com/cloudinary/cloudinary_php/blob/master/CONTRIBUTING.md Instructions for forking the Cloudinary PHP project on GitHub and cloning your forked repository locally. This sets up your local environment for making contributions. ```git git clone https://github.com/contributor/cloudinary_php.git cd cloudinary_php git remote add upstream https://github.com/cloudinary/cloudinary_php.git ``` -------------------------------- ### Video Transformation and HTML Tag Generation with Cloudinary PHP Source: https://context7.com/cloudinary/cloudinary_php/llms.txt This PHP code snippet demonstrates how to transform videos using the Cloudinary SDK and generate HTML video tags. It covers creating video URLs with transformations like resizing and format changes, generating basic video tags, applying transformations directly to video tags, adding custom HTML attributes, and creating video tags with multiple source formats for broader compatibility. The Cloudinary SDK is the primary dependency. ```php video('dog') ->resize(Resize::scale(500)) ->rotate(17) ->format('mp4') ->toUrl(); // Generate video tag $videoTag = new VideoTag('dog'); echo $videoTag; // // Video tag with transformation $videoTag = new VideoTag( (new Video('dog'))->rotate(17)->resize(Resize::scale(500)) ); echo $videoTag; // Video tag with HTML attributes $videoTag = (new VideoTag('dog'))->setAttributes([ 'controls', 'loop', 'muted' => 'true', 'preload' => 'auto', 'style' => 'border: 1px solid black' ]); echo $videoTag; // // Video with multiple source formats $videoTag = $cloudinary->videoTag('my_video', [ ['transformation' => ['width' => 1280, 'height' => 720, 'crop' => 'fill'], 'format' => 'mp4'], ['transformation' => ['width' => 1280, 'height' => 720, 'crop' => 'fill'], 'format' => 'webm'] ]); echo $videoTag; ``` -------------------------------- ### Upload Assets to Cloudinary using PHP Source: https://context7.com/cloudinary/cloudinary_php/llms.txt Shows how to upload various asset types (images, videos, raw files) to Cloudinary using the PHP SDK. Covers basic uploads, uploads with options like public ID, folder, tags, context, and overwriting. Also includes uploads from URLs, unsigned uploads with presets, and asynchronous uploads. ```php uploadApi(); // Basic upload from local file $result = $uploadApi->upload('/path/to/image.jpg'); echo $result['secure_url']; // https://res.cloudinary.com/... // Upload with options $result = $uploadApi->upload('/path/to/photo.jpg', [ 'public_id' => 'my_photo', 'folder' => 'portfolio', 'tags' => ['landscape', 'featured'], 'context' => ['caption' => 'Beautiful sunset', 'alt' => 'Sunset photo'], 'overwrite' => true, 'unique_filename' => false, 'resource_type' => 'image' ]); // Upload from URL $result = $uploadApi->upload('https://example.com/image.jpg', [ 'public_id' => 'remote_image' ]); // Upload video $result = $uploadApi->upload('/path/to/video.mp4', [ 'resource_type' => 'video', 'folder' => 'videos', 'eager' => [ ['width' => 300, 'height' => 300, 'crop' => 'pad', 'format' => 'mp4'], ['width' => 160, 'height' => 100, 'crop' => 'crop', 'format' => 'webm'] ] ]); // Unsigned upload with preset $result = $uploadApi->unsignedUpload('image.jpg', 'my_upload_preset', [ 'tags' => ['user_upload'] ]); // Async upload $promise = $uploadApi->uploadAsync('/path/to/large-file.jpg', [ 'public_id' => 'large_image' ]); $result = $promise->wait(); // Wait for completion ``` -------------------------------- ### Pushing Changes to the Remote Repository Source: https://github.com/cloudinary/cloudinary_php/blob/master/CONTRIBUTING.md Instructions for pushing your local feature branch to your remote repository on GitHub. ```git git push origin my-feature-branch ``` -------------------------------- ### Transform and Optimize Image with Cloudinary PHP SDK Source: https://github.com/cloudinary/cloudinary_php/blob/master/README.md This PHP code shows how to transform and optimize an image using the Cloudinary SDK. It resizes the image to a specified dimension and automatically determines the optimal format. ```php $cloudinary->image('sample.jpg')->resize(Resize::fill()->width(100)->height(150))->format(Format::auto()); ``` -------------------------------- ### Configuring Git User Information for Commits Source: https://github.com/cloudinary/cloudinary_php/blob/master/CONTRIBUTING.md How to set your global Git user name and email address, which is necessary for making commits to the repository. ```git git config --global user.name "Your Name" git config --global user.email "contributor@example.com" ``` -------------------------------- ### Creating a Topic Branch for Contributions Source: https://github.com/cloudinary/cloudinary_php/blob/master/CONTRIBUTING.md Steps to create a new branch for your feature or bug fix, ensuring your local repository is up-to-date with the upstream master branch. ```git git checkout master git pull upstream master git checkout -b my-feature-branch ``` -------------------------------- ### Upload Asset with Cloudinary PHP SDK Source: https://github.com/cloudinary/cloudinary_php/blob/master/README.md This PHP code snippet demonstrates how to upload an asset, such as an image, to Cloudinary using the SDK's upload API. It utilizes the default upload configuration. ```php $cloudinary->uploadApi->upload('my_image.jpg'); ``` -------------------------------- ### Committing Changes in the Cloudinary PHP Project Source: https://github.com/cloudinary/cloudinary_php/blob/master/CONTRIBUTING.md The process of staging changes and committing them with a descriptive message. It also mentions squashing commits for a cleaner git log. ```git git add ... git commit ``` -------------------------------- ### Generate HTML Image Tags with Cloudinary PHP Source: https://context7.com/cloudinary/cloudinary_php/llms.txt This code snippet shows how to generate HTML image tags, including responsive variants with srcset and picture tags, using the Cloudinary PHP SDK. It covers basic tag generation, applying transformations to tags, defining explicit and auto-optimal breakpoints for srcset, and creating complex picture tags with media queries. The output is standard HTML. ```php // Image tag with transformation $tag = (new ImageTag('sample')) ->resize(Resize::scale(500)) ->rotate(17) ->effect(Effect::sepia()); echo $tag; // // Responsive image with srcset breakpoints $tag = (new ImageTag('sample')) ->breakpoints([320, 768, 1024, 1920]); echo $tag; // // Auto-optimal breakpoints $tag = (new ImageTag('sample'))->autoOptimalBreakpoints(); echo $tag; // Generates optimal breakpoints automatically // Picture tag with media queries use Cloudinary\Tag\PictureTag; $pictureTag = new PictureTag( 'sample', [ ['max_width' => 360, 'transformation' => 'ar_9:16,c_fill,g_auto,w_360'], ['min_width' => 360, 'max_width' => 800, 'transformation' => 'ar_1,c_fill,g_auto,w_800'], ['min_width' => 800, 'transformation' => 'ar_16:9,c_fill,g_auto'] ] ); echo $pictureTag; // // // // // // ``` -------------------------------- ### Handle Cloudinary PHP API Errors and Exceptions Source: https://context7.com/cloudinary/cloudinary_php/llms.txt Demonstrates how to handle various Cloudinary API errors and exceptions using try-catch blocks in PHP. This includes handling general API errors, specific errors like NotFound, RateLimited, and AuthorizationRequired, as well as configuration validation errors. It also shows how to handle errors in asynchronous operations. ```php uploadApi()->upload('image.jpg', [ 'public_id' => 'my_image', 'overwrite' => false ]); echo "Upload successful: " . $result['secure_url']; } catch (ApiError $e) { echo "Upload failed: " . $e->getMessage(); echo "HTTP Status: " . $e->getCode(); } // Admin API with specific error handling try { $result = $cloudinary->adminApi()->asset('nonexistent_image'); } catch (NotFound $e) { echo "Asset not found: " . $e->getMessage(); } catch (AuthorizationRequired $e) { echo "Authentication failed: " . $e->getMessage(); } catch (RateLimited $e) { echo "Rate limit exceeded, retry after: " . $e->getMessage(); } catch (ApiError $e) { echo "API error: " . $e->getMessage(); } // Configuration validation try { $cloudinary = new Cloudinary([ 'cloud' => ['cloud_name' => ''] // Invalid ]); $cloudinary->configuration->validate(); } catch (ConfigurationException $e) { echo "Invalid configuration: " . $e->getMessage(); } // Async error handling $promise = $cloudinary->uploadApi()->uploadAsync('file.jpg'); $promise->then( function ($result) { echo "Success: " . $result['public_id']; }, function ($error) { echo "Error: " . $error->getMessage(); } ); ?> ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.