PHP code example of gtarr / webp-parse-and-convert

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

    

gtarr / webp-parse-and-convert example snippets


var_dump(function_exists('imagewebp')); // bool(true) - можно

$content = '<html>...<img src="">...</html>'; // HTML страницы
$rootDir = $_SERVER['DOCUMENT_ROOT'];         // корень сайта


$rootDir = $_SERVER['DOCUMENT_ROOT'];

$options = [
   "formats" => ['jpg', 'jpeg', 'png'],
   "patterns" => [
      [
         'pattern' => '/<img[^>]+src=("[^"]*")[^>]*>/i',
         'exclude' => ['"', './']
      ],
      [
         'pattern' => '/background-image:.+url\(([^"]+)\)/i',
         'exclude' => ["'", "./"]
      ],
   ],
   "devices" => ['iphone', 'ipod', 'ipad', 'macintosh', 'mac os', 'Edge', 'MSIE', 'Trident'],
   "converterOptions" => [],
   "debug" => false,
   "useApi" => false,
   "api" => []
];

$converter = new WebPParseAndConvert($content, $rootDir, $options); 

$options = array(
    'useApi' => true,
    'api' => array(
       'key' => 'some API key',
       'url' => 'http://example.com/wpc.php'
    )
);