PHP - How to Convert File(image, audio, video) Extension using CloudConvert API?

By Hardik Savani May 14, 2024 Category : PHP Bootstrap

We may sometimes require to convert file extension like if you have video 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, Same as for audio mp3, then all the thing you can do using CloudConvert API.

CloudConvert API is very popular website for convert file extension. They provide us api to convert file extension like image into pdf. So it will be pretty easy. 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 indexPro.php file

4. Create uploads folder

So let's follow bellow step:

Step 1: Download Autoload File

In first step we have to download autoload file 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

Ok in the last step, we have to make index.php file on root directory. So let's create index.php file and put bellow code on that file:

index.php

<!DOCTYPE html>

<html>

<head>

<title>PHP - Cloudconvert API Example</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="indexPro.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 indexPro.php File

Ok in the last step, we have to make indexPro.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:

cloudconvert.com

After getting api key you have to just put on Api Class constructor.

So let's create indexPro.php file and put bellow code on that file:

indexPro.php

<?php


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


use \CloudConvert\Api;


$api = new Api("put api key here");


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

try {


$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

$uploadFilePath = 'uploads/test.'.$ext;

move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);


$api->convert([

'inputformat' => $ext,

'outputformat' => 'pdf',

'input' => 'upload',

'file' => fopen($uploadFilePath, 'r'),

])

->wait()

->download('uploads/output.pdf');


} catch (Exception $e) {

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

}


print_r("File change format successfully.");

exit;

}else{

print_r("Pls Select image");

exit;

}


?>

Step 4: Create uploads folder

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

So let's create new folder uploads with proper permission.

Ok, now we are ready to run this example, so just run bellow command on root folder for run your project.

php -S localhost:8000

Now you can open bellow url on your browser:

http://localhost:8000

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

I hope it can help you...

Shares