Laravel 5 cloudinary upload file and images with example

Laravel 5 cloudinary upload file and images with example

Laravel 5 cloudinary upload file and images with example

In this tutorial, I will let you know how to upload files and images on cloudinary server.

Cloudinary provide best service for image and video manipulation on the fly. There is open source PHP library provided by Cloudinary.

There are lots of benefits to store your media files, images and videos on the Cloudinary that is cloud based service.

Main Features
  • Cloudinary deliver the responsive images.
  • Easily optimize the images.
  • Media files will be delivered through a fast CDN.
  • Easily integrate with the web and mobile application.

There is cool feature of cropping images on the fly.

In this example, we will upload the images on cloudinary using jrm2k6/cloudder package.

Install jrm2k6/cloudder

First, I will install jrm2k6/cloudder package using composer.

composer require jrm2k6/cloudder:0.4.*

Now add the following line with cloudinary information in your .env file.

CLOUDINARY_API_KEY=`your key`
CLOUDINARY_API_SECRET=`your secret`
CLOUDINARY_CLOUD_NAME=`your cloud name`

You will get the cloudinary information from Cloudinary.

Now open config/app.php file and add following service provider and aliases in respective array :

'providers' => array(
  'JD\Cloudder\CloudderServiceProvider'
);

'aliases' => array(
  'Cloudder' => 'JD\Cloudder\Facades\Cloudder'
);

Run following command to publish :

php artisan vendor:publish --provider="JD\Cloudder\CloudderServiceProvider"

After running above command, you will get cloudder configuration file in following path config/cloudder.php

There you can change the following settings :

CLOUDINARY_BASE_URL
CLOUDINARY_SECURE_URL
CLOUDINARY_API_BASE_URL
Add Routes

In this step, add following routes in your routes/web.php file.

Route::get('get-file', 'CloudderController@getFile');
Route::post('upload-file', ['as'=>'upload-file','uses'=>'CloudderController@uploadFile']);
Create CloudderController

Now create "CloudderController" in following path app/Http/Controllers to handle the request.

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. class CloudderController extends Controller
  5. {
  6.     public function getFile(){        
  7.         return view('cloudder');
  8.     }
  9.     public function uploadFile(Request $request){
  10.          if($request->hasFile('image_file')){
  11. \Cloudder::upload($request->file('image_file'));
  12. $c=\Cloudder::getResult();
  13. if($c){
  14. return back()
  15.          ->with('success','You have successfully upload images.')
  16.          ->with('image',$c['url']);
  17. }
  18. }
  19.     }
  20. }
Create Blade File

Finally create a "cloudder.blade.php" file to render HTML form to upload files.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Laravel 5 cloudinary upload file and images with example</title>
  5. <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  6. </head>
  7. <body>
  8. @if ($message = Session::get('success'))
  9. <div class="alert alert-success alert-block">
  10. <button type="button" class="close" data-dismiss="alert">×</button>
  11. <strong>{{ $message }}</strong>
  12. </div>
  13. <img src="{{ Session::get('image') }}">
  14. @endif
  15. {!! Form::open(array('route' => 'upload-file','files'=>true)) !!}
  16. <div class="row">
  17. <div class="col-md-6">
  18. {!! Form::file('image_file', array('class' => 'form-control')) !!}
  19. </div>
  20. <div class="col-md-6">
  21. <button type="submit" class="btn btn-success">Upload</button>
  22. </div>
  23. </div>
  24. {!! Form::close() !!}
  25. </body>
  26. </html>

$c=\Cloudder::getResult(); will return following output :

array:15 [▼
  "public_id" => "sample"
  "version" => 1499596361
  "width" => 864
  "height" => 576
  "format" => "jpg"
  "resource_type" => "image"
  "created_at" => "2017-07-09T10:32:41Z"
  "tags" => []
  "bytes" => 120253
  "type" => "upload"
  "etag" => "14500e08ec2701bfd36a8e9a5585261e"
  "url" => "http://res.cloudinary.com/demo/image/upload/v1499589454/sample.jpg"
  "secure_url" => "http://res.cloudinary.com/demo/image/upload/v1499589454/sample.jpg"
  "original_filename" => "sample"
]

You can save required details into your database. You can use public id to destroy images in following way :

Cloudder::destroyImage($publicId, array $options)
Cloudder::delete($publicId, array $options)

Phone: (+91) 8800417876
Noida, 201301