Add command to make webp binaries executables

This commit is contained in:
afiq.hamzah
2022-12-07 13:43:35 +08:00
parent 2c997c0e00
commit 290e6ce6b7
2 changed files with 81 additions and 18 deletions
@@ -0,0 +1,72 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class WebpMakeExecutables extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'webp:make_exec';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Make webp binaries executable';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$DS = DIRECTORY_SEPARATOR;
$cwebp_compilers = [
'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' . $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');
}
}
+9 -18
View File
@@ -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);
}
}