From 883c99df9c4d053fbb381fcc339bd7f6b4788e39 Mon Sep 17 00:00:00 2001 From: Michael van Laar Date: Fri, 19 May 2023 19:52:17 +0200 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=90=9B=20fix(index.php):=20add=20chec?= =?UTF-8?q?k=20for=20svg=20files=20to=20avoid=20generating=20webp=20files?= =?UTF-8?q?=20for=20them=20The=20plugin=20was=20generating=20webp=20files?= =?UTF-8?q?=20for=20all=20file=20types,=20including=20svg=20files.=20This?= =?UTF-8?q?=20commit=20adds=20a=20check=20to=20avoid=20generating=20webp?= =?UTF-8?q?=20files=20for=20svg=20files.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index 921206e..9216008 100644 --- a/index.php +++ b/index.php @@ -6,12 +6,12 @@ Kirby::plugin('felixhaeberle/kirby3-webp', [ 'hooks' => [ 'file.create:after' => function ($file) { - if ($this->option('kirby3-webp', false)) { + if ($this->option('kirby3-webp', false) && $file->extension() !== 'svg') { (new WebP\Convert)->generateWebP($file); } }, 'file.replace:after' => function ($newFile, $oldFile) { - if ($this->option('kirby3-webp', false)) { + if ($this->option('kirby3-webp', false) && $newFile->extension() !== 'svg') { (new WebP\Convert)->generateWebP($newFile); } }, From db7d499b867405ce69993dc6b61430070ad97f5c Mon Sep 17 00:00:00 2001 From: Michael van Laar Date: Fri, 19 May 2023 22:28:48 +0200 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=90=9B=20fix(index.php):=20remove=20w?= =?UTF-8?q?ebp=20files=20and=20txt=20files=20when=20original=20file=20is?= =?UTF-8?q?=20deleted=20This=20commit=20adds=20a=20new=20hook=20to=20the?= =?UTF-8?q?=20Kirby=20plugin=20that=20removes=20the=20corresponding=20webp?= =?UTF-8?q?=20and=20txt=20files=20when=20the=20original=20file=20is=20dele?= =?UTF-8?q?ted.=20This=20ensures=20that=20the=20webp=20files=20are=20not?= =?UTF-8?q?=20left=20behind=20when=20the=20original=20file=20is=20deleted.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/index.php b/index.php index 9216008..d1c1c56 100644 --- a/index.php +++ b/index.php @@ -15,5 +15,15 @@ (new WebP\Convert)->generateWebP($newFile); } }, + 'file.delete:after' => function ($file) { + $webpFile = dirname($file->root()) . '/' . $file->name() . '.webp'; + $webpTxtFile = dirname($file->root()) . '/' . $file->name() . '.webp.txt'; + if (F::exists($webpFile)) { + F::remove($webpFile); + } + if (F::exists($webpTxtFile)) { + F::remove($webpTxtFile); + } + }, ], ]); From c4c36ea30259986a3c9ab6f012bf6cb77aa9553f Mon Sep 17 00:00:00 2001 From: Michael van Laar Date: Fri, 19 May 2023 22:29:33 +0200 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=94=A8=20refactor(index.php):=20extra?= =?UTF-8?q?ct=20functions=20to=20improve=20code=20readability=20and=20main?= =?UTF-8?q?tainability=20The=20code=20has=20been=20refactored=20to=20extra?= =?UTF-8?q?ct=20the=20functionality=20of=20generating=20and=20deleting=20w?= =?UTF-8?q?ebp=20files=20into=20separate=20functions.=20This=20improves=20?= =?UTF-8?q?the=20readability=20and=20maintainability=20of=20the=20code=20b?= =?UTF-8?q?y=20making=20it=20easier=20to=20understand=20and=20modify.=20Th?= =?UTF-8?q?e=20extracted=20functions=20are=20`shouldGenerateWebP`,=20`gene?= =?UTF-8?q?rateWebP`,=20`deleteWebPFiles`,=20and=20`deleteIfExist`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.php | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/index.php b/index.php index d1c1c56..14c42cd 100644 --- a/index.php +++ b/index.php @@ -3,27 +3,41 @@ @include_once __DIR__ . '/vendor/autoload.php'; @include_once __DIR__ . '/src/webp.php'; +function shouldGenerateWebP($file) { + return $file->kirby()->option('kirby3-webp', false) && $file->extension() !== 'svg'; +} + +function generateWebP($file) { + (new WebP\Convert)->generateWebP($file); +} + +function deleteWebPFiles($file) { + $webpFile = dirname($file->root()) . '/' . $file->name() . '.webp'; + $webpTxtFile = dirname($file->root()) . '/' . $file->name() . '.webp.txt'; + deleteIfExist($webpFile); + deleteIfExist($webpTxtFile); +} + +function deleteIfExist($file) { + if (F::exists($file)) { + F::remove($file); + } +} + Kirby::plugin('felixhaeberle/kirby3-webp', [ 'hooks' => [ 'file.create:after' => function ($file) { - if ($this->option('kirby3-webp', false) && $file->extension() !== 'svg') { - (new WebP\Convert)->generateWebP($file); + if (shouldGenerateWebP($file)) { + generateWebP($file); } }, 'file.replace:after' => function ($newFile, $oldFile) { - if ($this->option('kirby3-webp', false) && $newFile->extension() !== 'svg') { - (new WebP\Convert)->generateWebP($newFile); + if (shouldGenerateWebP($newFile)) { + generateWebP($newFile); } }, 'file.delete:after' => function ($file) { - $webpFile = dirname($file->root()) . '/' . $file->name() . '.webp'; - $webpTxtFile = dirname($file->root()) . '/' . $file->name() . '.webp.txt'; - if (F::exists($webpFile)) { - F::remove($webpFile); - } - if (F::exists($webpTxtFile)) { - F::remove($webpTxtFile); - } + deleteWebPFiles($file); }, ], ]); From b7e1bdc27583697eb8162759c2f34f564c1c39bf Mon Sep 17 00:00:00 2001 From: Michael van Laar Date: Sat, 20 May 2023 13:57:14 +0200 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=90=9B=20fix(Convert.php):=20change?= =?UTF-8?q?=20file=20type=20check=20to=20extension=20check=20to=20include?= =?UTF-8?q?=20all=20image=20types=20=E2=9C=A8=20feat(index.php):=20remove?= =?UTF-8?q?=20file=20extension=20check=20in=20shouldGenerateWebP=20functio?= =?UTF-8?q?n=20The=20file=20type=20check=20in=20Convert.php=20was=20change?= =?UTF-8?q?d=20to=20an=20extension=20check=20to=20include=20all=20image=20?= =?UTF-8?q?types.=20This=20improves=20the=20functionality=20of=20the=20cla?= =?UTF-8?q?ss=20as=20it=20can=20now=20process=20all=20image=20types.=20The?= =?UTF-8?q?=20file=20extension=20check=20in=20shouldGenerateWebP=20functio?= =?UTF-8?q?n=20in=20index.php=20was=20removed=20as=20it=20is=20no=20longer?= =?UTF-8?q?=20necessary=20with=20the=20changes=20made=20in=20Convert.php.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.php | 2 +- lib/Convert.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index 14c42cd..52452ae 100644 --- a/index.php +++ b/index.php @@ -4,7 +4,7 @@ @include_once __DIR__ . '/src/webp.php'; function shouldGenerateWebP($file) { - return $file->kirby()->option('kirby3-webp', false) && $file->extension() !== 'svg'; + return $file->kirby()->option('kirby3-webp', false); } function generateWebP($file) { diff --git a/lib/Convert.php b/lib/Convert.php index 342fe0e..873c535 100644 --- a/lib/Convert.php +++ b/lib/Convert.php @@ -29,7 +29,7 @@ public function generateWebP($file) { try { // Checking file type since only images are processed - if ($file->type() == 'image') { + if (in_array($file->extension(), ['jpg', 'jpeg', 'png'])) { // WebPConvert options $path = $file->contentFileDirectory() . '/'; $input = $path . $file->filename();