|
| 1 | +<div align="center"> |
| 2 | + <h1> Laravel Cloudinary </h1> |
| 3 | +</div> |
| 4 | + |
| 5 | +<p align="center"> |
| 6 | + <a href="https://packagist.org/packages/unicodeveloper/laravel-cloudinary"> |
| 7 | + <img src="https://img.shields.io/packagist/dt/unicodeveloper/laravel-cloudinary.svg?style=flat-square" alt="Total Downloads"> |
| 8 | + </a> |
| 9 | + <a href="https://packagist.org/packages/unicodeveloper/laravel-cloudinary"> |
| 10 | + <img src="https://poser.pugx.org/unicodeveloper/laravel-cloudinary/v/stable.svg" alt="Latest Stable Version"> |
| 11 | + </a> |
| 12 | + <a href="https://packagist.org/packages/unicodeveloper/laravel-cloudinary"> |
| 13 | + <img src="https://poser.pugx.org/unicodeveloper/laravel-cloudinary/license.svg" alt="License"> |
| 14 | + </a> |
| 15 | +</p> |
| 16 | + |
| 17 | +> A Laravel Package for uploading, optimizing, transforming and delivering media files with Cloudinary. Furthermore, it provides a fluent and expressive API to easily attach your media files to Eloquent models. |
| 18 | +
|
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +**Upload** a file (_Image_, _Video_ or any type of _File_) to **Cloudinary**: |
| 23 | + |
| 24 | +```php |
| 25 | + |
| 26 | +/** |
| 27 | +* Using the Cloudinary Facade |
| 28 | +*/ |
| 29 | + |
| 30 | +// Upload an Image File to Cloudinary with One line of Code |
| 31 | +$uploadedFileUrl = Cloudinary::upload($request->file('file')->getRealPath())->getSecurePath(); |
| 32 | + |
| 33 | +// Upload an Video File to Cloudinary with One line of Code |
| 34 | +$uploadedFileUrl = Cloudinary::uploadVideo($request->file('file')->getRealPath())->getSecurePath(); |
| 35 | + |
| 36 | +// Upload any File to Cloudinary with One line of Code |
| 37 | +$uploadedFileUrl = Cloudinary::uploadFile($request->file('file')->getRealPath())->getSecurePath(); |
| 38 | + |
| 39 | +/** |
| 40 | + * This package also exposes a helper function you can use if you are not a fan of Facades |
| 41 | + * Shorter, expressive, fluent using the |
| 42 | + * cloudinary() function |
| 43 | + */ |
| 44 | + |
| 45 | +// Upload an Image File to Cloudinary with One line of Code |
| 46 | +$uploadedFileUrl = cloudinary()->upload($request->file('file')->getRealPath())->getSecurePath(); |
| 47 | + |
| 48 | +// Upload an Video File to Cloudinary with One line of Code |
| 49 | +$uploadedFileUrl = cloudinary()->uploadVideo($request->file('file')->getRealPath())->getSecurePath(); |
| 50 | + |
| 51 | +// Upload any File to Cloudinary with One line of Code |
| 52 | +$uploadedFileUrl = cloudinary()->uploadFile($request->file('file')->getRealPath())->getSecurePath(); |
| 53 | + |
| 54 | +/** |
| 55 | + * You can also skip the Cloudinary Facade or helper method and laravel-ize your uploads by simply calling the |
| 56 | + * storeOnCloudinary() method on the file itself |
| 57 | + */ |
| 58 | + |
| 59 | +// Store the uploaded file on Cloudinary |
| 60 | +$result = $request->file('file')->storeOnCloudinary(); |
| 61 | + |
| 62 | +// Store the uploaded file on Cloudinary |
| 63 | +$result = $request->file->storeOnCloudinary(); |
| 64 | + |
| 65 | +// Store the uploaded file in the "lambogini" directory on Cloudinary |
| 66 | +$result = $request->file->storeOnCloudinary('lambogini'); |
| 67 | + |
| 68 | +// Store the uploaded file in the "lambogini" directory on Cloudinary with the filename "prosper" |
| 69 | +$result = $request->file->storeOnCloudinaryAs('lambogini', 'prosper'); |
| 70 | + |
| 71 | +$result->getPath(); // Get the url of the uploaded file; http |
| 72 | +$result->getSecurePath(); // Get the url of the uploaded file; https |
| 73 | +$result->getSize(); // Get the size of the uploaded file in bytes |
| 74 | +$result->getReadableSize(); // Get the size of the uploaded file in bytes, megabytes, gigabytes or terabytes. E.g 1.8 MB |
| 75 | +$result->getFileType(); // Get the type of the uploaded file |
| 76 | +$result->getFileName(); // Get the file name of the uploaded file |
| 77 | +$result->getOriginalFileName(); // Get the file name of the file before it was uploaded to Cloudinary |
| 78 | +$result->getPublicId(); // Get the public_id of the uploaded file |
| 79 | +$result->getExtension(); // Get the extension of the uploaded file |
| 80 | +$result->getWidth(); // Get the width of the uploaded file |
| 81 | +$result->getHeight(); // Get the height of the uploaded file |
| 82 | +$result->getTimeUploaded(); // Get the time the file was uploaded |
| 83 | +``` |
| 84 | + |
| 85 | +**Attach Files** to Laravel **Eloquent Models**: |
| 86 | + |
| 87 | +```php |
| 88 | +/** |
| 89 | + * How to attach a file to a Model by model creation |
| 90 | + */ |
| 91 | +$page = Page::create($this->request->input()); |
| 92 | +$page->attachMedia($file); // Example of $file is $request->file('file'); |
| 93 | + |
| 94 | +/** |
| 95 | + * How to attach a file to a Model by retreiving model records |
| 96 | + */ |
| 97 | +$page = Page::find(2); |
| 98 | +$page->attachMedia($file); // Example of $file is $request->file('file'); |
| 99 | + |
| 100 | +/** |
| 101 | + * How to retrieve files that were attached to a Model |
| 102 | + */ |
| 103 | +$filesBelongingToSecondPage = Page::find(2)->fetchAllMedia(); |
| 104 | + |
| 105 | +/** |
| 106 | + * How to retrieve the first file that was attached to a Model |
| 107 | + */ |
| 108 | +$fileBelongingToSecondPage = Page::find(2)->fetchFirstMedia(); |
| 109 | + |
| 110 | +/** |
| 111 | + * How to retrieve the last file that was attached to a Model |
| 112 | + */ |
| 113 | +$fileBelongingToSecondPage = Page::find(2)->fetchLastMedia(); |
| 114 | + |
| 115 | +/** |
| 116 | + * How to replace/update files attached to a Model |
| 117 | + */ |
| 118 | +$page = Page::find(2); |
| 119 | +$page->updateMedia($file); // Example of $file is $request->file('file'); |
| 120 | + |
| 121 | +/** |
| 122 | +* How to detach a file from a Model |
| 123 | +*/ |
| 124 | +$page = Page::find(2); |
| 125 | +$page->detachMedia($file) // Example of $file is $request->file('file'); |
| 126 | +``` |
| 127 | + |
| 128 | +**Upload Files Via An Upload Widget**: |
| 129 | + |
| 130 | +Use the `x-cld-upload-button` Blade upload button component that ships with this Package like so: |
| 131 | +``` |
| 132 | +<!DOCTYPE html> |
| 133 | +<html> |
| 134 | + <head> |
| 135 | + ... |
| 136 | + @cloudinaryJS |
| 137 | + </head> |
| 138 | + <body> |
| 139 | + <x-cld-upload-button> |
| 140 | + Upload Files |
| 141 | + </x-cld-upload-button> |
| 142 | + </body> |
| 143 | +</html> |
| 144 | +```` |
| 145 | +
|
| 146 | +Other Blade components you can use are: |
| 147 | +
|
| 148 | +```php |
| 149 | +<x-cld-image public-id="prosper" width="300" height="300"></x-cld-image> // Blade Image Component for displaying images |
| 150 | +
|
| 151 | +<x-cld-video public-id="awesome"></x-cld-video> // Blade Video Component for displaying videos |
| 152 | +``` |
| 153 | + |
| 154 | +**Media Management via The Command Line**: |
| 155 | + |
| 156 | +```bash |
| 157 | +/** |
| 158 | +* Back up Files on Cloudinary |
| 159 | +*/ |
| 160 | +php artisan cloudinary:backup |
| 161 | + |
| 162 | +/** |
| 163 | + * Delete a File on Cloudinary |
| 164 | + */ |
| 165 | +php artisan cloudinary:delete |
| 166 | + |
| 167 | +/** |
| 168 | + * Fetch a File from Cloudinary |
| 169 | + */ |
| 170 | +php artisan cloudinary:fetch |
| 171 | + |
| 172 | +/** |
| 173 | + * Rename a File from Cloudinary |
| 174 | + */ |
| 175 | +php artisan cloudinary:rename |
| 176 | + |
| 177 | +/** |
| 178 | + * Upload a File to Cloudinary |
| 179 | + */ |
| 180 | +php artisan cloudinary:upload |
| 181 | +``` |
| 182 | + |
| 183 | + |
| 184 | +## Installation |
| 185 | + |
| 186 | +[PHP](https://php.net) 7.0+, and [Composer](https://getcomposer.org) are required. |
| 187 | + |
| 188 | +To get the latest version of Laravel Cloudinary, simply require it: |
| 189 | + |
| 190 | +```bash |
| 191 | +composer require unicodeveloper/laravel-cloudinary |
| 192 | +``` |
| 193 | + |
| 194 | +Or add the following line to the require block of your `composer.json` file. |
| 195 | + |
| 196 | +``` |
| 197 | +"unicodeveloper/laravel-cloudinary": "1.0.0-beta" |
| 198 | +``` |
| 199 | + |
| 200 | +You'll then need to run `composer install` or `composer update` to download it and have the autoloader updated. |
| 201 | + |
| 202 | + |
| 203 | +Once Laravel Cloudinary is installed, you need to register the service provider. Open up `config/app.php` and add the following to the `providers` key. |
| 204 | + |
| 205 | +```php |
| 206 | +'providers' => [ |
| 207 | + ... |
| 208 | + Unicodeveloper\Cloudinary\CloudinaryServiceProvider::class, |
| 209 | + ... |
| 210 | +] |
| 211 | +``` |
| 212 | + |
| 213 | +> Note: If you use **Laravel >= 5.5** you can skip this step (adding the code above to the providers key) and go to [**`configuration`**](https://github.com/unicodeveloper/laravel-cloudinary#configuration) |
| 214 | +
|
| 215 | +Also, register the Cloudinary Facade like so: |
| 216 | + |
| 217 | +```php |
| 218 | +'aliases' => [ |
| 219 | + ... |
| 220 | + 'Cloudinary' => Unicodeveloper\Cloudinary\Facades\Cloudinary::class, |
| 221 | + ... |
| 222 | +] |
| 223 | +``` |
| 224 | + |
| 225 | +## Configuration |
| 226 | + |
| 227 | +You can publish the configuration file using this command: |
| 228 | + |
| 229 | +```bash |
| 230 | +php artisan vendor:publish --provider="Unicodeveloper\Cloudinary\CloudinaryServiceProvider" |
| 231 | +``` |
| 232 | + |
| 233 | +A configuration-file named `cloudinary.php` with some sensible defaults will be placed in your `config` directory: |
| 234 | + |
| 235 | +```php |
| 236 | +<?php |
| 237 | +return [ |
| 238 | + /* |
| 239 | + |-------------------------------------------------------------------------- |
| 240 | + | Cloudinary Configuration |
| 241 | + |-------------------------------------------------------------------------- |
| 242 | + | |
| 243 | + | An HTTP or HTTPS URL to notify your application (a webhook) when the process of uploads, deletes, and any API |
| 244 | + | that accepts notification_url has completed. |
| 245 | + | |
| 246 | + | |
| 247 | + */ |
| 248 | + 'notification_url' => env('CLOUDINARY_NOTIFICATION_URL'), |
| 249 | + |
| 250 | + |
| 251 | + /* |
| 252 | + |-------------------------------------------------------------------------- |
| 253 | + | Cloudinary Configuration |
| 254 | + |-------------------------------------------------------------------------- |
| 255 | + | |
| 256 | + | Here you may configure your Cloudinary settings. Cloudinary is a cloud hosted |
| 257 | + | media management service for all file uploads, storage, delivery and transformation needs. |
| 258 | + | |
| 259 | + | |
| 260 | + */ |
| 261 | + 'cloud_url' => env('CLOUDINARY_URL'), |
| 262 | + |
| 263 | + /** |
| 264 | + * Upload Preset From Cloudinary Dashboard |
| 265 | + * |
| 266 | + */ |
| 267 | + 'upload_preset' => env('CLOUDINARY_UPLOAD_PRESET') |
| 268 | +]; |
| 269 | +``` |
| 270 | + |
| 271 | +### API Keys |
| 272 | +Open your `.env` file and add your API Environment variable, upload_preset (this is optional, until you need to use the widget) like so: |
| 273 | + |
| 274 | +```php |
| 275 | +CLOUDINARY_URL=xxxxxxxxxxxxx |
| 276 | +CLOUDINARY_UPLOAD_PRESET=xxxxxxxxxxxxx |
| 277 | +CLOUDINARY_NOTIFICATION_URL= |
| 278 | +``` |
| 279 | + |
| 280 | +***Note:** You need to get these credentials from your [Cloudinary Dashboard](https://cloudinary.com/console)* |
| 281 | + |
| 282 | +*If you are using a hosting service like heroku,forge,digital ocean, etc, please ensure to add the above details to your configuration variables.* |
| 283 | + |
| 284 | +### Cloudinary JS |
| 285 | + |
| 286 | +Cloudinary relies on its own JavaScript library to initiate the Cloudinary Upload Widget. You can load the JavaScript library by placing the @cloudinaryJS directive right before your application layout's closing </head> tag: |
| 287 | + |
| 288 | +```html |
| 289 | +<head> |
| 290 | + ... |
| 291 | + |
| 292 | + @cloudinaryJS |
| 293 | +</head> |
| 294 | +``` |
| 295 | + |
| 296 | +***Note:** ONLY LOAD THIS IF YOU HAVE DECIDED TO USE THE UPLOAD WIDGET. IF YOU ARE USING THIS PACKAGE FOR A LARAVEL API BACKEND, YOU DON'T NEED TO DO THIS!* |
| 297 | + |
| 298 | +## License |
| 299 | + |
| 300 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments