jorisvanw/cloudconvert-laravel

CloudConvert API 的 Laravel 封装

v2.1.7 2016-01-11 14:54 UTC

README

CloudConvert API 的 Laravel 封装。更多详情请访问 https://cloudconvert.org

Build Status

安装

通过 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

Laravel 4 需要稍微不同的语法来发布配置,使用 php artisan config:publish robbiep/cloudconvert-laravel

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

您可以通过在 https://cloudconvert.org 注册来获取免费的 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');

网站截图

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/packages/robbiep/cloudconvert-laravel/config.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: send_conversion.php
CloudConvert::file('/a/path/to/file.mov')
            ->callback('http://myserver.com/save_file.php')
            ->convert('mp4');
            

# Script save_file.php
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');

待办事项

  • 发布
  • 编写更多测试
  • 启用多个文件的合并
  • 启用单个过程中的多个转换
  • 重构命令

贡献

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

鸣谢

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

资源