diff --git a/app/Http/Controllers/Hse/EmployeeController.php b/app/Http/Controllers/Hse/EmployeeController.php index 3759a09..5579442 100644 --- a/app/Http/Controllers/Hse/EmployeeController.php +++ b/app/Http/Controllers/Hse/EmployeeController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Hse; use App\Http\Controllers\Controller; use App\Models\Employee; use App\Models\User; +use App\Services\ImageCompressionService; use App\Services\SimperPdfExporter; use App\Services\SimpleXlsxExporter; use App\Services\SimpleXlsxImporter; @@ -536,6 +537,7 @@ class EmployeeController extends Controller 'position' => ['required', 'string'], 'is_operator' => ['required', 'in:1,0'], 'simper_expired_at' => ['nullable', 'date'], + 'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'], ], [ 'nik.required' => 'NIK wajib diisi.', 'nik.unique' => 'NIK sudah terdaftar dalam sistem.', @@ -563,6 +565,11 @@ class EmployeeController extends Controller $user->assignRole('employee'); } + $photoPath = null; + if ($request->hasFile('photo')) { + $photoPath = ImageCompressionService::uploadAndCompress($request->file('photo'), 'employees', 500, 500, 80); + } + Employee::create([ 'nik' => $validated['nik'], 'name' => $validated['name'], @@ -571,6 +578,7 @@ class EmployeeController extends Controller 'position' => $validated['position'], 'is_operator' => $isOperator, 'simper_expired_at' => $simperExpiredAt, + 'photo_path' => $photoPath, 'user_id' => $user->id, ]); @@ -592,6 +600,7 @@ class EmployeeController extends Controller 'position' => ['sometimes', 'required', 'string'], 'is_operator' => ['required', 'in:1,0'], 'simper_expired_at' => ['nullable', 'date'], + 'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'], ], [ 'nik.unique' => 'NIK sudah digunakan oleh karyawan lain.', 'name.required' => 'Nama lengkap wajib diisi.', @@ -601,6 +610,12 @@ class EmployeeController extends Controller $isOperator = (bool) $validated['is_operator']; $simperExpiredAt = $isOperator ? ($validated['simper_expired_at'] ?? $employee->simper_expired_at) : null; + $photoPath = $employee->photo_path; + if ($request->hasFile('photo')) { + ImageCompressionService::deleteIfExists($employee->photo_path); + $photoPath = ImageCompressionService::uploadAndCompress($request->file('photo'), 'employees', 500, 500, 80); + } + $employee->update([ 'nik' => $validated['nik'] ?? $employee->nik, 'name' => $validated['name'] ?? $employee->name, @@ -609,6 +624,7 @@ class EmployeeController extends Controller 'position' => $validated['position'] ?? $employee->position, 'is_operator' => $isOperator, 'simper_expired_at' => $simperExpiredAt, + 'photo_path' => $photoPath, ]); return back()->with('success', "Data SIMPER untuk {$employee->name} berhasil diperbarui."); diff --git a/app/Http/Controllers/Qma/VehicleController.php b/app/Http/Controllers/Qma/VehicleController.php index 09b8cfe..129a720 100644 --- a/app/Http/Controllers/Qma/VehicleController.php +++ b/app/Http/Controllers/Qma/VehicleController.php @@ -9,6 +9,7 @@ use App\Models\P2hRecord; use App\Models\P2hTemplate; use App\Models\Vehicle; use App\Models\VehicleActivity; +use App\Services\ImageCompressionService; use App\Services\SimpleXlsxExporter; use App\Services\VehiclePdfExporter; use Carbon\Carbon; @@ -895,6 +896,7 @@ class VehicleController extends Controller 'kir_document' => ['nullable', 'string', 'max:150'], 'ownership' => ['nullable', 'string', 'max:150'], 'division' => ['nullable', 'string', 'max:100'], + 'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'], ]); return DB::transaction(function () use ($request, $category) { @@ -918,6 +920,11 @@ class VehicleController extends Controller return back()->withInput()->with('error', "Kode unit [{$code}] sudah terdaftar di sistem. Silakan gunakan nomor urut lain."); } + $photoPath = null; + if ($request->hasFile('photo')) { + $photoPath = ImageCompressionService::uploadAndCompress($request->file('photo'), 'vehicles', 800, 800, 75); + } + $unit = Vehicle::create([ 'code' => $code, 'category' => 'unit_support', @@ -939,6 +946,7 @@ class VehicleController extends Controller 'fuel_consumption' => $category->fuel_consumption, 'silo_document' => $request->input('silo_document'), 'kir_document' => $request->input('kir_document'), + 'photo_path' => $photoPath, ]); return back()->with('success', "Unit fisik [{$unit->code}] berhasil ditambahkan ke kategori {$category->unit_category}."); @@ -962,6 +970,7 @@ class VehicleController extends Controller 'division' => ['nullable', 'string', 'max:100'], 'status' => [$category === 'unit_support' ? 'nullable' : 'required', 'string', 'in:Operation,Standby,Breakdown,Maintenance'], 'current_km' => [$category === 'unit_support' ? 'nullable' : 'required', 'numeric', 'min:0'], + 'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'], ]; // Atribut opsional spesifik Unit Support (Kendaraan Berat) @@ -994,6 +1003,11 @@ class VehicleController extends Controller $validated['current_km'] = $validated['current_km'] ?? 0; } + if ($request->hasFile('photo')) { + $validated['photo_path'] = ImageCompressionService::uploadAndCompress($request->file('photo'), 'vehicles', 800, 800, 75); + } + unset($validated['photo']); + $vehicle = Vehicle::create($validated); if ($vehicle->isSupportCategory()) { @@ -1045,6 +1059,7 @@ class VehicleController extends Controller 'stnk_document' => ['nullable', 'string', 'max:150'], 'pajak_document' => ['nullable', 'string', 'max:150'], 'kir_document' => ['nullable', 'string', 'max:150'], + 'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'], ]; $validated = $request->validate($rules, [ @@ -1054,6 +1069,12 @@ class VehicleController extends Controller 'current_km.required' => 'Nilai KM / HM wajib diisi.', ]); + if ($request->hasFile('photo')) { + ImageCompressionService::deleteIfExists($vehicle->photo_path); + $validated['photo_path'] = ImageCompressionService::uploadAndCompress($request->file('photo'), 'vehicles', 800, 800, 75); + } + unset($validated['photo']); + DB::transaction(function () use ($vehicle, $validated, $oldCode, $newCode) { $vehicle->update($validated); @@ -1133,6 +1154,13 @@ class VehicleController extends Controller } } + if ($vehicle->isSupportCategory()) { + foreach ($vehicle->units as $u) { + ImageCompressionService::deleteIfExists($u->photo_path); + } + } + ImageCompressionService::deleteIfExists($vehicle->photo_path); + $code = $vehicle->code; $vehicle->delete(); diff --git a/app/Models/Employee.php b/app/Models/Employee.php index cadf280..5f02434 100644 --- a/app/Models/Employee.php +++ b/app/Models/Employee.php @@ -30,6 +30,10 @@ class Employee extends Model 'simper_expired_at' => 'date', ]; + protected $appends = [ + 'photo_url', + ]; + /** * Cek apakah SIMPER masih aktif hari ini. */ @@ -42,6 +46,19 @@ class Employee extends Model return $this->simper_expired_at->isFuture() || $this->simper_expired_at->isToday(); } + public function getPhotoUrlAttribute(): ?string + { + if (!$this->photo_path) { + return null; + } + + if (str_starts_with($this->photo_path, 'http://') || str_starts_with($this->photo_path, 'https://')) { + return $this->photo_path; + } + + return asset('storage/' . ltrim($this->photo_path, '/')); + } + /** * Scope untuk mengambil hanya karyawan yang berstatus operator/driver. */ diff --git a/app/Models/Vehicle.php b/app/Models/Vehicle.php index 04bfae8..49ca044 100644 --- a/app/Models/Vehicle.php +++ b/app/Models/Vehicle.php @@ -46,11 +46,28 @@ class Vehicle extends Model 'parent_id' => 'integer', ]; + protected $appends = [ + 'photo_url', + ]; + public function isLightVehicle(): bool { return $this->category === 'light_vehicle'; } + public function getPhotoUrlAttribute(): ?string + { + if (!$this->photo_path) { + return null; + } + + if (str_starts_with($this->photo_path, 'http://') || str_starts_with($this->photo_path, 'https://')) { + return $this->photo_path; + } + + return asset('storage/' . ltrim($this->photo_path, '/')); + } + public function isUnitSupport(): bool { return $this->category === 'unit_support'; diff --git a/app/Services/ImageCompressionService.php b/app/Services/ImageCompressionService.php new file mode 100644 index 0000000..6d6c3b0 --- /dev/null +++ b/app/Services/ImageCompressionService.php @@ -0,0 +1,150 @@ +guessExtension() ?: 'jpg'); + return $file->storeAs($cleanDir, $fallbackName, 'public'); + } + + try { + $realPath = $file->getRealPath(); + $imageContent = file_get_contents($realPath); + if ($imageContent === false) { + $fallbackName = Str::random(40) . '.' . ($file->guessExtension() ?: 'jpg'); + return $file->storeAs($cleanDir, $fallbackName, 'public'); + } + + // Buat resource gambar dari data string + $srcImage = @imagecreatefromstring($imageContent); + if (!$srcImage) { + $fallbackName = Str::random(40) . '.' . ($file->guessExtension() ?: 'jpg'); + return $file->storeAs($cleanDir, $fallbackName, 'public'); + } + + // 1. Tangani Auto-Orientation berdasarkan EXIF (kamera smartphone) + if (function_exists('exif_read_data')) { + $exif = @exif_read_data($realPath); + if (!empty($exif['Orientation'])) { + $srcImage = match ($exif['Orientation']) { + 3 => imagerotate($srcImage, 180, 0), + 6 => imagerotate($srcImage, -90, 0), + 8 => imagerotate($srcImage, 90, 0), + default => $srcImage, + }; + } + } + + // 2. Hitung Dimensi Proporsional + $origWidth = imagesx($srcImage); + $origHeight = imagesy($srcImage); + + if ($origWidth > $maxWidth || $origHeight > $maxHeight) { + $ratio = min($maxWidth / $origWidth, $maxHeight / $origHeight); + $targetWidth = max(1, (int) round($origWidth * $ratio)); + $targetHeight = max(1, (int) round($origHeight * $ratio)); + } else { + $targetWidth = $origWidth; + $targetHeight = $origHeight; + } + + // 3. Buat Canvas Truecolor Baru + $canvas = imagecreatetruecolor($targetWidth, $targetHeight); + + // Beri latar belakang putih solid jika ada area transparan (PNG/WebP) + $white = imagecolorallocate($canvas, 255, 255, 255); + imagefilledrectangle($canvas, 0, 0, $targetWidth, $targetHeight, $white); + + // 4. Salin & Resample Gambar dengan Kualitas Tinggi + imagecopyresampled( + $canvas, + $srcImage, + 0, + 0, + 0, + 0, + $targetWidth, + $targetHeight, + $origWidth, + $origHeight + ); + + // 5. Simpan Hasil Kompresi Gambar ke Buffer Memori sesuai format yang didukung + $compressedStream = null; + $extension = 'jpg'; + + if (function_exists('imagejpeg')) { + $extension = 'jpg'; + ob_start(); + imagejpeg($canvas, null, $quality); + $compressedStream = ob_get_clean(); + } elseif (function_exists('imagewebp')) { + $extension = 'webp'; + ob_start(); + imagewebp($canvas, null, $quality); + $compressedStream = ob_get_clean(); + } elseif (function_exists('imagepng')) { + $extension = 'png'; + $pngLevel = max(0, min(9, (int) round((100 - $quality) / 10))); + ob_start(); + imagepng($canvas, null, $pngLevel); + $compressedStream = ob_get_clean(); + } + + // Bersihkan resource GD dari memori + imagedestroy($canvas); + imagedestroy($srcImage); + + if (!empty($compressedStream)) { + $fileName = Str::random(40) . '.' . $extension; + $targetPath = $cleanDir . '/' . $fileName; + Storage::disk('public')->put($targetPath, $compressedStream); + return $targetPath; + } + } catch (\Throwable $e) { + // Jika terjadi kesalahan proses GD, fallback aman simpan file asli + } + + $fallbackName = Str::random(40) . '.' . ($file->guessExtension() ?: 'jpg'); + return $file->storeAs($cleanDir, $fallbackName, 'public'); + } + + /** + * Hapus file foto lama dari disk publik jika ada. + * + * @param string|null $path + * @return void + */ + public static function deleteIfExists(?string $path): void + { + if (!empty($path) && Storage::disk('public')->exists($path)) { + Storage::disk('public')->delete($path); + } + } +} diff --git a/resources/views/hse/employees_index.blade.php b/resources/views/hse/employees_index.blade.php index 162bd91..6bbaabe 100644 --- a/resources/views/hse/employees_index.blade.php +++ b/resources/views/hse/employees_index.blade.php @@ -151,8 +151,19 @@