|
| 1 | +# Cloudinary PHP SDK |
| 2 | + |
1 | 3 | [](https://github.com/cloudinary/cloudinary_php/actions/workflows/test.yaml) |
2 | | -[](https://github.com/cloudinary/cloudinary_php/blob/master/LICENSE) |
3 | | -[](https://packagist.org/packages/cloudinary/cloudinary_php) |
4 | | -[](https://packagist.org/packages/cloudinary/cloudinary_php/stats) |
| 4 | +[](https://github.com/cloudinary/cloudinary_php/blob/master/LICENSE) |
| 5 | +[](https://packagist.org/packages/cloudinary/cloudinary_php) |
5 | 6 |
|
6 | | -Cloudinary PHP SDK |
7 | | -================== |
| 7 | +The `cloudinary/cloudinary_php` package is the server-side Cloudinary SDK for PHP. Use it on a server or in a build step to upload assets, build transformation and delivery URLs, and call the Admin API. It holds the API secret, so it handles the operations that can't run in a browser: signed uploads, signed delivery URLs, and asset administration. The current release (3.x) requires PHP 8.0 or later. |
8 | 8 |
|
9 | | -## About |
| 9 | +## Installation |
10 | 10 |
|
11 | | -The Cloudinary PHP SDK allows you to quickly and easily integrate your application with Cloudinary. |
12 | | -Effortlessly optimize, transform, upload and manage your cloud's assets. |
| 11 | +```bash |
| 12 | +composer require "cloudinary/cloudinary_php" |
| 13 | +``` |
13 | 14 |
|
14 | | -#### Note |
| 15 | +This pulls in the bundled transformation builder (`cloudinary/transformation-builder-sdk`) automatically. |
15 | 16 |
|
16 | | -This Readme provides basic installation and usage information. |
17 | | -For the complete documentation, see the [PHP SDK Guide](https://cloudinary.com/documentation/php_integration). |
| 17 | +## Configuration |
18 | 18 |
|
19 | | -## Table of Contents |
| 19 | +Construct a `Cloudinary` instance with no arguments and it reads credentials from the `CLOUDINARY_URL` environment variable: |
20 | 20 |
|
21 | | -- [Key Features](#key-features) |
22 | | -- [Version Support](#Version-Support) |
23 | | -- [Installation](#installation) |
24 | | -- [Usage](#usage) |
25 | | - - [Setup](#Setup) |
26 | | - - [Transform and Optimize Assets](#Transform-and-Optimize-Assets) |
| 21 | +```bash |
| 22 | +CLOUDINARY_URL=cloudinary://<API_KEY>:<API_SECRET>@<CLOUD_NAME> |
| 23 | +``` |
27 | 24 |
|
28 | | -## Key Features |
| 25 | +```php |
| 26 | +require 'vendor/autoload.php'; |
29 | 27 |
|
30 | | -- [Transform](https://cloudinary.com/documentation/php_video_manipulation#video_transformation_examples) and |
31 | | - [optimize](https://cloudinary.com/documentation/php_image_manipulation#image_optimizations) assets. |
32 | | -- Generate [image](https://cloudinary.com/documentation/php_image_manipulation#deliver_and_transform_images) and |
33 | | - [video](https://cloudinary.com/documentation/php_video_manipulation#php_video_transformation_code_examples) tags. |
34 | | -- [Asset Management](https://cloudinary.com/documentation/php_asset_administration). |
35 | | -- [Secure URLs](https://cloudinary.com/documentation/video_manipulation_and_delivery#generating_secure_https_urls_using_sdks). |
| 28 | +use Cloudinary\Cloudinary; |
36 | 29 |
|
37 | | -## Version Support |
| 30 | +$cloudinary = new Cloudinary(); // credentials come from CLOUDINARY_URL in the environment |
| 31 | +``` |
38 | 32 |
|
39 | | -| SDK Version | PHP 5.4 | PHP 5.5 | PHP 5.6 | PHP 7.x | PHP 8.0 - 8.3 | PHP 8.4 | |
40 | | -|-------------|---------|---------|---------|---------|---------------|---------| |
41 | | -| 3.x | ✘ | ✘ | ✘ | ✘ | ✔ | ✔ | |
42 | | -| 2.x | ✘ | ✘ | ✔ | ✔ | ✔ | ✘ * | |
43 | | -| 1.x | ✔ | ✔ | ✔ | ✔ | ✘ | ✘ | |
| 33 | +To set them in code instead, pass a configuration array: |
44 | 34 |
|
45 | | -\* Deprecation warnings |
| 35 | +```php |
| 36 | +require 'vendor/autoload.php'; |
46 | 37 |
|
47 | | -## Installation |
| 38 | +use Cloudinary\Cloudinary; |
48 | 39 |
|
49 | | -```bash |
50 | | -composer require "cloudinary/cloudinary_php" |
| 40 | +$cloudinary = new Cloudinary([ |
| 41 | + 'cloud' => [ |
| 42 | + 'cloud_name' => 'my_cloud_name', |
| 43 | + 'api_key' => 'my_key', |
| 44 | + 'api_secret' => 'my_secret', |
| 45 | + ], |
| 46 | +]); |
51 | 47 | ``` |
52 | 48 |
|
53 | | -# Usage |
54 | | - |
55 | | -### Migration |
| 49 | +Keep the API secret on the server. Don't put it in client-side code or commit it to version control. |
56 | 50 |
|
57 | | -See the [Cloudinary PHP SDK Migration guide](https://cloudinary.com/documentation/php2_migration) for more information |
58 | | -on migrating to this version of the PHP SDK. |
| 51 | +## Quick examples |
59 | 52 |
|
60 | | -The previous (1.x) version of the SDK is located [here](https://github.com/cloudinary/cloudinary_php/tree/support/1.x). |
| 53 | +### Upload a file |
61 | 54 |
|
62 | | -### Setup |
| 55 | +`uploadApi()->upload()` takes a local path, a remote HTTP/HTTPS URL, raw data, or a base64 data URI as its first argument. It returns an array-accessible `ApiResponse` that includes `public_id` and `secure_url`: |
63 | 56 |
|
64 | 57 | ```php |
| 58 | +require 'vendor/autoload.php'; |
| 59 | + |
65 | 60 | use Cloudinary\Cloudinary; |
66 | 61 |
|
67 | | -$cloudinary = new Cloudinary(); |
| 62 | +$cloudinary = new Cloudinary(); // credentials come from CLOUDINARY_URL in the environment |
| 63 | + |
| 64 | +$result = $cloudinary->uploadApi()->upload('my_image.jpg', [ |
| 65 | + 'public_id' => 'cms/hero', // optional: where the asset lives in your media library |
| 66 | +]); |
| 67 | + |
| 68 | +echo $result['public_id'], ' ', $result['secure_url']; |
68 | 69 | ``` |
69 | 70 |
|
70 | | -### Transform and Optimize Assets |
| 71 | +### Transform and optimize a delivery URL |
71 | 72 |
|
72 | | -- [See full documentation](https://cloudinary.com/documentation/php_image_manipulation). |
| 73 | +`image()` returns a builder you can cast to a string — no network call. This resizes to a 100x150 fill crop and lets Cloudinary pick the format and quality for the requesting browser (`f_auto`, `q_auto`): |
73 | 74 |
|
74 | 75 | ```php |
75 | | -$cloudinary->image('sample.jpg')->resize(Resize::fill()->width(100)->height(150))->format(Format::auto()); |
76 | | -``` |
| 76 | +require 'vendor/autoload.php'; |
77 | 77 |
|
78 | | -### Upload |
| 78 | +use Cloudinary\Cloudinary; |
| 79 | +use Cloudinary\Transformation\Resize; |
| 80 | +use Cloudinary\Transformation\Format; |
| 81 | +use Cloudinary\Transformation\Quality; |
79 | 82 |
|
80 | | -- [See full documentation](https://cloudinary.com/documentation/php_image_and_video_upload). |
81 | | -- [Learn more about configuring your uploads with upload presets](https://cloudinary.com/documentation/upload_presets). |
| 83 | +$cloudinary = new Cloudinary(); |
82 | 84 |
|
83 | | -```php |
84 | | -$cloudinary->uploadApi->upload('my_image.jpg'); |
| 85 | +echo $cloudinary->image('sample.jpg') |
| 86 | + ->resize(Resize::fill()->width(100)->height(150)) |
| 87 | + ->format(Format::auto()) |
| 88 | + ->quality(Quality::auto()); |
| 89 | +// https://res.cloudinary.com/demo/image/upload/c_fill,h_150,w_100/f_auto/q_auto/sample.jpg |
85 | 90 | ``` |
86 | 91 |
|
87 | | -### Security options |
| 92 | +### Retrieve asset details |
88 | 93 |
|
89 | | -- [See full documentation](https://cloudinary.com/documentation/solution_overview#security). |
| 94 | +`adminApi()->asset()` takes a public ID and returns the asset's metadata, including its format, dimensions, and `secure_url`: |
90 | 95 |
|
91 | | -## Contributions |
| 96 | +```php |
| 97 | +require 'vendor/autoload.php'; |
92 | 98 |
|
93 | | -- Ensure tests run locally |
94 | | -- Open a PR and ensure Travis tests pass |
| 99 | +use Cloudinary\Cloudinary; |
95 | 100 |
|
96 | | -## Get Help |
| 101 | +$cloudinary = new Cloudinary(); // credentials come from CLOUDINARY_URL in the environment |
97 | 102 |
|
98 | | -If you run into an issue or have a question, you can either: |
| 103 | +$asset = $cloudinary->adminApi()->asset('sample'); |
99 | 104 |
|
100 | | -- Issues related to the SDK: [Open a GitHub issue](https://github.com/cloudinary/cloudinary_php/issues). |
101 | | -- Issues related to your account: [Open a support ticket](https://cloudinary.com/contact) |
| 105 | +echo $asset['format'], ' ', $asset['width'], 'x', $asset['height'], ' ', $asset['secure_url']; |
| 106 | +``` |
102 | 107 |
|
103 | | -## About Cloudinary |
| 108 | +## For AI agents |
104 | 109 |
|
105 | | -Cloudinary is a powerful media API for websites and mobile apps alike, Cloudinary enables developers to efficiently |
106 | | -manage, transform, optimize, and deliver images and videos through multiple CDNs. Ultimately, viewers enjoy responsive |
107 | | -and personalized visual-media experiences—irrespective of the viewing device. |
| 110 | +`cloudinary/cloudinary_php` is the PHP server-side SDK. Choose it for backend upload, asset administration, search, and signed URL or tag generation, where the API secret stays private. The API surfaces are methods, not properties: call `$cloudinary->uploadApi()->upload(...)`, not `$cloudinary->uploadApi->upload(...)`. For other Cloudinary tasks, choose a different package: |
108 | 111 |
|
109 | | -## Additional Resources |
| 112 | +| Task | Package | |
| 113 | +|---|---| |
| 114 | +| Build transformations at a lower level (already bundled here) | [`cloudinary/transformation-builder-sdk`](https://github.com/cloudinary/php-transformation-builder-sdk) | |
| 115 | +| Build delivery URLs with a low-level PHP helper | [`php-url-builder`](https://github.com/cloudinary/php-url-builder) | |
| 116 | +| Build delivery URLs in the browser | [`@cloudinary/url-gen`](https://github.com/cloudinary/js-url-gen) | |
| 117 | +| Run Cloudinary operations as agent tools | [Cloudinary MCP servers](https://github.com/cloudinary/mcp-servers) | |
110 | 118 |
|
111 | | -- [Cloudinary Transformation and REST API References](https://cloudinary.com/documentation/cloudinary_references): |
112 | | - Comprehensive references, including syntax and examples for all SDKs. |
113 | | -- [MediaJams.dev](https://mediajams.dev/): Bite-size use-case tutorials written by and for Cloudinary Developers |
114 | | -- [DevJams](https://www.youtube.com/playlist?list=PL8dVGjLA2oMr09amgERARsZyrOz_sPvqw): Cloudinary developer podcasts on |
115 | | - YouTube. |
116 | | -- [Cloudinary Academy](https://training.cloudinary.com/): Free self-paced courses, instructor-led virtual courses, and |
117 | | - on-site courses. |
118 | | -- [Code Explorers and Feature Demos](https://cloudinary.com/documentation/code_explorers_demos_index): A one-stop shop |
119 | | - for all code explorers, Postman collections, and feature demos found in the docs. |
120 | | -- [Cloudinary Roadmap](https://cloudinary.com/roadmap): Your chance to follow, vote, or suggest what Cloudinary should |
121 | | - develop next. |
122 | | -- [Cloudinary Facebook Community](https://www.facebook.com/groups/CloudinaryCommunity): Learn from and offer help to |
123 | | - other Cloudinary developers. |
124 | | -- [Cloudinary Account Registration](https://cloudinary.com/users/register/free): Free Cloudinary account registration. |
125 | | -- [Cloudinary Website](https://cloudinary.com): Learn about Cloudinary's products, partners, customers, pricing, and |
126 | | - more. |
| 119 | +## Links |
127 | 120 |
|
128 | | -## Licence |
| 121 | +- [PHP SDK guide](https://cloudinary.com/documentation/php_integration) |
| 122 | +- [Upload](https://cloudinary.com/documentation/php_image_and_video_upload) |
| 123 | +- [Asset administration (Admin API)](https://cloudinary.com/documentation/php_asset_administration) |
| 124 | +- [Search API](https://cloudinary.com/documentation/search_api) |
| 125 | +- [Transformation and API references](https://cloudinary.com/documentation/cloudinary_references) |
| 126 | +- [Documentation llms.txt index](https://cloudinary.com/documentation/llms.txt) |
| 127 | +- [Package on Packagist](https://packagist.org/packages/cloudinary/cloudinary_php) |
129 | 128 |
|
130 | 129 | Released under the MIT license. |
0 commit comments