Skip to content

Commit

Permalink
* **Image Optimization** Support AVIF replacement (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
tynanbe authored Dec 9, 2024
1 parent 6a56982 commit f080b75
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cli/image.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function status()
}
}

foreach (array('reduced', 'reduced_webp') as $v) {
foreach (array('reduced', 'reduced_webp', 'reduced_avif') as $v) {
if (!empty($summary[$v])) {
$summary[$v] = Utility::real_size($summary[$v]);
}
Expand Down
1 change: 1 addition & 0 deletions src/htaccess.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ private function _browser_cache_rules($cfg)
'ExpiresByType image/png A' . $ttl,
'ExpiresByType image/gif A' . $ttl,
'ExpiresByType image/webp A' . $ttl,
'ExpiresByType image/avif A' . $ttl,
'',
'ExpiresByType video/ogg A' . $ttl,
'ExpiresByType audio/ogg A' . $ttl,
Expand Down
72 changes: 45 additions & 27 deletions src/media.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,37 +342,52 @@ public function media_row_con($post_id)
echo '</p>';

echo '<p>';
// WebP info
if ($size_meta && !empty($size_meta['webp_saved'])) {
$percent = ceil(($size_meta['webp_saved'] * 100) / $size_meta['webp_total']);
// WebP/AVIF info
if ($size_meta && !empty($size_meta[$this->_format . '_saved'])) {
$is_avif = 'avif' === $this->_format;
$size_meta_saved = $size_meta[$this->_format . '_saved'];
$size_meta_total = $size_meta[$this->_format . '_total'];

$link = Utility::build_url(Router::ACTION_IMG_OPTM, 'webp' . $post_id);
$percent = ceil(($size_meta_saved * 100) / $size_meta_total);

$link = Utility::build_url(Router::ACTION_IMG_OPTM, $this->_format . $post_id);
$desc = false;

$cls = '';

if ($this->info($short_path . '.webp', $post_id)) {
if ($this->info($short_path . '.' . $this->_format, $post_id)) {
$curr_status = __('(optm)', 'litespeed-cache');
$desc =
__('Currently using optimized version of WebP file.', 'litespeed-cache') .
'&#10;' .
__('Click to switch to original (unoptimized) version.', 'litespeed-cache');
} elseif ($this->info($short_path . '.optm.webp', $post_id)) {
$desc = $is_avif
? __('Currently using optimized version of AVIF file.', 'litespeed-cache')
: __('Currently using optimized version of WebP file.', 'litespeed-cache');
$desc .= '&#10;' . __('Click to switch to original (unoptimized) version.', 'litespeed-cache');
} elseif ($this->info($short_path . '.optm.' . $this->_format, $post_id)) {
$cls .= ' litespeed-warning';
$curr_status = __('(non-optm)', 'litespeed-cache');
$desc =
__('Currently using original (unoptimized) version of WebP file.', 'litespeed-cache') .
'&#10;' .
__('Click to switch to optimized version.', 'litespeed-cache');
$desc = $is_avif
? __('Currently using original (unoptimized) version of AVIF file.', 'litespeed-cache')
: __('Currently using original (unoptimized) version of WebP file.', 'litespeed-cache');
$desc .= '&#10;' . __('Click to switch to optimized version.', 'litespeed-cache');
}

echo GUI::pie_tiny(
$percent,
24,
sprintf(__('WebP file reduced by %1$s (%2$s)', 'litespeed-cache'), $percent . '%', Utility::real_size($size_meta['webp_saved'])),
sprintf(
$is_avif
? __('AVIF file reduced by %1$s (%2$s)', 'litespeed-cache')
: __('WebP file reduced by %1$s (%2$s)', 'litespeed-cache'),
$percent . '%',
Utility::real_size($size_meta_saved)
),
'left'
);
echo sprintf(__('WebP saved %s', 'litespeed-cache'), $percent . '%');
echo sprintf(
$is_avif
? __('AVIF saved %s', 'litespeed-cache')
: __('WebP saved %s', 'litespeed-cache'),
$percent . '%'
);

if ($desc) {
echo sprintf(
Expand All @@ -384,13 +399,16 @@ public function media_row_con($post_id)
);
} else {
echo sprintf(
' <span class="litespeed-desc" data-balloon-pos="left" data-balloon-break aria-label="%1$s">%2$s</span>',
__('Using optimized version of file. ', 'litespeed-cache') . '&#10;' . __('No backup of unoptimized WebP file exists.', 'litespeed-cache'),
' <span class="litespeed-desc" data-balloon-pos="left" data-balloon-break aria-label="%1$s&#10;%2$s">%3$s</span>',
__('Using optimized version of file. ', 'litespeed-cache'),
$is_avif
? __('No backup of unoptimized AVIF file exists.', 'litespeed-cache')
: __('No backup of unoptimized WebP file exists.', 'litespeed-cache'),
__('(optm)', 'litespeed-cache')
);
}
} else {
echo __('WebP', 'litespeed-cache') . '<span class="litespeed-left10">—</span>';
echo __('WebP/AVIF', 'litespeed-cache') . '<span class="litespeed-left10">—</span>';
}

echo '</p>';
Expand Down Expand Up @@ -956,7 +974,7 @@ private function _replace_buffer_img_webp($content)
*/
public function replace_background_webp($content)
{
Debug2::debug2('[Media] Start replacing bakcground WebP.');
Debug2::debug2('[Media] Start replacing background WebP/AVIF.');

// Handle Elementors data-settings json encode background-images
$content = $this->replace_urls_in_json($content);
Expand Down Expand Up @@ -1046,17 +1064,17 @@ public function replace_urls_in_json($content)
}

/**
* Replace internal image src to webp
* Replace internal image src to webp or avif
*
* @since 1.6.2
* @access public
*/
public function replace_webp($url)
{
Debug2::debug2('[Media] webp replacing: ' . substr($url, 0, 200));
Debug2::debug2('[Media] ' . $this->_format . ' replacing: ' . substr($url, 0, 200));

if (substr($url, -5) == '.webp') {
Debug2::debug2('[Media] already webp');
if (substr($url, -5) === '.' . $this->_format) {
Debug2::debug2('[Media] already ' . $this->_format);
return false;
}

Expand All @@ -1068,10 +1086,10 @@ public function replace_webp($url)
*/
if (apply_filters('litespeed_media_check_ori', Utility::is_internal_file($url), $url)) {
// check if has webp file
if (apply_filters('litespeed_media_check_webp', Utility::is_internal_file($url, 'webp'), $url)) {
$url .= '.webp';
if (apply_filters('litespeed_media_check_webp', Utility::is_internal_file($url, $this->_format), $url)) {
$url .= '.' . $this->_format;
} else {
Debug2::debug2('[Media] -no WebP file, bypassed');
Debug2::debug2('[Media] -no WebP or AVIF file, bypassed');
return false;
}
} else {
Expand Down
8 changes: 3 additions & 5 deletions src/placeholder.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,8 @@ private function _placeholder_realpath($src, $size)
{
// Use LQIP Cloud generator, each image placeholder will be separately stored

// Compatibility with WebP
if (substr($src, -5) === '.webp') {
$src = substr($src, 0, -5);
}
// Compatibility with WebP and AVIF
$src = Utility::drop_webp($src);

$filepath_prefix = $this->_build_filepath_prefix('lqip');

Expand Down Expand Up @@ -414,7 +412,7 @@ private function _generate_placeholder($raw_size_and_src, $from_cron = false)
$req_data = array(
'width' => $width,
'height' => $height,
'url' => substr($src, -5) === '.webp' ? substr($src, 0, -5) : $src,
'url' => Utility::drop_webp($src),
'quality' => $this->_conf_lqip_qual,
);

Expand Down
4 changes: 2 additions & 2 deletions src/utility.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,13 +421,13 @@ public static function basename($url)
}

/**
* Drop .webp if existed in filename
* Drop .webp and .avif if existed in filename
*
* @since 4.7
*/
public static function drop_webp($filename)
{
if (substr($filename, -5) === '.webp') {
if (in_array(substr($filename, -5), array('.webp', '.avif'))) {
$filename = substr($filename, 0, -5);
}

Expand Down

0 comments on commit f080b75

Please sign in to comment.