> ## 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.

# Configure the PHP SDK (video tutorial)

[githublink]: https://github.com/cloudinary-devs/php_credentials

## Overview

Learn how to install and configure the [Cloudinary PHP SDK](php_integration) in your PHP environment. This enables you to utilize Cloudinary for uploading, managing, and displaying assets in your applications.

## Video tutorial

  This video is brought to you by Cloudinary's video player - embed your own!Use the controls to set the playback speed, navigate to chapters of interest and select subtitles in your preferred language.

> **TIP**: :title=View the code

You can find the code from this tutorial in [GitHub][githublink].
## Tutorial contents
This tutorial presents the following topics. Click a timestamp to jump to that part of the video.### Introduction
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=0 :sec=00 :player=cld} | Cloudinary has powerful tools for transforming and managing your images and videos to improve your media workflow. In this video, you'll learn how to install and set up the [Cloudinary PHP SDK](php_integration) so you can start using these features.

### Install the PHP SDK
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=0 :sec=16 :player=cld} | To install the Cloudinary PHP SDK, use Composer to manage dependencies. In your `composer.json` file, add `"cloudinary/cloudinary_php": "^3"`, then run `composer install` to install the Cloudinary PHP package and its dependencies.
|

> **NOTE**: This guide covers, and recommends installing, the current release of the [Cloudinary PHP SDK v3.x](https://github.com/cloudinary/cloudinary_php).

```php
{
    "name": "my-org/php_credentials",
    "description": "Set up the Cloudinary PHP SDK",
    "type": "project",
    "require": {
        "cloudinary/cloudinary_php": "^3"
    },
    "license": "MIT",
    "authors": [
        {
            "name": "my-org",
            "email": "example@my-org.com"
        }
    ]
}
```

### Retrieving and configuring Cloudinary credentials
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=0 :sec=34 :player=cld} | Next, retrieve your Cloudinary credentials and add them to your `.env` file: In the Cloudinary dashboard, click Go to API Keys to open the API Keys page in the Console Settings.Copy the environment variable format and paste it into your <code>.env file.Replace the placeholders in the format for the API Key and API Secret with your actual values. The Cloud Name is already correctly included.
|

.env

```php
CLOUDINARY_URL=cloudinary://<your-api-key>:<your-api-secret>@<your-cloud-name>
```

> **INFO**:
>
> * When writing your own applications, follow your organization's policy on storing secrets and don't expose your API secret.

> * Don't store your `.env` under version control for maximum security.

### Loading environment variables in PHP
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=1 :sec=07 :player=cld} | To import the credentials from your `.env` file into your project, use the `phpdotenv` library:Install it via Composer by entering <code>composer require vlucas/phpdotenv in the terminal.In your PHP file, import the <code>phpdotenv package to load environment variables, as well as the Cloudinary <code>Configuration class.Use methods from the <code>phpdotenv package such as <code>createImmutable() and <code>load() to load your credentials from the <code>.env file. 
|

```php
require_once __DIR__ . '/vendor/autoload.php';

use Dotenv\Dotenv; 
use Cloudinary\Configuration\Configuration;

// Load .env file 
$dotenv = Dotenv::createImmutable(__DIR__); 
$dotenv->load(); 
```

### Creating a Cloudinary instance
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=1 :sec=44 :player=cld} | Use a configuration object to create a Cloudinary instance: Create a Cloudinary configuration object using `new Configuration()`, pass the `CLOUDINARY_URL` environment variable from the `.env` file, and store in a variable.Create an instance using `new Cloudinary()`, passing the configuration object you just created.To confirm you set everything up correctly, print the instance to your HTML page and run the code. You should see your configuration details, including your Cloud Name, API Key, and API Secret.
|

```php
// Initialize Configuration
$config = new Configuration($_ENV['CLOUDINARY_URL']);

// Print the configuration object details
echo '<pre>';
var_dump($config); // You can use var_dump or print_r for detailed output
echo '</pre>';

// Create the Cloudinary instance with the configuration
$cld = new Cloudinary($config);
```

### Generating an HTML image tag with Cloudinary
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=2 :sec=16 :player=cld} | Use the Cloudinary instance to generate a basic HTML `<img>` tag:Import the <code>ImageTag method from the PHP SDK with <code>use Cloudinary\Tag\ImageTag;Use <code>ImageTag() with your Cloudinary instance and pass in the public ID of the image to create its HTML image tag.Store the result in a variable and use <code>echo to render the image in your HTML.Run the code to display your image.|

```php
use Cloudinary\Tag\ImageTag;


// Create the image tag with the transformed image
$imgtag = (new ImageTag('cld-sample')) 
    ->resize(Resize::scale()
        ->width(400)
    );

echo $imgtag;
// The code above generates the following HTML image tag:
//  <img src="https://res.cloudinary.com/new-shop/image/upload/cld-sample">
```

### Applying image transformations 
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=3 :sec=33 :player=cld} | The image may be too large, but one of the great features of Cloudinary is on-the-fly transformations. Instead of using HTML resizing, Cloudinary optimizes the image itself. To resize an image:Import the resize transformation with <code>use Cloudinary\Transformation\Resize;Add the resize transformation to <code>ImageTag() using <code>Resize::scale(), setting the width to 400px: <code>Resize::scale()->width(400);Run the code again, and the image is smaller.|

```php
use Cloudinary\Transoformation\Resize;


// Create the image tag with the transformed image
$imgtag = $cld->ImageTag('cld-sample')
    ->resize(Resize::scale()
        ->width(400)
    );

echo $imgtag;
// The code above generates the following HTML image tag:
//  <img src="https://res.cloudinary.com/new-shop/image/upload/c_scale,w_400/cld-sample">
```

## Keep learning

> **READING**:
>
> * Learn more about the [PHP SDK](php_integration).

> * Watch more [Dev Hints videos](https://www.youtube.com/playlist?list=PL8dVGjLA2oMpaTbvoKCaRNBMQzBUIv7N8) on the [Cloudinary YouTube channel](https://www.youtube.com/cloudinary).

#### If you like this, you might also like...

  
  
  
    Optimization Tips
    Tips for delivering optimized images 
  

  
  
  
    Create Upload Presets (Node.js)
    Streamline media uploads using signed upload presets 
  

  
  
  
    Video Player in HTML
    Embed the Cloudinary Video Player in HTML 
  

&nbsp;

&nbsp;Check out the Cloudinary Academy for free self-paced Cloudinary courses on a variety of developer or DAM topics, or register for formal instructor-led courses, either virtual or on-site.
&nbsp;
