diff --git a/app/Services/CertificateService.php b/app/Services/CertificateService.php index 79dbdb6..af5af60 100644 --- a/app/Services/CertificateService.php +++ b/app/Services/CertificateService.php @@ -9,6 +9,7 @@ use App\Models\CertificateTemplate; use Illuminate\Support\Facades\Storage; use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Gd\Driver; +use Intervention\Image\Encoders\JpegEncoder; use Intervention\Image\Typography\FontFactory; class CertificateService @@ -32,21 +33,19 @@ class CertificateService throw new \RuntimeException('Template sijil tidak dijumpai.'); } - $templatePath = Storage::path($template->image_path); + $templatePath = Storage::disk('local')->path($template->image_path); if (! file_exists($templatePath)) { throw new \RuntimeException('Fail template sijil tidak dijumpai di storage.'); } - $image = $this->manager->read($templatePath); + $image = $this->manager->decodePath($templatePath); $config = $template->config_json ?? []; $fields = $config['fields'] ?? []; - // Overlay name if (isset($fields['name'])) { $this->writeText($image, $certificate->participant->name, $fields['name']); } - // Overlay certificate number if configured if (isset($fields['certificate_no']) && $certificate->certificate_no) { $this->writeText($image, $certificate->certificate_no, $fields['certificate_no']); } @@ -54,8 +53,9 @@ class CertificateService $outputDir = 'certificates/' . $certificate->program_id; $outputFile = $outputDir . '/' . $certificate->uuid . '.jpg'; - Storage::makeDirectory($outputDir); - $image->toJpeg(90)->save(Storage::path($outputFile)); + Storage::disk('local')->makeDirectory($outputDir); + $image->encode(new JpegEncoder(90)) + ->save(Storage::disk('local')->path($outputFile)); $certificate->update([ 'file_path' => $outputFile, @@ -73,8 +73,8 @@ class CertificateService public function generatePreview(CertificateTemplate $template, string $sampleName, string $sampleNo = ''): string { - $templatePath = Storage::path($template->image_path); - $image = $this->manager->read($templatePath); + $templatePath = Storage::disk('local')->path($template->image_path); + $image = $this->manager->decodePath($templatePath); $config = $template->config_json ?? []; $fields = $config['fields'] ?? []; @@ -86,25 +86,24 @@ class CertificateService $this->writeText($image, $sampleNo, $fields['certificate_no']); } - return $image->toJpeg(85)->toString(); + return $image->encode(new JpegEncoder(85))->toString(); } - private function writeText(\Intervention\Image\Image $image, string $text, array $cfg): void + private function writeText(\Intervention\Image\Interfaces\ImageInterface $image, string $text, array $cfg): void { $fontFile = $this->resolveFontPath($cfg['font_file'] ?? 'DejaVuSans-Bold.ttf'); - $fontSize = (int) ($cfg['font_size'] ?? 48); - $fontColor = (string)($cfg['font_color'] ?? '#000000'); - $align = (string)($cfg['align'] ?? 'center'); + $fontSize = (int) ($cfg['font_size'] ?? 48); + $fontColor = (string)($cfg['font_color'] ?? '#000000'); + $align = (string)($cfg['align'] ?? 'center'); $valign = (string)($cfg['valign'] ?? 'top'); - $x = (int) ($cfg['x'] ?? 0); - $y = (int) ($cfg['y'] ?? 0); + $x = (int) ($cfg['x'] ?? 0); + $y = (int) ($cfg['y'] ?? 0); $image->text($text, $x, $y, function (FontFactory $font) use ($fontFile, $fontSize, $fontColor, $align, $valign) { $font->filename($fontFile); $font->size($fontSize); $font->color($fontColor); - $font->align($align); - $font->valign($valign); + $font->align($align, $valign); // v4: satu kaedah untuk horizontal + vertical }); }