Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
function tagPicture($img, $className = 'picture'){
$arWebpFile = pathinfo($img);
$webpFile = $_SERVER['DOCUMENT_ROOT'] . "{$arWebpFile["dirname"]}/{$arWebpFile["filename"]}.webp";
if (!file_exists($webpFile))
exec("cwebp -q 85 {$_SERVER['DOCUMENT_ROOT']}{$img} -o {$webpFile}");
$webp = "{$arWebpFile["dirname"]}/{$arWebpFile["filename"]}.webp";
if ($arWebpFile['extension'] == 'png'){
$extension = 'png';
$extension = 'jpg';
$picture = "<picture>
<source srcset='{$webp}' type=\"image/webp\">
<source srcset=\"{$img}\" type=\"image/{$extension}\">
<img class=\"{$className}\" src=\"{$img}\">
return $picture;
Copy link

SeRGei93 commented Sep 9, 2019

yum install libwebp-tools

кормим функции png или jpg, получаем webp в теге picture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment