Laravel – How to convert file(image, audio, video) extension using CloudConvert?

Laravel – How to convert file(image, audio, video) extension using CloudConvert?

In this post we will give you information about Laravel – How to convert file(image, audio, video) extension using CloudConvert?. Hear we will give you detail about Laravel – How to convert file(image, audio, video) extension using CloudConvert?And how to use it also give you demo for it if it is necessary.

We may sometimes require to convert file extension like if you have video type mov or flv or 3gp etc and you should have to convert it into mp4, OR if you have image type png, jpeg, gif and require to convert it into jpj, Same as for audio mp3, then all the thing you can do using CloudConvert API. you can also convert pdf into jpg using CloudConvert API.

In this example i use CloudConvert API with Laravel. CloudConvert is a very popular API for convert file extensions. So in this example i used CloudConvert API for any video extension convert into mp4. you can also convert anything as you want like any audio 3gp into mp3 etc.

So, you can convert any video extension into mp4, you have to just follow few step and get example:

1.Package Installation

In first step we have to install cloudconvert-laravel package using composer, so open your terminal and run bellow command:

composer require robbiep/cloudconvert-laravel

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

config/app.php

'providers' => [

....

RobbiePCloudConvertLaravelCloudConvertLaravelServiceProvider::class

],

One more after this, we have to publish configuration file of cloudconvert api file. Run bellow command:

php artisan vendor:publish

Now you have configuration file here config/cloudconvert.php, In this file you have to put your API key. If you haven’t API key then you can generate from here : cloudconvert.com And create new account, after email confirmation it will give you API key. you have to just copy that 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' => '',

]

);

2. Add Route

First you have add two route in routes.php file. first one for generate view and second one for post method. so let’s add bellow route in your routes.php file.

app/Http/routes.php

Route::get('fileUpload', function () {

return view('fileUpload');

});

Route::post('fileUpload', ['as'=>'fileUpload','uses'=>'HomeController@fileUpload']);


3. Add Controller Function

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

app/Http/Controllers/HomeController.php

namespace AppHttpControllers;


use IlluminateHttpRequest;

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

}


}

4. Create Blade File

In At Last we require to create view file for image or file uploading. so you can create fileUpload.blade.php and put following code in that file.

resources/views/fileUpload.blade.php

@extends('layouts.app')


@section('content')


@if (count($errors) > 0)

<div >

<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 >

<div >

{!! Form::file('video', array('class' => 'video')) !!}

</div>

<div >

<button type="submit" >Create</button>

</div>

</div>

{!! Form::close() !!}


@endsection

Now you can run, If you want to get more information about package then you can from here : cloudconvert-laravel.

Hope this code and post will helped you for implement Laravel – How to convert file(image, audio, video) extension using CloudConvert?. 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 *

14  +    =  23

We accept paid advance and paid guest Posting on our Site : Contact US