PHP code example of rosell-dk / webp-convert-and-serve

1. Go to this page and download the library: Download rosell-dk/webp-convert-and-serve 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/ */

    

rosell-dk / webp-convert-and-serve example snippets


try {
    $success = WebPConvert::convert($source, $destination, $options);
    if ($success) {
        header('Content-type: image/webp');
        readfile($destination);        
    }
}



use WebPConvertAndServe\WebPConvertAndServe;

$source = __DIR__ . '/logo.jpg';
$destination = $source . '.webp';
$options = [
    'fail' => 'original',
    'critical-fail' => '404',

    // You can specify any WebPConvert option here - such as defining a converters array, which
    // is needed, if you need to use a cloud converter
    'converters' => [
        [
            'converter' => 'ewww',
            'options' => [
                'key' => 'blah',
            ],
        ],
        'cwebp',
        'gd'
    ];

$status = WebPConvertAndServe::convertAndServe($source, $destination, $options);