Skip to content

Commit

Permalink
Merge pull request #13 from MichaelvanLaar/master
Browse files Browse the repository at this point in the history
Fix issue with unsupported image type uploads, add missing functionality to remove WebP files when deleting the original files via the panel
  • Loading branch information
felixhaeberle authored Jul 10, 2023
2 parents 696c5cb + b7e1bdc commit f2fa585
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
32 changes: 28 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,41 @@
@include_once __DIR__ . '/vendor/autoload.php';
@include_once __DIR__ . '/src/webp.php';

function shouldGenerateWebP($file) {
return $file->kirby()->option('kirby3-webp', false);
}

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)) {
(new WebP\Convert)->generateWebP($file);
if (shouldGenerateWebP($file)) {
generateWebP($file);
}
},
'file.replace:after' => function ($newFile, $oldFile) {
if ($this->option('kirby3-webp', false)) {
(new WebP\Convert)->generateWebP($newFile);
if (shouldGenerateWebP($newFile)) {
generateWebP($newFile);
}
},
'file.delete:after' => function ($file) {
deleteWebPFiles($file);
},
],
]);
2 changes: 1 addition & 1 deletion lib/Convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f2fa585

Please sign in to comment.