From 290e6ce6b712abb81fa7ff4a17e8a0f471739790 Mon Sep 17 00:00:00 2001 From: "afiq.hamzah" Date: Wed, 7 Dec 2022 13:43:35 +0800 Subject: [PATCH] Add command to make webp binaries executables --- app/Console/Commands/WebpMakeExecutables.php | 72 ++++++++++++++++++++ app/Services/MarhunPhotoService.php | 27 +++----- 2 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 app/Console/Commands/WebpMakeExecutables.php diff --git a/app/Console/Commands/WebpMakeExecutables.php b/app/Console/Commands/WebpMakeExecutables.php new file mode 100644 index 00000000..c1be1f54 --- /dev/null +++ b/app/Console/Commands/WebpMakeExecutables.php @@ -0,0 +1,72 @@ + 'binaries' . $DS . 'libwebp-1.2.4-windows-x64' . $DS . 'bin' . $DS . 'cwebp', + 'Linux' => 'binaries' . $DS . 'libwebp-1.2.4-linux-x86-64' . $DS . 'bin' . $DS .'cwebp', + 'Darwin' => [ + 'X86-64' => 'binaries' . $DS . 'libwebp-1.2.4-mac-X86-64' . $DS . 'bin' . $DS . 'cwebp', + 'arm64' => 'binaries' . $DS . 'libwebp-1.2.4-mac-arm64' . $DS . 'bin' . $DS .'cwebp', + ], + ]; + + $server_operating_system = explode(' ', trim(php_uname()))[0]; + $server_processor_architecture = trim(php_uname('m')); + + + if($server_operating_system === 'Windows'){ + $this->error('Windows OS are not permitted'); + return; + } + + if ($server_operating_system === 'Darwin') { + $webp_binary_path = resource_path($cwebp_compilers['Darwin'][$server_processor_architecture]); + } + + if ($server_operating_system === 'Linux') { + $webp_binary_path = resource_path($cwebp_compilers[$server_operating_system]); + } + + exec('chmod +x ' . $webp_binary_path, $output, $result); + + $this->info('Webp binaries are now executables'); + } +} diff --git a/app/Services/MarhunPhotoService.php b/app/Services/MarhunPhotoService.php index 619a2f80..63392384 100644 --- a/app/Services/MarhunPhotoService.php +++ b/app/Services/MarhunPhotoService.php @@ -50,9 +50,7 @@ class MarhunPhotoService Storage::disk('marhun')->putFileAs($directory, $request->gambarMarhun, "${marhunImageFileName}.${imageExtension}"); $stored_path = Storage::disk('marhun')->path("{$directory}{$marhunImageFileName}.${imageExtension}"); - $convertedFilePath = $this->optimizeImageToWebP($stored_path, $directory, $marhunImageFileName); - - $this->changeFileOwnerToServerUser($convertedFilePath); + $this->optimizeImageToWebP($stored_path, $directory, $marhunImageFileName); Storage::disk('marhun')->delete("{$directory}${marhunImageFileName}.${imageExtension}"); @@ -126,12 +124,14 @@ class MarhunPhotoService private function getWebpCompilerByOS() { + $DS = DIRECTORY_SEPARATOR; + $cwebp_compilers = [ - 'Windows' => 'binaries\libwebp-1.2.4-windows-x64\bin\cwebp', - 'Linux' => 'binaries\libwebp-1.2.4-linux-x86-64\bin\cwebp', + 'Windows' => 'binaries' . $DS . 'libwebp-1.2.4-windows-x64' . $DS . 'bin' . $DS . 'cwebp', + 'Linux' => 'binaries' . $DS . 'libwebp-1.2.4-linux-x86-64' . $DS . 'bin' . $DS .'cwebp', 'Darwin' => [ - 'X86-64' => 'binaries\libwebp-1.2.4-mac-X86-64\bin\cwebp', - 'arm64' => 'binaries\libwebp-1.2.4-mac-arm64\bin\cwebp', + 'X86-64' => 'binaries' . $DS . 'libwebp-1.2.4-mac-X86-64' . $DS . 'bin' . $DS . 'cwebp', + 'arm64' => 'binaries' . $DS . 'libwebp-1.2.4-mac-arm64' . $DS . 'bin' . $DS .'cwebp', ], ]; @@ -140,12 +140,11 @@ class MarhunPhotoService if ($server_operating_system === 'Darwin') { return resource_path($cwebp_compilers['Darwin'][$server_processor_architecture]); - } + } if (in_array($server_operating_system, array_keys($cwebp_compilers))) { return resource_path($cwebp_compilers[$server_operating_system]); } - } private function optimizeImageToWebP($originalFilePath, $directory, $lelongbidmarhun_id) @@ -154,14 +153,6 @@ class MarhunPhotoService $webpCompiler = $this->getWebpCompilerByOS(); - exec($webpCompiler . ' -preset photo ' . ' -resize 1280 720 ' . ' -mt ' .$originalFilePath . ' -o ' . $convertedFilePath); - - return $convertedFilePath; - } - - - private function changeFileOwnerToServerUser($filepath) - { - chown($filepath, get_current_user()); + exec($webpCompiler . ' -preset photo ' . ' -resize 1280 720 ' . ' -mt ' . $originalFilePath . ' -o ' . $convertedFilePath); } }