This quick start is intended to let you quickly try using several common Cloudinary features. It doesn't necessarily employ coding best practices and the code you create here isn't intended for production.
You can perform this quick start in a code sandbox or in a clean project in the dev environment of your choice.
You can also view the completed code of this quick start in a GitHub repo.
You may also find our Glossary helpful to understand Cloudinary-specific terminology.
To perform the steps in this quick start, you'll need:
- A Cloudinary Programmable Media account. If you don't have one yet, you can quickly register for free.
- Your account credentials. You can find your credentials in the Dashboard page of your Cloudinary console.
- A working PHP environment with a supported version of PHP.
Composer to manage your PHP library dependency, and install Cloudinary's PHP library.
composer.jsonfile as follows:composer.json
Automatically install dependencies, including Cloudinary's PHP package, by running the following command in the root folder of your project:
Within a new file based on an empty HTML template called 'php_quickstart', use your API environment variable to configure your account credentials (replace
cloudinary://my_key:my_secret@my_cloud_name below with your environment variable value):
<html lang="HTML5"> <head> <title>PHP Quick Start</title> </head> <body> <?php require __DIR__ . '/vendor/autoload.php'; // Use the Configuration class use Cloudinary\Configuration\Configuration; // Configure an instance of your Cloudinary cloud Configuration::instance('cloudinary://my_key:my_secret@my_cloud_name?secure=true');
upload method of the
UploadApi class to upload assets to your account. The code is encased in HTML to format and display the response:
// Use the UploadApi class for uploading assets use Cloudinary\Api\Upload\UploadApi; // Upload the image $upload = new UploadApi(); echo '<pre>'; echo json_encode( $upload->upload('https://res.cloudinary.com/demo/image/upload/flower.jpg', [ 'public_id' => 'flower_sample', 'use_filename' => TRUE, 'overwrite' => TRUE]), JSON_PRETTY_PRINT ); echo '</pre>';
asset method of the
AdminApi class to return the details of our uploaded asset. The code is encased in HTML to format and display the response:
// Use the AdminApi class for managing assets use Cloudinary\Api\Admin\AdminApi; // Get the asset details $admin = new AdminApi(); echo '<pre>'; echo json_encode($admin->asset('flower_sample', [ 'colors' => TRUE]), JSON_PRETTY_PRINT ); echo '</pre>';
imageTag method to generate the full image URL based on the specified transformation parameters and add the image tag to your HTML code:
// Use the Resize transformation group and the ImageTag class use Cloudinary\Transformation\Resize; use Cloudinary\Transformation\Background; use Cloudinary\Tag\ImageTag; // Create the image tag with the transformed image $imgtag = (new ImageTag('flower_sample')) ->resize(Resize::pad() ->width(400) ->height(400) ->background(Background::predominant()) ); echo $imgtag; // The code above generates an HTML image tag similar to the following: // <img src="https://res.cloudinary.com/demo/image/upload/b_auto:predominant,c_pad,h_400,w_400/flower_sample">
php_quickstart.php file and take a look at the output you get from each step of this quick start.
The sample code for this quick start can be forked from GitHub.
- Learn more about the PHP SDK by visiting the other pages in this SDK guide.
- Get comprehensive details about Cloudinary features and capabilities:
- Media upload guide: Provides details and examples of the upload options.
- Image transformations guide: Provides details and examples of the transformations you can apply to image assets.
- Video transformations guide: Provides details and examples of the transformations you can apply to video assets.
- Transformation URL API Reference: Provides details and examples of all available transformation parameters.
- Admin API guide: Provides details and examples of the methods available for managing and organizing your media assets.