Cloudinary Blog

Introducing the Cloudinary PHP SDK, Mark II

By
New Cloudinary PHP SDK Released

Cloudinary supports a wide range of SDKs, many of which have been around for quite some time now. The PHP SDK was initially released in 2012, nine years ago, and much has changed since then with regard to programming languages and development concepts. This means that the SDKs need a refresh in order to be aligned with the latest standards and best practices, also to comply with the current language standards and syntax usage.

Since our existing SDKs have to support a wide range of specific technology versions, reducing support in a minor release is unacceptable. So, Cloudinary is revamping the entire SDK suite, making it more modern, intuitive, and user friendly. The PHP SDK is the first to undergo a major overhaul with a major release. Our main focus for v2 is on enhancing developer experience with the following features:

  • Structured action-based syntax
  • High discoverability with integrated autocomplete
  • Maintainability
  • Error handling
  • Preserving flexibility and future compatibility
  • Comprehensive reference documentation

Actions

Cloudinary’s SDK v1 suite was all about wrapping our URL syntax, which requires knowledge of the URL’s transformation syntax. For example, when creating a simple transformation on an image that includes rotating, cropping, and an effect, the v1 code reads as follows:

Copy to clipboard
#SDK 1
cloudinary_url("bike", 
  array(
    "angle"=>20, 
    "crop"=>"crop", 
    "gravity"=>"face", 
    "effect"=>"cartoonify"));

As part of our focus on the developer experience, we want the new SDK to have an easy, strongly typed, discoverable syntax, enabling developers to write their code quickly, without compromising the flexibility and future compatibility in the current SDKs. To that end, we introduced a structured way of building action-oriented transformations. The new code for the example above reads this way:

