lingwave/cloudconvert-laravel

CloudConvert API的Laravel包装器

5.0.2 2021-07-22 18:12 UTC

README

A Laravel wrapper for the CloudConvert API. See https://cloudconvert.com for more details.

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

安装

通过Composer安装此包。

将以下内容添加到您的composer.json依赖项中

使用Laravel 8.0+

"require": {
   "lingwave/cloudconvert-laravel": "5.*"
}

使用Laravel 7.0+

"require": {
   "lingwave/cloudconvert-laravel": "4.*"
}

对于Laravel的旧版本,请参阅https://github.com/robbiepaul/cloudconvert-laravel

运行composer install下载所需的文件。

接下来,您需要将服务提供者添加到config/app.php

'providers' => array(
    ...
    Lingwave\CloudConvertLaravel\CloudConvertLaravelServiceProvider::class
)

再走一步。

您需要发布配置php artisan vendor:publish

只需在config/cloudconvert.php中输入您的API密钥即可。

您可以通过在https://cloudconvert.com注册来获取免费的API密钥。

现在您可以在应用程序中使用CloudConvert了!

用法

使用CloudConvert有多种方式。以下我将介绍其中几种,对于所有转换选项,我建议您查看API文档。

文件转换

# Convert the file to /a/path/to/file.mp4

CloudConvert::file('/a/path/to/file.mov')->to('mp4');
# Convert the file and save it in a different location /a/new/path/to/new.mp4

CloudConvert::file('/a/path/to/biggles.webm')->to('/a/new/path/to/new.mp4');
# It also works with Laravel's file upload

if (Input::hasFile('photo'))
{
    CloudConvert::file( Input::file('photo') )->to('/a/local/path/profile_image.jpg');
}
# Convert the image to kitty.jpg with quality of 70%

CloudConvert::file('kitty.png')->quality(70)->to('jpg');
# Convert a PowerPoint presentation to a set of images, let's say you only want slides 2 to 4
# This will save presentation-2.jpg, presentation-3.jpg and presentation-4.jpg

CloudConvert::file('presentation.ppt')->pageRange(2, 4)->to('jpg');

动态文件转换

# Dynamic PDF creation using DOCX/PPTX templates
# See this blog post for more details: https://cloudconvert.com/blog/dynamic-pdf-creation-using-docx-templates/

$variables = ['name' => 'John Doe', 'address' => 'Wall Street'];
CloudConvert::file('invoice_template.docx')->templating($variables)->to('invoice.pdf');

转换选项

还有很多其他转换选项。我已将如上所示的快捷方式用于最常见的情况。但是,您可以使用withOptions方法传递任何您喜欢的选项,例如

# Convert the meow.wav to meow.mp3 with a frequecy of 44100 Hz and normalize the audio to +20dB

CloudConvert::file('meow.wav')->withOptions([
    'audio_frequency' => '44100', 
    'audio_normalize' => '+20dB'
])->to('mp3');


# Convert the fido_falls_over.mp4 to fido.gif but you only want 10 seconds of it, starting at 1:02

CloudConvert::file('fido_falls_over.mp4')->withOptions([
    'trim_from' => '62', 
    'trim_to' => '72'
])->to('fido.gif');

# Or the same with using the shortcuts:
CloudConvert::file('fido_falls_over.mp4')->trimFrom(62)->trimTo(72)->to('fido.gif');

链式多个转换

您也可以在一个过程中链式多个转换,如下所示

# Convert a TrueType font in to all the fonts you need for a cross browser web font pack

CloudConvert::file('claw.ttf')->to('eot', true)->to('otf', true)->to('woff', true)->to('svg');

# Or the same thing with an array
CloudConvert::file('claw.ttf')->to(['eot', 'otf', 'woff', 'svg']);

远程文件

它还可以与转换远程文件一起使用(只需确保您提供要保存的路径即可)

# Convert Google's SVG logo hosted on Wikipedia to a png on your server

CloudConvert::file('http://upload.wikimedia.org/wikipedia/commons/a/aa/Logo_Google_2013_Official.svg')
            ->to('images/google.png');

合并PDF文件

目前,合并仅适用于远程托管文件,但将来它将支持上传文件和存储中的文件

# Merge the PDFs in the array in to a single PDF

CloudConvert::merge([
             'https://cloudconvert.com/assets/d04a9878/testfiles/pdfexample1.pdf',                          
             'https://cloudconvert.com/assets/d04a9878/testfiles/pdfexample2.pdf'
            ])
            ->to('merged.pdf');

网站截图

CloudConvert还将对网站进行截图,并将其转换为图像或PDF

# Take a screenshot with the default options: 1024px with with full height of webpage

