markocupic/cloudconvert-bundle

This Bundle for the Contao CMS provides a OOP PHP wrapper for CloudConvert's API. Convert files from one filetype into an other with ease.

Installs: 2 356

Dependents: 3

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

Type:contao-bundle

2.5.5 2024-02-11 16:37 UTC

README

logo.png?raw=true

Cloudconvert Bundle

This simple bundle for the Contao CMS provides an OOP PHP wrapper for converting files from one format into another using the Cloudconvert API.

logo_cloudconvert.png

Almost everything is possible:

  • docx -> pdf
  • jpeg -> png
  • wav -> mp3
  • csv -> xlsx
  • etc. For a full list of formats visit Cloudconvert.

Free plan (25 credits per day)

Get your free API key for using the Cloudconvert API: Free Plan Cloudconvert

Installation & configuration

Install the extension via the Contao Manager or call composer require markocupic/cloudconvert-bundle in your command line.

In your config/config.yaml you now have to set the api key.

# config/config.yaml
markocupic_cloudconvert:
  api_key: '****' # is mandatory
  sandbox_api_key: '****' # is optional
  backend_alert_credit_limit: 100 # is optional /default to: 200 /set to 0 to disable the system alert
  credit_expiration_notification: # is optional
      enabled: true # is optional
      limit: 150 # is optional
      email: ['foo@bar.ch', 'bar@foo.ch'] # is optional

To complete the installation please run composer install in your command line.

Usage

<?php

declare(strict_types=1);

namespace App\Controller;

use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\System;
use Markocupic\CloudconvertBundle\Conversion\ConvertFile;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/cloudconvert_demo', name: CloudconvertDemoController::class, defaults: ['_scope' => 'frontend'])]
class CloudconvertDemoController extends AbstractController
{

    public function __construct(
        private readonly ContaoFramework $framework,
        private readonly ConvertFile $convertFile,
    ){}

    /**
     * @throws \Exception
     */
    public function __invoke(): BinaryFileResponse
    {

        $this->framework->initialize();
        $projectDir = System::getContainer()->getParameter('kernel.project_dir');

        $sourcePath = $projectDir.'/files/mswordfile.docx';

        // Basic example (minimal configuration):
        // Convert from docx to pdf
        $objSplFile = $this->convertFile
            ->file($sourcePath)
            // Save converted file in the same
            // directory as the source file.
            ->convertTo('pdf')
        ;

        $sourcePath = $projectDir.'/files/samplesound.wav';

        // Convert from wav to mp3
        $objSplFile = $this->convertFile
            ->reset()
            ->file($sourcePath)
            ->convertTo('mp3')
        ;

        $sourcePath = $projectDir.'/files/image.jpg';

        // A slightly more sophisticated example:
        $objSplFile = $this->convertFile
            ->reset()
            ->file($sourcePath)
            // Sandbox API key has to be set in config/config.yaml
            ->sandbox(true)
            ->uncached(false) // Enable cache
            ->setCacheHashCode('566TZZUUTTAGHJKUZT') // use the hash of your file to get the file from the cache directory
            // For a full list of possible options
            // please visit https://cloudconvert.com/api/v2/convert#convert-tasks
            ->setOption('width', 1200)
            ->setOption('quality', 90)
            // Convert docx file into the png format and
            // save file in a different directory.
            // For a full list of supported formats
            // please visit https://cloudconvert.com/api/v2/convert#convert-formats
            ->convertTo('png', 'files/images/my_new_image.png')
        ;

        // Send the converted file to the browser
        return $this->file($objSplFile->getRealPath());
    }
}