How to convert file extension using CloudConvert in laravel?

Today now in this post i will show you How to convert file(image, audio, video) extension by using CloudConvert in Laravel? Sometimes we need to convert file extension as like if we have any video type mov or flv or 3gp etc and we need to convert it into mp4, OR if we have any image type png, jpeg, gif and need to convert it into jpj, And also same as for audio mp3, then all the thing we can also do by using CloudConvert API. We can also convert any pdf into jpg by using CloudConvert API.

So it might be help you also with version laravel 6, laravel 7 and laravel 8 any one.

Now in this example i will use CloudConvert API also with Laravel application. We know CloudConvert is a very popular API for the convert file extensions. So now in this example i will used CloudConvert API for any video extension to convert into mp4. Here we can also convert anything as we want as like any audio 3gp into any mp3 etc.

So, we can convert any video extension into mp4, we just need to follow the few step and get example:

Package Installation

Now in first step we need to install cloudconvert-laravel package by using composer, so need to open our terminal and then run bellow command:

composer require robbiep/cloudconvert-laravel

After the install this package, Now need to open config/app.php file and then add service provider.

config/app.php

'providers' => [
	....
	RobbieP\CloudConvertLaravel\CloudConvertLaravelServiceProvider::class
],

Now one more things after this, we need to publish configuration file for cloudconvert api file. Just need to run bellow command:

php artisan vendor:publish

Now we need configuration the file here config/cloudconvert.php, In this file we need to put our API key. If we hasn’t any API key then we need generate from here : cloudconvert.com And after create new account, after email confirmation it will give us API key. We have to just copy that the key and put here:

config/cloudconvert.php

return array(
	'api_key' => 'Your API Key',
	's3' => [
		'accesskeyid' => '',
		'secretaccesskey' => '',
		'bucket' => '',
		'acl' => '',
		'region' => ''
	],
	'ftp' => [
		'host' => '',
		'user' => '',
		'password' => '',
		'port' => 21,
		'dir' => '',
	]
);
Add Route

Now first we need to add two route in web.php file. At first one for the generate view and second one for the post method. So let’s add the bellow route in our web.php file.

routes/web.php

Route::get('fileUpload', function () {
    return view('fileUpload');
});
Route::post('fileUpload', ['as'=>'fileUpload','uses'=>'HomeController@fileUpload']);
Add Controller Function

Ok, now we have to add the fileUpload() function in HomeController.php file. If we don’t have HomeController then we need to create new controller and then need to put bellow code on that file. We must have one folder in public/videos with full permission because for every video will upload in videos folder.

app/Http/Controllers/HomeController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use CloudConvert;
class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function fileUpload(Request $request)
    {
        $this->validate($request, [
            'video' => 'required|mimes:mp4,avi,asf,mov,qt,avchd,flv,swf,mpg,mpeg,mpeg-4,wmv,divx,3gp|max:20480',
        ]);
        $videotmp = time();
        $video = $request->file('video');
        $input['video'] = $videotmp.'.'.$video->getClientOriginalExtension();
        $destinationPath = public_path('/videos');
        $video->move($destinationPath, $input['video']);
        if($video->getClientOriginalExtension() != "mp4"){
            CloudConvert::file($destinationPath.'/'.$input['video'])->to('mp4');
            File::delete($destinationPath.'/'. $input['video']);
            $input['video'] = $videotmp.'.mp4';
        }
        return back()->with('success','Video Upload successful');
    }
}
Create Blade File

Now in the Last we need to create new view file for image or any file uploading. So we need to create fileUpload.blade.php and then put the following code in that file.

resources/views/fileUpload.blade.php

@extends('layouts.app')
@section('content')
@if (count($errors) > 0)
	<div class="alert alert-danger">
		<strong>Whoops!</strong> There were some problems with your input.<br><br>
		<ul>
			@foreach ($errors->all() as $error)
				<li>{{ $error }}</li>
			@endforeach
		</ul>
	</div>
@endif

{!! Form::open(array('route' => 'fileUpload','enctype' => 'multipart/form-data')) !!}
	<div class="row cancel">
		<div class="col-md-4">
			{!! Form::file('video', array('class' => 'video')) !!}
		</div>
		<div class="col-md-4">
			<button type="submit" class="btn btn-success">Create</button>
		</div>
	</div>
{!! Form::close() !!}


@endsection

Read Also : How to Create Custom Blade Directive in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →