wintercms / wn-tinypng-plugin

Winter CMS plugin to automatically optimize images with TinyPNG
MIT License
9 stars 3 forks source link

Add support for WebP #2

Open AIC-BV opened 1 year ago

AIC-BV commented 1 year ago

Would be nice if the plugin was extended to support WebP which appears to be a new feature of TinyPNG. This allows WinterCMS developers to be up to date with todays technology! 😄
image Ideally it would serve automatically .webp if supported, if not a fallback to .png/.jpg (only for Safari? It seems supported since end of 2022 so it still needs the fallback for users that didn't update their OS).

Images would be compressed twice (once like it already is, and once as WebP) doubling the compressions and the price, so it should be optional with a toggle switch in the settings of the plugin

LukeTowers commented 1 year ago

Feel free to submit a PR 😉

LukeTowers commented 1 year ago

@bennothommo can you copy the tags over to this repo?

bennothommo commented 1 year ago

@LukeTowers done :)

LukeTowers commented 1 year ago

Thanks @bennothommo :)

AIC-BV commented 1 year ago

I would say converting is as easy as

"tinify/tinify": "^1.6.1"
// DEFAULT
$source = Source::fromFile($tempPath);
$source->toFile($tempPath);

// NEW
$sourceWebp = Source::fromFile($tempPath);
$sourceWebp->convert(["type" => "image/webp"]);
$extension = $converted->result()->extension();
$pathWithoutExtension = substr($tempPath, 0, strrpos($tempPath, "."));
$sourceWebp->toFile($pathWithoutExtension . "." . $extension);

var_dump($extension);
var_dump($pathWithoutExtension);
var_dump($sourceWebp);

But it isn't doing anything https://tinypng.com/developers/reference#converting-images