PHP integration
Overview
Cloudinary is a cloud-based service that provides an end-to-end image management solution including uploads, storage, manipulations, optimizations and delivery.
With Cloudinary you can easily upload images to the cloud, automatically perform smart image manipulations without installing any complex software. All your images are then seamlessly delivered through a fast CDN, optimized and using industry best practices. Cloudinary offers comprehensive APIs and administration capabilities and is easy to integrate with new and existing web and mobile applications.
Quick example
Take a look at the following Cloudinary URL that generates the image below:
Simply accessing the above URL told Cloudinary to transform an uploaded image, create a 150x150px thumbnail using face detection based cropping, round the image's corners, add a sepia effect, convert it to a transparent PNG format, add a watermark layer, rotate the image by 10 degrees and ultimately delivered the resulting image through a fast CDN using smart caching techniques.
PHP Library Features
Cloudinary provides an open source PHP library for further simplifying the integration:
- Build URLs for image transformation & manipulation.
- PHP view helper methods for embedding and transforming images.
- API wrappers: image upload, administration, sprite generation and more.
- Direct image uploading from the browser using a jQuery plugin.
The library was tested with PHP 5.3 and PHP 5.4. Cloudinary's PHP library is based on a generic code that can be used with PHP frameworks such as Yii, CodeIgniter, CakePHP, Zend, Symfony and others.
PHP - Getting started guide
1Installation
Cloudinary's PHP integration library is available as an open-source PHP code. You can download the latest sources from the following link:
https://github.com/cloudinary/cloudinary_php/tarball/master
Copy all files from the src folder into your PHP project and include Cloudinary's PHP classes in your code:
require 'Cloudinary.php'; require 'Uploader.php'; require 'Api.php';
If you use Composer to manage your PHP library dependency, you can install Cloudinary's PHP library directly from the Packagist repository. Update your composer.json file as follows:
{ "require": { "cloudinary/cloudinary_php": "dev-master" } }
Automatically install dependencies including Cloudinary's PHP package:
php composer.phar install
- https://github.com/cloudinary/cloudinary_php
- https://packagist.org/packages/cloudinary/cloudinary_php
2Configuration
Your cloud_name account parameter is required to build image URLs. api_key and api_secret are further needed to perform secure API calls to Cloudinary (e.g., image uploads). See Account and API setup for more details.
Setting the configuration parameters can be done either programmatically in each call to a Cloudinary method or globally using an environment variable or the config method.
You can find your configuration parameters in the dashboard of our Management Console.
Here's an example of setting configuration parameters globally in your PHP application:
\Cloudinary::config(array( "cloud_name" => "sample", "api_key" => "874837483274837", "api_secret" => "a676b67565c6767a6767d6767f676fe1" ));
See Configuration Options for more details and additional configuration methods.
3Upload images
You can upload images and any other files from your PHP server. Uploading is done over HTTPS using a secure protocol based on the api_key and api_secret parameters you provide.
The following command uploads a local file to Cloudinary:
\Cloudinary\Uploader::upload("/home/my_image.jpg")
Alternatively, you can a specify a local path, a public HTTP URL, an S3 URL or an actual image file's data. For example:
\Cloudinary\Uploader::upload("http://www.example.com/image.jpg")
Each image uploaded to Cloudinary is assigned a unique Public ID and is available for immediate delivery and transformation. The upload method returns an associative array with content similar to that shown in the following example:
Array ( [public_id] => sample [version] => 1312461204 [width] => 864 [height] => 576 [format] => jpg [bytes] => 120253 [url] => http://res.cloudinary.com/demo/image/upload/v1371281596/sample.jpg [secure_url] => https://res.cloudinary.com/demo/image/upload/v1371281596/sample.jpg )
As you can see in the following example, with a single call you can define your own Public ID, apply an incoming image transformation before storing the image in the cloud, generate derived images eagerly and assign tags to uploaded images:
\Cloudinary\Uploader::upload($_FILES["file"]["tmp_name"], array( "public_id" => "sample_id", "crop" => "limit", "width" => "2000", "height" => "2000", "eager" => array( array( "width" => 200, "height" => 200, "crop" => "thumb", "gravity" => "face", "radius" => 20, "effect" => "sepia" ), array( "width" => 100, "height" => 150, "crop" => "fit", "format" => "png" ) ), "tags" => array( "special", "for_homepage" ) ));
Many more upload options as well as direct image uploading from the browser are detailed here: PHP image upload.
Cloudinary supports uploading videos as well. See Upload videos documentation page for more details.
4Display and manipulate images
Adding images to your PHP view is quite straightforward. Just use the cl_image_tag helper method. This method generates the full image resource URL based on the given parameters and internally uses image tag to add the image to your HTML code:
For example, displaying the uploaded image with the sample public ID, while providing an alternate text:
<?php echo cl_image_tag("sample.jpg", array( "alt" => "Sample Image" )); ?>
Now, let's say that you wish to display a small thumbnail of this uploaded image. Simply add the transformation instructions to your call. For example, displaying the 'sample' image transformed to fill a 100x150 rectangle:
<?php echo cl_image_tag("sample.jpg", array( "width" => 100, "height" => 150, "crop" => "fill" )); ?>
This is equivalent to:
<img src='http://res.cloudinary.com/demo/image/upload/c_fill,h_150,w_100/sample.jpg' width='100' height='150' />
Using simple parameters you can perform powerful image manipulations. The PHP library builds Cloudinary URLs that you can embed in your web and mobile views for dynamically transforming your uploaded images in the cloud and delivering the results through a fast CDN with advanced caching.
You can easily convert image formats, resize your images, perform face detection based cropping, apply effects and filters, append textual layers or watermarks and more.
The following command, for example, embeds a JPG thumbnail of a profile photo fetched from Facebook in real-time, crops it to a circle, applies a sepia effect and delivers it optimized through a CDN:
<?php echo facebook_profile_image_tag("billclinton.jpg", array( "width" => 90, "height" => 98, "crop" => "fill", "gravity" => "face", "radius" => "max", "effect" => "sepia" )); ?>
For a full list of supported transformations and their usage, refer to Image transformations.
For more details about Cloudinary's image transformation and manipulation in PHP, see PHP image manipulation.
Videos can be delivered and manipulated as well. See Video manipulation and delivery documentation page for more details.
5Sample projects
To find additional useful code samples and learn how to integrate Cloudinary with your PHP applications, take a look at our Sample Projects.
Basic PHP sample: Uploading local and remote images to Cloudinary and generating various transformation URLs.
PHP Photo Album: A fully working web application that allows you to uploads photos, maintain a database with references to them, list them with their metadata, and display them using various cloud-based transformations. Image uploading is performed both from the server side and directly from the browser using a jQuery plugin.
6What's next
Sign up for a free account if you haven't done so already. Follow the steps above and try Cloudinary out. Finished all steps? That's just an example of what Cloudinary can offer. Here's some additional reading material to help you get the best out of Cloudinary:
Learn more about PHP image upload.
Explore powerful PHP image manipulation features and see our Image transformations docs.
Browse additional PHP integration topics: configuration, Admin API, etc.
Stay tuned for updates, tips and tutorials: Blog, Twitter, Facebook.