A major challenge faced by web designers or developers is effectively storing and managing their media assets. Some image galleries are space-sensitive and lack basic functionality such as effective upload, transformation, and management.
Since tech professionals work with large and complex files, they must manage these media assets in reliable image galleries rather than conventional ones. But how can they assess extensive individual image galleries that are efficient, seamless, and well-optimized?
This article will explore Cloudinary’s PHP digital asset management, features, configuration, and basic functionalities such as upload, transformation, and management.
Transform and optimize your images and videos effortlessly with Cloudinary’s cloud-based solutions. Sign up for free today!
In this article:
- The Cloudinary PHP SDK
- Setting Up the Cloudinary PHP SDK
- Uploading Images and Videos to Cloudinary
- Image and Video Transformations with PHP
- Managing Your Digital Assets
The Cloudinary PHP SDK
The Cloudinary PHP SDK provides a powerful option for integrating Cloudinary’s Image and Video API and Digital Asset Management system into any PHP application. Leveraging their cloud-based infrastructure allows seamless uploading, storage, and manipulation of media assets at scale.
It also includes powerful features such as automatic format conversion, responsive image resizing, and real-time transformations, which can be easily integrated into your workflow using a simple API. This ensures your media is always optimized for performance and quality, improving user experience and site efficiency.
One of the standout features of the Cloudinary PHP SDK is its ability to perform complex image and video transformations on the fly. Developers can apply operations such as cropping, resizing, and adding overlays directly through the API without needing additional software.
This simplifies the development process and offloads the heavy lifting to Cloudinary’s servers, ensuring faster load times and reducing the burden on your own infrastructure.
Setting Up the Cloudinary PHP SDK
Composer manages the library dependency for installing Cloudinary’s PHP SDK. The PHP library should be installed directly from the packagist repository.
The prompt below allows you to update your composer.json
file.
{ "require": { "cloudinary/cloudinary_php": "^2" } }
The dependencies and the PHP package can be installed automatically with this prompt:
composer install
Should challenges related to permission occur, run the file with the prompt:
php composer .phar install.
For the Cloudinary PHP library to function optimally, we need to configure our cloud_name
. For API calls to upload images or videos, the api_key
and api_secret
need to be included, too. You can find these keys in the Cloudinary console dashboard.
Additionally, several optional configuration parameters may be needed and can be done globally with an environmental variable of the configuration: :instance
method or programmatically in each call to a Cloudinary method.
Uploading Images and Videos to Cloudinary
In addition to its robust Image and Video API, Cloudinary offers a feature-rich digital asset management (DAM) product that guides users through several functionalities. These tools help developers and creatives maximize digital assets and simplify operations to provide value to the end user.
For developers who need to create and work with PHP image galleries, this feature is encapsulated in the PHP SDK. In the following sections, we’ll see how files can be managed in the PHP library – from uploading to deployment.
Starting off, uploading media to Cloudinary can be done in one of two ways:
- Server-side upload via Cloudinary’s API
- Direct upload from a browser through their DAM system
Let’s explore each of them.
Server-Side Upload
The server-side method involves uploading a raw file to the library using a PHP backend. The process is done over HTTPS with a secure protocol based on your environment’s api_keys
and api_secret
parameters.
To upload an image, the uploadApi class is used with either of these commands:
use Cloudinary\Api\Upload\UploadApi; public function upload($file, $options = [])
For example, to upload an image with the name my_school.jpg
, you should have the following prompt:
(new UploadApi())->upload('my_school.jpg')
Specific file parameters to upload include a local path, remote HTTP or HTTPS URL, a whitelisted storage bucket URL, and data stream. Different upload parameters exist, but some are specific to Cloudinary.
Uploading videos follows a similar path. However, the resource_type
must be specified as “video” within the upload method. Cloudinary has a specific data size requirement for video upload. The maximum video size to upload is pegged at 20 MB. If your video file exceeds this, the PHP SDK supports automatically uploading large files in chunks. The default chunk_size
parameter is 20 MB.
The command below uploads a video (dog.mp4
) to Cloudinary’s PHP SDK in chunks of 6 MB and stores it in a folder with the public ID dog_closeup
. The resulting video is also transformed into a square and a small rectangle.
(new UploadApi())->upload('dog.mp4', [ 'resource_type' => 'video', 'public_id' => 'myfolder/mysubfolder/dog_closeup', 'chunk_size' => 6000000, 'eager' => [ ['width' => 300, 'height' => 300, 'crop' => 'pad'], ['width' => 160, 'height' => 100, 'crop' => 'crop', 'gravity' => 'south']], 'eager_async' => true, 'eager_notification_url' => 'https://mysite.example.com/notify_endpoint'] )
Once your image or video is uploaded, they are stored in the PHP image gallery and immediately available to you for transformation. The uploads will have an HTTP or HTTPS URL for easy access. Additional assets such as the public ID, resources type, width, height, file size, format, and authenticating signature will be provided.
Direct Upload from Browser
Optimizing media in your PHP image library is made simpler with direct browser uploads to Cloudinary. Instead of using the API, developers can use the Cloudinary dashboard to streamline the image upload process, enhancing efficiency and user experience. The
- Sign in to Cloudinary – Start by logging into your Cloudinary dashboard. If you’re new, signing up is quick and straightforward.
- Navigate to the Media Library – Head over to the Media Library tab once logged in. Here’s where all the magic happens.
- Click ‘Upload’ – Look for the “Upload” button – it’s your gateway to adding new images.
- Select Your Images – Choose the images you wish to upload from your device. You can select multiple files for efficiency.
- Optimization and Transformation – Configure your settings to automatically optimize and transform your images upon upload. This step is crucial for making sure your images are web-ready.
- Upload – Hit the “Upload” button and watch as your images are uploaded to the cloud, returning optimized, transformed, and ready to enhance your web projects.
In just a few clicks, you’ve managed to upload your images and ensure they’re optimized for any platform.
Image and Video Transformations with PHP
Cloudinary makes it easier for developers to create their own PHP image gallery. This is done by setting up the Cloudinary PHP SDK and uploading your desired files. Easy access to the media folder can transform the files by resizing, cropping, lightning, overlaying, and adding effects.
Cloudinary’s PHP image and video transformation lets you alter the file from its original state and perform several actions. We’ll consider the example below to further demonstrate the transformation process.
Assuming you’ve recently uploaded a file (livingroom_yellow_chairs.jpg
) to your PHP image gallery and want to transform it by resizing, changing the dimension from a 250px square, and making it circular, you can search for the image in the folder and use this prompt:
use Cloudinary\Transformation\Resize; use Cloudinary\Transformation\RoundCorners; use Cloudinary\Transformation\Delivery; use Cloudinary\Transformation\Format; (new ImageTag('livingroom-yellow-chair.png')) ->resize(Resize::fill()->width(250) ->aspectRatio(1.0)) ->roundCorners(RoundCorners::max()) ->delivery(Delivery::format( Format::auto()));
After transformation, the following image will be obtained.
Transformed Image
The same prompt above can be used to transform a video.
Managing Your Digital Assets
With the Cloudinary PHP SDK, you can upload media files seamlessly, creating a PHP image gallery. But how can you manage these files and perform functions such as renaming, deleting, and more?
With the Upload and Admin APIs, you can effectively manage, organize, and create more media assets.
The Upload API method allows you to perform several functions, such as:
- Renaming and permanently deleting assets.
- Adding tags, context, and structure metadata to assets.
- Creating new assets such as text images, archives, or zipping files.
- Modifying existing assets.
The Admin API, on the hand, facilitates:
- Listing and restoring assets.
- Bulk deleting of assets.
- Managing uploads.
- Updating existing assets
- Performing advanced searches
- Generating user reports.
Example of Deleting a Single Asset with Upload API
This PHP prompt example below shows how we can use the Upload API to destroy media to a video that is labelled with a public ID “Sample.”
use Cloudinary\Api\Upload\UploadApi; $result = (new UploadApi())->destroy('sample', ['resource_type' => 'video']);
After this command, the resulting Sample becomes:
{ "result": "ok" }
Example of How to Use the Admin API to get the Details of an Asset
To retrieve the details of an image with the public ID “Sample,” the following PHP prompt can be used.
use Cloudinary\Api\Admin\AdminApi; $result = (new AdminApi())->asset("sample");
After retrieving the details, the output is presented in JSON format as shown below.
{ "asset_id": "d86882d7788f5d1d702cb63418f082a6", "public_id": "sample", "format": "jpg", "version": 1312461204, "resource_type": "image", "type": "upload", "created_at": "2017-08-04T12:33:24Z", "bytes": 120253, "width": 864, "height": 576, "url": "http://res.cloudinary.com/demo/image/upload/v1312461204/sample.jpg", "secure_url": "https://.../image/upload/v1312461204/sample.jpg", "next_cursor": "041a39fc10971b9eabd4993470f6bfaf", "derived": [ { "transformation": "c_fill,w_100,h_100", "format": "jpg", "bytes": 7112, "id": "8267a869b62a93a59248f35d7f124c1f", "url": "http://.../demo/image/upload/c_fill,w_100,h_100/v1312461204/sample.jpg", "secure_url": "https://.../image/upload/c_fill,w_100,h_100/v1312461204/sample.jpg" }, { "transformation": "w_230,h_168,c_fit", "format": "jpg", "bytes": 19173, "id": "383e22a57167445552a3cdc16f0a0c85", "url": "http://.../demo/image/upload/w_230,h_168,c_fit/v1312461204/sample.jpg", "secure_url": "https://.../image/upload/w_230,h_168,c_fit/v1312461204/sample.jpg" } ] }
These two examples demonstrate how far the Upload and Admin APIs can go when managing PHP media assets.
Wrap Up
Cloudinary’s PHP SDK makes it easy for developers and creatives to upload files to and manage their PHP image gallery. Our fully automated process makes it easier to not create these folders but effectively manage them and complete actions such as transformation and optimization of small to complex files.
Empower your development team with Cloudinary’s easy-to-use APIs and SDKs. Sign up for free today!