CloudConvert::website('www.nyan.cat')->to('screenshots/nyan.jpg');
# You can also specify the width and the height as converter options

CloudConvert::website('www.nyan.cat')
            ->withOptions([
                 'screen_width' => 1024,
                 'screen_height' => 700
            ])->to('screenshots/nyan.png');

转换为和从外部存储选项

目前CloudConvert允许您使用FTPAmazon S3作为存储选项。然而,看起来将来他们将在API中添加Google DriveDropbox

**请注意:** 使用这些存储选项,您需要在config/cloudconvert.php中提供配置。

# Lets say you have a PDF and you want to convert it to an ePub file and 
# store it on your Amazon S3 bucket (defined in your config). It's this simple:

CloudConvert::file('/a/local/path/garfield.pdf')->to(CloudConvert::S3('Garfield_converted.epub'));
# You can also override the default options by providing them as an array as the second argument

CloudConvert::file('/a/local/path/garfield.pdf')
            ->to(CloudConvert::S3('Garfield_converted.epub', [
                'bucket'  => 'a-different-bucket',
                'acl'     => 'public-read',
                'region'  => 'us-east-1'
            ]));
# Now you want to convert the file on your S3 to a txt file and store it on a server via FTP

CloudConvert::file(CloudConvert::S3('Garfield_converted.epub'))
            ->to(CloudConvert::FTP('path/to/garfield.txt'));

就是这样简单。存储选项CloudConvert::S3($path)CloudConvert::FTP($path)可以用于输入文件和输出文件。

使用回调URL进行非阻塞转换

当转换可能需要很长时间时,您可以使用

# Script: sendConversion
CloudConvert::file('/a/path/to/file.mov')
            ->callback('http://myserver.com/save_file.php')
            ->convert('mp4');
            

# Script: saveFile
CloudConvert::useProcess($_REQUEST['url'])
            ->save('/path/converted.mp4');

使用队列进行非阻塞转换

要使用队列,您需要在config/queue.php中设置beanstalk或iron

# The queue will check every second if the conversion has finished. 
# It times out after 120 seconds (configurable).

CloudConvert::file('/a/path/to/file.mov')->queue('to', '/a/path/to/file.mp4')

转换类型

您可以使用conversionTypes()方法查看转换类型。它始终返回Illuminate\Support\Collection

# To get all possible types

$types = CloudConvert::conversionTypes();
# To get all possible types in a specific group

$types = CloudConvert::conversionTypes('video');
# To get all possible output formats if you know the input format

$types = CloudConvert::input('pdf')->conversionTypes();
# Same if you know the output format and want to see what can be inputted

$types = CloudConvert::output('jpg')->conversionTypes();

进程状态

您可能想列出所有进程,正在运行的、完成的和失败的。它始终返回一个Illuminate\Support\Collection

# To get all possible types
$processes = CloudConvert::processes();

# To delete a process by ID
CloudConvert::deleteProcess($process_id);

Artisan命令

如果您想从控制台快速进行转换或调用API,可以使用以下命令

转换文件

# Options: --opions, --background, --storage, --path
php artisan cloudconvert:convert video.mov mp4
php artisan cloudconvert:convert /path/to/video.mov converted.mp4 --storage='s3'

网站截图

# Options: --opions, --storage, --path
php artisan cloudconvert:website www.laravel.com jpg

进程列表

# Options: --delete (used with process_id)
# Argument: process_id (optional) - will show the status of that process
php artisan cloudconvert:processes <process_id>

转换类型

# Options: --input, --output 
# (both optional - however if you included both you will see all the 
# possible converter options for those types, not just the default ones)
php artisan cloudconvert:types
php artisan cloudconvert:types --input='nice.pdf'
php artisan cloudconvert:types --input='doc' --output='jpg'

在不使用Laravel的情况下使用此包

您仍然需要使用composer。输入 composer require robbiep/cloudconvert-laravel 命令以下载文件,然后您可以使用该软件包,如下所示

require_once('vendor/autoload.php');

$cloudConvert = new RobbieP\CloudConvertLaravel\CloudConvert(['api_key' => 'API_KEY_HERE']);

$cloudConvert->file('randomuser.jpg')->to('png');

待办事项

  • 发布
  • 编写更多测试
  • 启用多个文件合并
  • 启用单个进程中的多个转换
  • 重构命令
  • 增加了对 Guzzle ~6.0 的支持
  • README文件变得很长,转换为维基百科

贡献

  1. 进行Fork操作
  2. 创建您的功能分支: git checkout -b my-new-feature
  3. 提交您的更改: git commit -am '添加一些功能'
  4. 推送到分支: git push origin my-new-feature
  5. 提交拉取请求

致谢

感谢Lunaweb Ltd. 提供他们的API。去看看这里

资源