Files
lelong/app/Services/MarhunPhotoService.php
T

159 lines
5.5 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Models\FileMarhun;
use App\Models\LelongBid;
use Exception;
use Buglinjo\LaravelWebp\Facades\Webp;
class MarhunPhotoService
{
public function store(Request $request)
{
try {
$file_type = $request->gambarMarhun->getClientOriginalExtension();
} catch (Exception $exception) {
$file_type = 'PNG';
logger($exception);
}
$addfile = new FileMarhun();
$addfile->lelongbid_id = $request->lelongbid_id;
$addfile->file_type = $file_type;
if ($request->photo_type == 1) {
$addfile->photo_type = 1;
$addfile->save();
} elseif ($request->photo_type == 2) {
$addfile->photo_type = 2;
$addfile->lelongbidmarhun_id = $request->lelongbidmarhun_id;
$addfile->save();
}
$no_rujukan_sag = LelongBid::select('norujukan')->where('id', $request->lelongbid_id)->first()->norujukan;
$photo_type_name = $request->photo_type === '1' ? 'overall_photo' : 'by_marhun_photo';
$directory = "/{$no_rujukan_sag}/{$photo_type_name}/";
$imageExtension = $file_type;
$marhunImageFileName = "overall";
if (!empty($request->lelongbidmarhun_id)) {
$marhunImageFileName = "$request->lelongbidmarhun_id";
}
Storage::disk('marhun')->putFileAs($directory, $request->gambarMarhun, "${marhunImageFileName}.${imageExtension}");
$stored_path = Storage::disk('marhun')->path("{$directory}{$marhunImageFileName}.${imageExtension}");
$this->optimizeImageToWebP($stored_path, $directory, $marhunImageFileName);
Storage::disk('marhun')->delete("{$directory}${marhunImageFileName}.${imageExtension}");
$addfile->update([
'file_type' => 'webp',
]);
}
public function delete(Request $request)
{
$deleteFile = FileMarhun::where('id', '=', $request->filemarhun_id)->first();
$no_rujukan_sag = LelongBid::select('norujukan')->where('id', $request->lelongbid_id)->first()->norujukan;
$photo_type_name = $request->photo_type === '1' ? 'overall_photo' : 'by_marhun_photo';
$directory = "/{$no_rujukan_sag}/{$photo_type_name}/";
$imageExtension = $deleteFile->file_type;
$marhunImageFileName = "overall.{$imageExtension}";
$lelongbidmarhun_id = $deleteFile->lelongbidmarhun_id;
if (!empty($request->lelongbidmarhun_id)) {
$marhunImageFileName = "$lelongbidmarhun_id.$imageExtension";
}
Storage::disk('marhun')->delete("{$directory}${marhunImageFileName}");
$deleteFile->delete();
}
public function show($norujukan_sag, $marhun)
{
$lelongbidmarhun_id = $marhun->lelongbidmarhun_id;
$photo_type_name = $marhun->photo_type === 1 ? 'overall_photo' : 'by_marhun_photo';
$directory = "/{$norujukan_sag}/{$photo_type_name}/";
$imageExtension = $marhun->file_type;
$marhunImageFileName = "overall.{$imageExtension}";
if (!empty($lelongbidmarhun_id)) {
$marhunImageFileName = "$lelongbidmarhun_id.$imageExtension";
}
$filePath = Storage::disk('marhun')->path("{$directory}${marhunImageFileName}");
header('Content-Type: ' . mime_content_type($filePath));
header('Content-Disposition: inline; filename="' . basename($filePath) . '"');
readfile($filePath);
}
public function get($norujukan_sag, $marhun)
{
$lelongbidmarhun_id = $marhun->lelongbidmarhun_id;
$photo_type_name = $marhun->photo_type === 1 ? 'overall_photo' : 'by_marhun_photo';
$directory = "/{$norujukan_sag}/{$photo_type_name}/";
$imageExtension = $marhun->file_type;
$marhunImageFileName = "overall.{$imageExtension}";
if (!empty($lelongbidmarhun_id)) {
$marhunImageFileName = "$lelongbidmarhun_id.$imageExtension";
}
return Storage::disk('marhun')->get("{$directory}${marhunImageFileName}");
}
private function getWebpCompilerByOS()
{
$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 === '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)
{
$convertedFilePath = config('filesystems.disks.marhun.root') . '/' . "{$directory}{$lelongbidmarhun_id}.webp";
$webpCompiler = $this->getWebpCompilerByOS();
exec($webpCompiler . ' -preset photo ' . ' -resize 1920 1080 ' . ' -mt ' . $originalFilePath . ' -o ' . $convertedFilePath);
}
}