> ## Documentation Index
> Fetch the complete documentation index at: https://cloudinary.com/documentation/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP SDK

[sample-projects-link]:php_sample_projects
[changelog-link]: https://github.com/cloudinary/cloudinary_php/blob/master/CHANGELOG.md

The Cloudinary PHP SDK provides simple, yet comprehensive image and video upload, transformation, optimization, and delivery capabilities through the [Cloudinary APIs](cloudinary_references#url_and_rest_apis), that you can implement using code that integrates seamlessly with your existing PHP application.

PHP library features...

* Build URLs for [image](php_image_manipulation) and [video](php_video_manipulation) transformations
* PHP view [helper tags](php_image_manipulation#the_imagetag_method) for embedding and transforming images
* API wrappers: [file upload](image_upload_api_reference), [administration](admin_api), [transformations](transformation_reference), and more
* [Server-side](php_image_and_video_upload#server_side_upload) file upload + [direct unsigned](php_image_and_video_upload#direct_uploading_from_the_browser) file upload from the browser using the jQuery plugin

> **INFO**: :title=SDK security upgrade, June 2025

We recently released an enhanced security version of this SDK that improves the validation and handling of input parameters. We recommend upgrading to the [latest version][changelog-link] of the SDK to benefit from these security improvements.

## How would you like to learn?

{table:class=no-borders overview}Resource | Description 
--|--
[PHP quick start](php_quickstart) | Get up and running in five minutes with a walk through of installation, configuration, upload, management and transformations.
[Video tutorials](php_video_tutorials) | Watch tutorials relevant to your use cases, from getting started with the PHP SDK, to uploading, transforming and analyzing your images and videos. 
[Sample projects](php_sample_projects) | Explore sample projects to see how to implement Cloudinary functionality such as upload and delivery with transformations.
[Cloudinary PHP SDK GitHub repo](https://github.com/cloudinary/cloudinary_php) | Explore the source code and see the [CHANGELOG][changelog-link] for details on all new features and fixes from previous versions. 
 | Try the free [Introduction to Cloudinary for PHP Developers](https://training.cloudinary.com/courses/introduction-for-api-users-and-php-developers) online course, where you can learn how to upload, manage, transform and optimize your digital assets.

Other helpful resources...

This guide focuses on how to set up and implement popular Cloudinary capabilities using the PHP SDK, but it doesn't cover every feature or option. Check out these other resources to learn about additional concepts and functionality in general. 

{table:class=no-borders overview}Resource | Description 
--|--
[Developer kickstart](dev_kickstart) |A hands-on, step-by-step introduction to Cloudinary features.
[Glossary](cloudinary_glossary) | A helpful resource to understand Cloudinary-specific terminology.
[Guides](programmable_media_guides) | In depth guides to help you understand the many, varied capabilities provided by the product. 
[References](cloudinary_references) | Comprehensive references for all APIs, including PHP code examples.

## Install

Use `Composer` to manage your PHP library dependency, and install Cloudinary's PHP library directly from the [Packagist](https://packagist.org/) repository.

1. Run the following command in the root folder of your project to add the SDK and update your `composer.json`: 

    ```
    composer require cloudinary/cloudinary_php
    ```

    This automatically adds the SDK under the require section in `composer.json`, like this:

    ```json
    {
      "require": {
        "cloudinary/cloudinary_php": "^3"
      }
    }
    ```
  
2. Install dependencies including Cloudinary's PHP package:
  
    ```php
    composer install
    ```

    (If you don't have the necessary permissions you can run the file itself: `php composer.phar install`)

> **NOTE**: Make sure that the composer autoload.php is required in any file that will include Cloudinary code, for example: `require_once __DIR__ . '/vendor/autoload.php';`

## Configure

You can set the required configuration parameters, `cloud_name`, `api_key` and `api_secret` either using the `CLOUDINARY_URL` environment variable, or using the `config` method in your code.

You can set the configuration parameters globally, using either an environment variable or the `Configuration::instance` method, or programmatically in each call to a Cloudinary method. Parameters set in a call to a Cloudinary method override globally set parameters.
To define the `CLOUDINARY_URL` environment variable:

1. Copy the **API environment variable** format from the [API Keys](https://console.cloudinary.com/app/settings/api-keys) page of the Cloudinary Console Settings. 
2. Replace `<your_api_key>` and `<your_api_secret>` with your actual values. Your cloud name is already correctly included in the format. 
 
For example:

```
CLOUDINARY_URL=cloudinary://my_key:my_secret@my_cloud_name
```

### Set required configuration parameters

In a simple PHP app, you can set the Cloudinary configuration globally using the `Configuration` class. This works as long as everything runs in one place.

But in apps with frameworks, modules, or multiple entry points, the global config may not always be available. In those cases, use a [separate instance configuration](#configure_instances) instead.

To set the required configuration parameters globally in your PHP application using the `Configuration` class:

```php
use Cloudinary\Configuration\Configuration;

// Global config: Affects all usage of Cloudinary SDK globally
Configuration::instance([
  'cloud' => [
    'cloud_name' => 'my_cloud_name',
    'api_key'    => 'my_key',
    'api_secret' => 'my_secret'
  ],
  'url' => [
    'secure' => true
  ]
]);

// Or configure programmatically
$config = Configuration::instance();
$config->cloud->cloudName = 'my_cloud_name';
$config->cloud->apiKey = 'my_key';
$config->cloud->apiSecret = 'my_secret';
$config->url->secure = true;
```
> **TIP**: To generate transformation URLs, you only need to configure the cloud name. The API key and API secret aren't required for URL generation.
### Set additional configuration parameters

In addition to the required configuration parameters, you can define a number of optional [configuration parameters](cloudinary_sdks#configuration_parameters) if relevant.

You can append configuration parameters, for example `upload_prefix` and `secure_distribution`, to the environment variable:

```
CLOUDINARY_URL=cloudinary://my_key:my_secret@my_cloud_name?secure_distribution=mydomain.com&upload_prefix=https://api-eu.cloudinary.com
```

Or you can use the `Configuration` method in your code, for example:

```php
use Cloudinary\Configuration\Configuration;

Configuration::instance([
  'cloud' => [
    'cloud_name' => 'my_cloud_name',
    'api_key'    => 'my_key',
    'api_secret' => 'my_secret'
  ],
  'url' => [
    'secure' => true,
    'cname'  => 'mydomain.com'
  ]
]);
```

### Configure instances

When your application becomes more complex (e.g., using frameworks, multiple modules, or different product environments), you'll need to handle your Cloudinary configuration using multiple instances. A global configuration may not be applicable across all the different areas where Cloudinary is used.

You can configure a Cloudinary instance either through a JSON object or programmatically. Below are examples for both methods:

```php
use Cloudinary\Configuration\Configuration;

// Instance config: You can pass this into different modules, useful for multiple environments

$cloudinary = new Cloudinary([
  'cloud' => [
    'cloud_name' => 'my_cloud_name',
    'api_key'  => 'my_key',
    'api_secret' => 'my_secret',
  'url' => [
    'secure' => true]]]);


//  Or configure programmatically

$config = new Configuration();
$config->cloud->cloudName = 'my_cloud_name';
$config->cloud->apiKey = 'my_key';
$config->cloud->apiSecret = 'my_secret';
$config->url->secure = true;
$cloudinary = new Cloudinary($config);
```

You can also configure parameters for an instance via an environment variable, for example:

```php
$cloudinary = new Cloudinary('cloudinary://my_key:my_secret@my_cloud_name?url[secure]=true&url[cname]=my_site.com')
```

> **NOTE**: You also need to include the classes you use in your code, although your IDE should warn you when you need to include a class. All available namespaces and classes are found in the [src folder](https://github.com/cloudinary/cloudinary_php/tree/master/src) of the PHP SDK, and in the [PHP reference guide](https://cloudinary.com/documentation/sdks/php/index).

### Configuration video tutorials

The following tutorials can help you install and set up your SDK.

  
  
  
    Find Your Credentials
    Find your Cloudinary credentials for APIs and SDKs 
  

  
  
  
    Configure the PHP SDK
    Install and configure the Cloudinary PHP SDK 
  

## Use

Once you've installed and configured the PHP SDK, you can use it for:
* **Uploading files to your product environment**: You can upload any files, not only images and videos, set your own naming conventions and overwrite policies, moderate and tag your assets on upload, and much more. [See&nbsp;example](#quick_example_file_upload)
* **Transforming and optimizing images and videos**: Keeping your original assets intact in your product environment, you can deliver different versions of your media - different sizes, formats, with effects and overlays, customized for your needs. [See&nbsp;example](#quick_example_transform_and_optimize)
* **Managing assets**: Using methods from the Admin and Upload APIs, you can organize your assets, for example, list, rename and delete them, add tags and metadata and use advanced search capabilities. [See&nbsp;example](#quick_example_get_details_of_a_single_asset)

Capitalization and data type guidelines...

When using the PHP SDK, keep these guidelines in mind:  

* Properties names: `camelCase`. For example: **publicId**
* Classes: `PascalCase`. For example: **ImageTag**
* Pass parameter data as: `array`

### Quick example: File upload

The following PHP code uploads the `dog.mp4` video to the specified folder, and using the public\_id, `my_dog`. The video overwrites the existing `my_dog` video if it exists. When the video upload is complete, the specified notification URL receives details about the uploaded media asset.

```php
(new UploadApi())->upload('dog.mp4', [
  'asset_folder' => 'pets', 
  'public_id' => 'my_dog', 
  'overwrite' => true, 
  'notification_url' => 'https://mysite.example.com/notify_endpoint', 
  'resource_type' => 'video']);
```

> **Learn more about upload**:
>
> * Read the [Upload guide](upload_images) to learn more about customizing uploads, using upload presets and more.

> * See more examples of [image and video upload](php_image_and_video_upload) using the Cloudinary PHP library.  

> * Explore the [Upload API reference](image_upload_api_reference) to see all available methods and options.

### Quick example: Transform and optimize

Take a look at the following transformation code and the image it delivers:

```php
use Cloudinary\Tag\ImageTag;
use Cloudinary\Transformation\RoundCorners;
use Cloudinary\Transformation\Effect;
use Cloudinary\Transformation\Overlay;
use Cloudinary\Transformation\Rotate;
use Cloudinary\Transformation\Adjust;
use Cloudinary\Transformation\Source;
use Cloudinary\Transformation\Position;
use Cloudinary\Transformation\Gravity;
use Cloudinary\Transformation\FocusOn;
use Cloudinary\Transformation\Compass;
use Cloudinary\Transformation\Resize;
use Cloudinary\Transformation\Transformation;

(new ImageTag('front_face.png'))
  ->resize(Resize::thumbnail()->width(150)
    ->height(150)
	  ->gravity(
	    Gravity::focusOn(FocusOn::face()))
	)
	->roundCorners(RoundCorners::byRadius(20))
	->effect(Effect::sepia())
	->overlay(Overlay::source(
	  Source::image("cloudinary_icon")
	    ->transformation((new Transformation())
	      ->adjust(Adjust::brightness()->level(90))
	      ->adjust(Adjust::opacity(60))
	      ->resize(Resize::scale()->width(50)))
	)
	  ->position((new Position())
	    ->gravity(Gravity::compass(Compass::southEast()))
      ->offsetX(5)
      ->offsetY(5))
	)
	->rotate(Rotate::byAngle(10));
```

This relatively simple code performs all of the following on the original front_face.jpg image before delivering it:

* **Crop** to a 150x150 thumbnail using face-detection gravity to automatically determine the location for the crop
* **Round the corners** with a 20 pixel radius
* Apply a **sepia effect**
* **Overlay the Cloudinary logo** on the southeast corner of the image (with a slight offset). Scale the logo overlay down to a 50 pixel width, with increased brightness and partial transparency (opacity = 60%) 
* **Rotate** the resulting image (including the overlay) by 10 degrees
* **Convert** and deliver the image in PNG format (the originally uploaded image was a JPG)

And here's the URL that's automatically generated and included in an image tag from the above code:

![sample transformation](https://res.cloudinary.com/demo/image/upload/c_thumb,g_face,h_150,w_150/r_20/e_sepia/l_cloudinary_icon/e_brightness:90/o_60/c_scale,w_50/fl_layer_apply,g_south_east,x_5,y_5/a_10/front_face.png  "secure: true, disable_all_tab: true, with_code:false, with_image:false")

In a similar way, you can [transform a video](php_video_manipulation#video_transformation_examples).

> **Learn more about transformations**:
>
> * See all possible transformations in the [Transformation URL API reference](transformation_reference).

> * See all PHP transformation actions and qualifiers in the [Cloudinary PHP SDK reference](https://cloudinary.com/documentation/sdks/php/index).

> * See more examples of [image](php_image_manipulation) and [video](php_video_manipulation) transformations using the `cloudinary_php v2.x` library.

> * Read the [image](image_transformations) and [video](video_manipulation_and_delivery) transformation guides to learn about the different ways to transform your assets.

### Quick example: Get details of a single asset

The following PHP example uses the Admin API [resource](admin_api#get_details_of_a_single_resource_by_public_id) method to return details of the image with public ID `cld-sample`:

```php
$result = $api->resource('cld-sample');
    print_r($result);
```

#### Sample response

```json
{
  "asset_id": "bf98540caf22ed65775ee0951f4746c9",
  "public_id": "cld-sample",
  "format": "jpg",
  "version": 1719304891,
  "resource_type": "image",
  "type": "upload",
  "created_at": "2024-06-25T08:41:31Z",
  "bytes": 476846,
  "width": 1870,
  "height": 1250,
  "backup": true,
  "asset_folder": "",
  "display_name": "cld-sample",
  "url": "http://res.cloudinary.com/cld-docs/image/upload/v1719304891/cld-sample.jpg",
  "secure_url": "https://res.cloudinary.com/cld-docs/image/upload/v1719304891/cld-sample.jpg",
  "next_cursor": "497b323dcb9883a15a5e6a7cfb75d439e4de1ca882f5cbe8de6a8b322c37bdad",
  "derived": [
    {
      "transformation": "c_scale,w_500",
      "format": "jpg",
      "bytes": 22871,
      "id": "ce3d7bf3068809656dc5aa76572535da",
      "url": "http://res.cloudinary.com/cld-docs/image/upload/c_scale,w_500/v1719304891/cld-sample.jpg",
      "secure_url": "https://res.cloudinary.com/cld-docs/image/upload/c_scale,w_500/v1719304891/cld-sample.jpg"
    }
  ]
}

 ```

> **Learn more about managing assets**:
>
> * Check out the [Image and Video asset management](asset_management) guide for all the different capabilities.

> * Get an overview of [asset management](php_asset_administration) using the PHP  SDK.

> * Select the `PHP` tab in the [Admin API](admin_api) and [Upload API](image_upload_api_reference) references to see example code snippets.

## Sample projects
Take a look at the [PHP sample projects][sample-projects-link] page to help you get started integrating Cloudinary into your PHP application.

> **READING**:
>
> * Learn more about [uploading images and videos](php_image_and_video_upload) using the PHP SDK.    

> * See examples of powerful [image](php_image_manipulation) and [video](php_video_manipulation) transformations using PHP code and see our [image transformations](image_transformations) and [video transformation](video_manipulation_and_delivery) docs.   

> * Check out Cloudinary's [asset management](php_asset_administration) capabilities, for example, renaming and deleting assets, adding tags and metadata to assets, and searching for assets.

> * Stay tuned for updates by following the [Release Notes](programmable_media_release_notes) and the [Cloudinary Blog](https://cloudinary.com/blog).
