Skip to content

Instantly share code, notes, and snippets.

@TrywaR
Last active July 21, 2020 15:25
Show Gist options
  • Save TrywaR/87b23c79eda267cb1a9e0315655cc485 to your computer and use it in GitHub Desktop.
Save TrywaR/87b23c79eda267cb1a9e0315655cc485 to your computer and use it in GitHub Desktop.
converter to webp
<?
// Подключение webp-convert
require_once( $modx->config['base_path'].'lib/webp-convert/webp-convert.inc' );
use WebPConvert\WebPConvert;
// Параметры
$sWebpQualityWebp = 100; # Степерь сжатия webp
$sImgSrc = ''; # Путь до картинки
if (file_exists($sImgSrc)) { # Если файл есть
if (pathinfo($sImgSrcFull, PATHINFO_EXTENSION) != 'webp'){ # И он ещё не в webp
// Ищем есть ли у него копия в webp
// Получаем формат
$arrImgSrc = explode('/',$sImgSrc);
$arrImgFormat = explode('.',$arrImgSrc[end($arrImgSrc)]);
$sImgFormat = end($arrImgFormat);
$sImgSrcWebp = str_replace('.'.$sImgFormat, '.webp', $sImgFormat);
if ( ! file_exists($sImgSrcWebp) ) { # Если изображения в Webp нет, создаём
if (WebPConvert::convert($sImgSrc, $sImgSrcWebp, ['quality' => $sWebpQualityWebp])){
return $sImgSrcWebp;
}
else {
$modx->log(modX::LOG_LEVEL_ERROR, 'Не удалось создать webp :( ' . $sImgSrcWebp);
}
}
}
return $sImgSrc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment