PHP code example of bnomei / kirby3-cloudconvert

1. Go to this page and download the library: Download bnomei/kirby3-cloudconvert library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bnomei / kirby3-cloudconvert example snippets


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();
    }
}

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);
        },
    ]
];

$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
}

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);
    }
];

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