Programmable Media

Configure the PHP SDK (video tutorial)

Last updated: Mar-07-2025

Overview

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

Video tutorial


View the code
You can find the code from this tutorial in GitHub.

Tutorial contents

This tutorial presents the following topics. Click a timestamp to jump to that part of the video.

Introduction

Jump to this spot in the video  0:00 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 so you can start using these features.

Install the PHP SDK

Jump to this spot in the video  0:16 To install the Cloudinary PHP SDK, use Composer to manage dependencies. In your composer.json file, add "cloudinary/cloudinary_php": "^2", then run composer install to install the Cloudinary PHP package and its dependencies.

Retrieving and configuring Cloudinary credentials

Jump to this spot in the video  0:34 Next, retrieve your Cloudinary credentials and add them to your .env file:

  1. In the Cloudinary dashboard, click Go to API Keys to open the API Keys page in the Console Settings.
  2. Copy the environment variable format and paste it into your .env file.
  3. 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

Important
  • 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

Jump to this spot in the video  1:07 To import the credentials from your .env file into your project, use the phpdotenv library:

  1. Install it via Composer by entering composer require vlucas/phpdotenv in the terminal.
  2. In your PHP file, import the phpdotenv package to load environment variables, as well as the Cloudinary Configuration class.
  3. Use methods from the phpdotenv package such as createImmutable() and load() to load your credentials from the .env file.

Creating a Cloudinary instance

Jump to this spot in the video  1:44 Use a configuration object to create a Cloudinary instance:

  1. Create a Cloudinary configuration object using new Configuration(), pass the CLOUDINARY_URL environment variable from the .env file, and store in a variable.
  2. 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.

Generating an HTML image tag with Cloudinary

Jump to this spot in the video  2:16 Use the Cloudinary instance to generate a basic HTML <img> tag:

  1. Import the ImageTag method from the PHP SDK with use Cloudinary\Tag\ImageTag;
  2. Use ImageTag() with your Cloudinary instance and pass in the public ID of the image to create its HTML image tag.
  3. Store the result in a variable and use echo to render the image in your HTML.

Run the code to display your image.

Applying image transformations

Jump to this spot in the video  3:33 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:

  1. Import the resize transformation with use Cloudinary\Transformation\Resize;
  2. Add the resize transformation to ImageTag() using Resize::scale(), setting the width to 400px: Resize::scale()->width(400);
Run the code again, and the image is smaller.

Keep learning

Related topics

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

 

Cloudinary Academy

 

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.

 

✔️ Feedback sent!

Rate this page: