Skip to content

Commit

Permalink
perf: cache WebP conversion (#1925)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny authored Mar 14, 2024
1 parent d6b03ea commit d4d8755
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,12 +508,19 @@ public function webp(?int $quality = null): self
return $assetWebp; // returns the asset with the new extension ('webp') only: CDN do the rest of the job
}

$img = ImageManager::make($assetWebp['content']);
$assetWebp['content'] = (string) $img->encode($format, $quality);
$img->destroy();
$assetWebp['path'] = preg_replace('/\.' . $this->data['ext'] . '$/m', ".$format", $this->data['path']);
$assetWebp['subtype'] = "image/$format";
$assetWebp['size'] = \strlen($assetWebp['content']);
$cache = new Cache($this->builder, (string) $this->builder->getConfig()->get('cache.assets.dir'));
$cacheKey = $cache->createKeyFromAsset($assetWebp, ["q$quality"]);
if (!$cache->has($cacheKey)) {
$img = ImageManager::make($assetWebp['content']);
$assetWebp['content'] = (string) $img->encode($format, $quality);
$img->destroy();
$assetWebp['path'] = preg_replace('/\.' . $this->data['ext'] . '$/m', ".$format", $this->data['path']);
$assetWebp['subtype'] = "image/$format";
$assetWebp['size'] = \strlen($assetWebp['content']);

$cache->set($cacheKey, $assetWebp->data);
}
$assetWebp->data = $cache->get($cacheKey);

return $assetWebp;
}
Expand Down

0 comments on commit d4d8755

Please sign in to comment.