Files
lelong/app/Services/ResitPhotoService.php
T
Nur Izzati Zulkifli 1ecd3141d6 add view photo resit
2022-12-21 16:13:41 +08:00

169 lines
5.7 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Models\FileResit;
use App\Models\LelongBid;
use Exception;
use Buglinjo\LaravelWebp\Facades\Webp;
class ResitPhotoService
{
public function store(Request $request)
{
try {
$file_type = $request->gambarMarhun->getClientOriginalExtension();
} catch (Exception $exception) {
$file_type = 'PNG';
logger($exception);
}
$addfile = new FileResit();
$addfile->lelong_id = $request->lelong_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->lelong_id = $request->lelong_id;
$addfile->save();
}
$user_id = LelongBid::select('norujukan')->where('id', $request->lelongbid_id)->first()->norujukan;
$photo_type_name = $request->photo_type === '1' ? 'overall_photo' : 'by_marhun_photo';
$directory = "/{$user_id}/{$photo_type_name}/";
$imageExtension = $file_type;
$resitImageFileName = "overall";
if (!empty($request->lelongbidmarhun_id)) {
$resitImageFileName = "$request->lelongbidmarhun_id";
}
Storage::disk('resit')->putFileAs($directory, $request->gambarMarhun, "${resitImageFileName}.${imageExtension}");
$stored_path = Storage::disk('marhun')->path("{$directory}{$resitImageFileName}.${imageExtension}");
$this->optimizeImageToWebP($stored_path, $directory, $resitImageFileName);
Storage::disk('resit')->delete("{$directory}${resitImageFileName}.${imageExtension}");
$addfile->update([
'file_type' => 'webp',
]);
}
public function delete(Request $request)
{
$deleteFile = FileResit::where('id', '=', $request->fileresit_id)->first();
$user_id = User::select('user_id')->where('id', $request->lelongbid_id)->first()->norujukan;
$photo_type_name = $request->photo_type === '1' ? 'overall_photo' : 'by_marhun_photo';
$directory = "/{$user_id}/{$photo_type_name}/";
$imageExtension = $deleteFile->file_type;
$resitImageFileName = "overall.{$imageExtension}";
$lelongbidmarhun_id = $deleteFile->lelongbidmarhun_id;
if (!empty($request->lelongbidmarhun_id)) {
$resitImageFileName = "$lelongbidmarhun_id.$imageExtension";
}
Storage::disk('marhun')->delete("{$directory}${resitImageFileName}");
$deleteFile->delete();
}
public function show($user_id, $resit)
{
$DS = DIRECTORY_SEPARATOR;
$lelong_id = $resit->lelong_id;
$fileresit_id = $resit->id;
// dd($fileresit_id);;
// $photo_type_name = $marhun->photo_type === 1 ? 'overall_photo' : 'by_marhun_photo';
$directory = $user_id.$DS.$lelong_id.$DS;
// dd($directory);
$imageExtension = $resit->file_type;
// dd($imageExtension);
// $resitImageFileName = "{$fileresit_id}.{$imageExtension}";
if (!empty($fileresit_id)) {
$resitImageFileName = "$fileresit_id.$imageExtension";
// dd($resitImageFileName);
}
$filePath = Storage::disk('resit')->path($directory.$resitImageFileName);
// $filePath = 'C:\laragon\www\lelong2\storage\app\resit\20\2\1.webp';
// dd($filePath);
// header('Content-Type: ' . mime_content_type($filePath));
header('Content-Disposition: inline; filename="' . basename($filePath) . '"');
readfile($filePath);
// dd($filePath);
}
public function get($user_id, $resit)
{
$lelong_id = $resit->lelong_id;
$fileresit_id = $resit->id;
// $photo_type_name = $marhun->photo_type === 1 ? 'overall_photo' : 'by_marhun_photo';
$directory = "/{$user_id}/{$lelong_id}/";
$imageExtension = $resit->file_type;
$resitImageFileName = "overall.{$imageExtension}";
if (!empty($fileresit_id)) {
$resitImageFileName = "overall.$imageExtension";
}
return Storage::disk('marhun')->get("{$directory}${resitImageFileName}");
}
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.resit.root') . '/' . "{$directory}{$lelongbidmarhun_id}.webp";
$webpCompiler = $this->getWebpCompilerByOS();
exec($webpCompiler . ' -preset photo ' . ' -resize 1280 720 ' . ' -mt ' . $originalFilePath . ' -o ' . $convertedFilePath);
}
}