How to Convert file(image, video, Audio) extension using CloudConvert API?

We sometimes require to convert all over file extension like if you have video,How To Convert File(Image, Video, Audio) Extension Using CloudConvert API? image type and you want to convert flv to mp4, convert 3gp to mp4 OR if you have image type jpeg, gif and require to convert it into png, jpeg, Same as for audio mp3, then all the thing you can do working CloudConvert API.

CloudConvert API is very popular website for convert file use extension. They provide helping us api to convert file extension like image into pdf. So it will be pretty easy.How To Convert File(Image, Video, Audio) Extension Using CloudConvert API? So today we are going to make simple example for convert image into pdf in PHP.

I am going to give you very basic example to do this example without using composer package. we will directly use auto load file from git and use it in this example. In this example we have to do as listed things:

1. Download Autoload File

2. Create Index.php file

3. Create indexPremium.php file

4. Create uploads folder

So let’s follow bellow step:

Step 1: Download Autoload File

In first step we have to download code autoload file from below link from GitHub, So first let’s download from here :
Click Here to download cloudconvert-php.phar


After download you have to put it on your root folder.

Step 2: Create Index.php File

Index.php

<!DOCTYPE html>
<html>
<head>
	<title>PHP - Cloudconvert API Example Laratuto.com</title>
	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>


<div class="container">
	<form method="POST" action="indexPremium.php" enctype="multipart/form-data">
        <div class="form-group">
            <label>Add Image:</label>
            <input type="file" name="file" class="form-control">
        </div>
        <div class="form-group">
            <button class="btn btn-success">Submit</button>
        </div>
    </form>
</div>
</body>
</html>

Step 3: Create indexPremium.php File

Ok in the last step, we have to make indexPremium.php file on root directory. In this file we will write code of convert file extension using cloudconvert api, So you have to generate api key from bellow link download code:

cloudconvert.com

indexPremium.php

<?php


require 'phar://cloudconvert-php.phar/vendor/autoload.php';


use \CloudConvert\Api;


$api_key = new Api("API_KEY_HERE");


if(!empty($_FILES["file"])){

 try {

    $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    $uploadFilePath = 'uploads/lara.'.$ext;
    move_uploaded_file($_FILES['file']['temp_name'], $uploadFilePath);

    $api->convert([
      'inputformat' => $ext,
      'outputformat' => 'pdf',
      'input' => 'upload',
      'file' => fopen($uploadFilePath, 'r'),
    ])
    ->wait()
    ->download('uploads/lara.pdf');


    } catch (Exception $e) {
           echo "Something else went wrong: " . $e->getMessage() . "\n";
    }


    print_r("File format changed successfully.");
    exit;

 } else{

  print_r("Files require");
  exit;

}

?>

Step 4: Create uploads folder

In this finaly last step, we have to create “uploads” folder on your project root directory, we will store input image and also store output file on this folder. After getting successfully message, you can now check output.pdf file on this folder.

So let’s create newly folder uploads with proper permission.

Ok, now we are ready to run this tutoria;, so just run bellow command on root folder for run your working code.

http:localhost:8000

Now you can fire bellow url on your browser:

After Viewing Successfully message see here for output : uploads/lara.pdf.

I hope it can help you developer……………

1 thought on “How to Convert file(image, video, Audio) extension using CloudConvert API?”

Leave a Comment