robbiep/cloudconvert-laravel

此包已弃用且不再维护。作者建议使用cloudconvert/cloudconvert-laravel包。

CloudConvert API的Laravel封装

v3.0 2020-01-16 20:51 UTC

README

CloudConvert API的Laravel封装。有关详细信息,请访问https://cloudconvert.com

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

安装

通过Composer安装此包。

将此添加到您的composer.json依赖项中

使用Laravel 5.0+

"require": {
   "robbiep/cloudconvert-laravel": "2.*"
}

使用Laravel ~4.2

"require": {
   "robbiep/cloudconvert-laravel": "1.*@dev"
}

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

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

'providers' => array(
    ...
    RobbieP\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 文件变得越来越长,转换为 wiki

贡献

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

致谢

感谢 Lunaweb Ltd. 提供的 API。去看看

资源