bnomei/kirby3-cloudconvert

插件,用于使用 cloudconvert 将任何内容转换为任何内容。

1.1.10 2022-12-05 12:44 UTC

This package is auto-updated.

Last update: 2024-09-20 00:19:49 UTC


README

Release Downloads Build Status Coverage Status Maintainability Twitter

插件,使用 cloudconvert 将任何内容转换为任何内容。

商业用途


支持开源!

此插件是免费的,但如果您在商业项目中使用它,请考虑赞助我或捐款。
如果我的工作帮助您赚了钱,对我来说似乎公平,我也可以得到一点回报,对吧?

善良。分享一点。谢谢。

- Bruno
 

安装

  • master.zip 解压为 site/plugins/kirby3-cloudconvert 目录,或者
  • 使用 git submodule add https://github.com/bnomei/kirby3-cloudconvert.git site/plugins/kirby3-cloudconvert
  • 使用 composer require bnomei/kirby3-cloudconvert

如何同步地按需转换文件?

示例 1:docx 转换为 pdf

if($fileWord = $page->file('test.docx')) {
    $filePDF = $fileWord->cloudconvert(
        [
            'inputformat' => 'docx',
            'outputformat' => 'pdf',
        ], 
        str_replace('.docx', '.pdf', $fileWord->root()),
        false // wait for conversion to be done
    );
    echo $fileWord->url().PHP_EOL;
    if($filePDF) {
        echo $filePDF->url();
    }
}

钩子:如何在上传/替换时转换文件(默认为异步)?

示例 2:gif 转换为 webm 和 mp4

在 Kirbys 配置文件中添加以下内容...然后使用面板上传 gif 到图像/文件部分。

function customConvertHook($file) {
    if($file->extension() == 'gif') {

        $file->cloudconvert(
            [
                'inputformat' => 'gif',
                'outputformat' => 'webm',
                'save' => true, // keep file at cloud to avoid another download from cloudconvert-server
            ]
        );

        $file->cloudconvert(
            [
                'inputformat' => 'gif',
                'outputformat' => 'mp4',
            ]
        );
    }
}

return [
    // ... other config settings
    'hooks' => [
        'file.create:after' => function($file) {
            customConvertHook($file);
        },
        'file.replace:after' => function($newFile, $oldFile) {
            customConvertHook($newFile);
        },
    ]
];

其他用例

示例 3:将 ai 转换为 svg 并优化

此示例展示了如何使用此插件与我的 thumb imageoptim 插件 一起使用。

$fileSvg = $file->cloudconvert(
    [
        'inputformat' => 'ai',
        'outputformat' => 'svg',
    ], 
    null, // auto-rename with extension
    false // on-demand aka synchonous aka wait
);
if($fileSvg) {
    // NOTE: resize() is called to trigger stuff like optimziers (see thumb-imageoptim plugin)
    $fileSvgOptimized = $fileSvg->resize();
    echo svg($fileSvgOptimized->root()); // use kirbys svg helper to inline the svg
}

示例 4:如何为 srcsets 转换图像文件? jpg 转换为 webp。

此示例展示了如何使用此插件与我的 srcset 插件 一起使用。

配置文件

return [
    // ... other settings
    'bnomei.srcset.types' => ['jpg', 'webp'],
    'bnomei.srcset.resize' => function (\Kirby\Cms\File $file, int $width, string $type) {
        // NOTE: resize() is called to trigger stuff like optimziers (see thumb-imageoptim plugin)

        // use jpg to create webp
        if($file->extension() == 'jpg' && $type == 'webp') {
            $fileWebp = $file->cloudconvert(
                [
                    'inputformat' => 'jpg',
                    'outputformat' => 'webp',
                ], 
                null, // auto-rename with extension
                false // on-demand aka synchonous aka wait
            );
            if($fileWebp) {
                return $fileWebp->resize($width);
            }
        }
        // otherwise default to returning image
        return $file->resize($width);
    }
];

全局 cloudconvert 辅助函数

此插件提供了一个辅助函数来调用 cloudconvert api。

$obj = cloudconvert($options); // will change extension but keep path
$obj = cloudconvert($options, $outputPath); // provide different path
$obj = cloudconvert($options, $outputPath, $async); // a/sync

返回值

  • 如果 $options['file'] 也是 Kirby\Cms\File$async == false,则返回 Kirby\Cms\File
  • 否则返回 CloudConvert\Process 实例
  • 或错误时返回 null

性能

TD;DR 调用 File-Method 有最佳性能,因为它仅在文件已修改或为新文件时才进行转换。

文件对象

建议使用 Kirby\Cms\File 对象作为 $options['file']。在这种情况下,修改时间戳将与缓存的值进行比较,并且仅在文件已修改或输出不存在时触发转换。这是此插件提供的 FileMethod 的默认行为。

危险:目前还没有检查文件是否正在由 cloudconvert 处理。这可能在以后得到改进。

路径

当使用路径时,只有在输出不存在时才会创建文件。您需要自己进行修改检查和删除旧文件,然后再开始转换。

设置

提示:如果您使用的是 dotenv 插件,则也可以设置回调 'bnomei.cloudconvert.apikey' => function() { return env('CLOUDCONVERT_APIKEY'); },

提示:考虑设置 预设 来管理您的设置,而不是在 Kirby 配置文件中。

免责声明

本插件提供“现状”且没有任何保证。请自行承担使用风险,并在将其用于生产环境之前自行测试。如果您发现任何问题,请创建新的问题

许可证

MIT

不建议在推广种族主义、性别歧视、恐同、动物虐待、暴力或其他任何形式仇恨言论的项目中使用此插件。