Laravel 5 cloudinary upload file and images with example

Laravel 5 cloudinary upload file and images with example

In this post we will give you information about Laravel 5 cloudinary upload file and images with example. Hear we will give you detail about Laravel 5 cloudinary upload file and images with exampleAnd how to use it also give you demo for it if it is necessary.

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(
  'JDCloudderCloudderServiceProvider'
);

'aliases' => array(
  'Cloudder' => 'JDCloudderFacadesCloudder'
);

Run following command to publish :

php artisan vendor:publish --provider="JDCloudderCloudderServiceProvider"

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 AppHttpControllers;
  3. use IlluminateHttpRequest;
  4. class CloudderController extends Controller
  5. {
  6.     public functiongetFile(){        
  7.         returnview('cloudder');
  8.     }
  9.     public functionuploadFile(Request $request){
  10.          if($request->hasFile('image_file')){
  11. Cloudder::upload($request->file('image_file'));
  12. $c=Cloudder::getResult();
  13. if($c){
  14. returnback()
  15.          ->with('success','You have successfully upload images.')
  16.          ->with('image',$c['url']);
  17. }
  18. }
  19.     }
  20. }
<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

class CloudderController extends Controller
{
	public function getFile(){		
		return view('cloudder');
	}

	public function uploadFile(Request $request){
	  	if($request->hasFile('image_file')){  
            Cloudder::upload($request->file('image_file'));
            $c=Cloudder::getResult();             
            if($c){
               return back()
		            ->with('success','You have successfully upload images.')
		            ->with('image',$c['url']);
            }

            
        }
	}

}



Create Blade File

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

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

</body>
</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)

Laravel 5.3 Upload Image with Validation example

Hope this code and post will helped you for implement Laravel 5 cloudinary upload file and images with example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

3  +  6  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US