Copy to clipboard
#SDK 2
$cld = new Cloudinary();
$cld->image('bike')
  ->rotate(Rotate::byAngle(20))
  ->resize(Resize::crop()->gravity(Gravity::focusOn(FocusOn::face()))
  ->effect(Effect::cartoonify())->toUrl();

Discoverability

The action-oriented structure in v2 improves discoverability, meaning that you need not learn the URL transformation syntax and can code more easily. The new structure also means that—

  • You work in a typed environment, in which the classes, methods, and variables of the SDK are autocompleted by the IDE, with the relevant options and values listed in a pop-up while you’re typing.
  • The IDE verifies the validity of your code, catching errors early on with no time lag for server errors.

Maintainability and Configuration

SDK v2 enables the adoption of new features quickly, reduces support of old technologies, and simplifies the configuration options. With the new modular structure, you can decide what parts of the SDK to use, configuring settings in several levels through a configuration hierarchy. For example, to set the configuration globally via a JSON object, code like this:

Copy to clipboard
use Cloudinary\Configuration\Configuration;
Configuration::instance([
  'cloud' => [
    'cloud_name' => 'my_cloud_name', 
    'api_key' => 'my_key', 
    'api_secret' => 'my_secret'],
  'url' => [
    'secure' => true]]);

The new SDK also supports multiple instances, each of which you can configure with a Cloudinary object. For example:

Copy to clipboard
use Cloudinary\Configuration\Configuration;
$cloudinary = new Cloudinary([
  'cloud' => [
    'cloud_name' => 'my_cloud_name',
    'api_key'  => 'my_key',
    'api_secret' => 'my_secret',
  'url' => [
    'secure' => true]]]);

Management APIs

PHP SDK v2 offers revamped Upload and Admin API methods for managing, organizing, and creating media assets. See the examples below.

To upload an asset to your account:

Copy to clipboard
use Cloudinary\Api\Upload\UploadApi;
(new UploadApi())->upload('my_image.jpg')

To list all the image tags that begin with shoe:

Copy to clipboard
use Cloudinary\Api\Admin\AdminApi;
$result = (new AdminApi())->tags(["prefix" => "shoe"]);

Documentation

PHP SDK v2 includes extensive documentation strings within the code, making it much easier to understand our code and its purpose. The strings are displayed within the IDE tooltips and automatically exported into an autogenerated PHP SDK Reference Guide, an addition to the Cloudinary PHP SDK User Guide.

Backward Compatibility and Migration

Be assured that all your existing URLs embedded in webpages will continue to work. Only the way in which PHP SDK v2 generates transformations has changed, not the output. So, just keep the URLs that are in place and start creating new ones with v2.

Even though the new syntax is much clearer, more logical, and more intuitive, a significant conceptual change does exist between the two syntaxes. Moving to a new SDK generally entails updating your code.

To help you with that update with minimal changes and without having to figure out the nuances and to translate your code to the new syntax, we’ve added a special action called fromParams with which you can enclose the code syntax in v1 of the SDK.

For example, this code in SDK v1—

Copy to clipboard
#SDK 1
cl_image_tag("actor", array("transformation"=>array(
  array("effect"=>"cartoonify"),
  array("radius"=>"max"),
  array("effect"=>"outline:100", "color"=>"lightblue"),
  array("background"=>"lightblue"),
  array("height"=>300, "crop"=>"scale")
)));

—becomes the following:

Copy to clipboard
#SDK 2
ImageTag::fromParams("actor", array("transformation"=>array(
  array("effect"=>"cartoonify"),
  array("radius"=>"max"),
  array("effect"=>"outline:100", "color"=>"lightblue"),
  array("background"=>"lightblue"),
  array("height"=>300, "crop"=>"scale")
)));

For more details and examples on upgrading to v2 of the SDK, see our PHP Migration Guide.

Summary

PHP SDK v2 offers an action-oriented, structured syntax; high discoverability; integrated autocomplete, an intuitive migration path; along with in-depth reference documentation. Consequently, coding to leverage Cloudinary’s capabilities is much easier. For details, check out our PHP documentation and the PHP Reference Guide.

Recent Blog Posts

Our $2B Valuation

By
Blackstone Growth Invests in Cloudinary

When we started our journey in 2012, we were looking to improve our lives as developers by making it easier for us to handle the arduous tasks of handling images and videos in our code. That initial line of developer code has evolved into a full suite of media experience solutions driven by a mission that gradually revealed itself over the course of the past 10 years: help companies unleash the full potential of their media to create the most engaging visual experiences.

Read more
Direct-to-Consumer E-Commerce Requires Compelling Visual Experiences

When brands like you adopt a direct–to-consumer (DTC) e-commerce approach with no involvement of retailers or marketplaces, you gain direct and timely insight into evolving shopping behaviors. Accordingly, you can accommodate shoppers’ preferences by continually adjusting your product offering and interspersing the shopping journey with moments of excitement and intrigue. Opportunities abound for you to cultivate engaging customer relationships.

Read more
Automatically Translating Videos for an International Audience

No matter your business focus—public service, B2B integration, recruitment—multimedia, in particular video, is remarkably effective in communicating with the audience. Before, making video accessible to diverse viewers involved tasks galore, such as eliciting the service of production studios to manually dub, transcribe, and add subtitles. Those operations were costly and slow, especially for globally destined content.

Read more
Cloudinary Helps Minted Manage Its Image-Generation Pipeline at Scale

Shoppers return time and again to Minted’s global online community of independent artists and designers because they know they can count on unique, statement-making products of the highest quality there. Concurrently, the visual imagery on Minted.com must do justice to the designs into which the creators have poured their hearts and souls. For Minted’s VP of Engineering David Lien, “Because we are a premium brand, we need to ensure that every single one of our product images matches the selected configuration exactly. For example, if you pick an 18x24 art print on blue canvas, we will show that exact combination on the hero images in the PDF.”

Read more
Highlights on ImageCon 2021 and a Preview of ImageCon 2022

New year, same trend! Visual media will continue to play a monumental role in driving online conversions. To keep up with visual-experience trends and best practices, Cloudinary holds an annual conference called ImageCon, a one-of-a-kind event that helps attendees create the most engaging visual experiences possible.

Read more