markocupic / cloudconvert-bundle
此Bundle为Contao CMS提供了CloudConvert API的OOP PHP封装。轻松将文件从一种格式转换为另一种格式。
2.5.5
2024-02-11 16:37 UTC
Requires
- php: ^8.1
- cloudconvert/cloudconvert-php: ^3.2
- contao/core-bundle: ^4.13 || ^5.0
Requires (Dev)
README
Cloudconvert Bundle
这个简单的Bundle为Contao CMS提供了一个OOP PHP封装,用于使用Cloudconvert API将文件从一种格式转换为另一种格式。
几乎一切皆可
- docx -> pdf
- jpeg -> png
- wav -> mp3
- csv -> xlsx
- 等等。要查看完整格式列表,请访问Cloudconvert。
免费计划(每天25个积分)
获取使用Cloudconvert API的免费API密钥: Free Plan Cloudconvert
安装和配置
通过Contao Manager安装扩展或在你的命令行中调用composer require markocupic/cloudconvert-bundle
。
在你的config/config.yaml
中,现在你需要设置API密钥。
# 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
为了完成安装,请在你的命令行中运行composer install
。
使用方法
<?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()); } }