fix: kemas kini CertificateService untuk Intervention Image v4 API

v3 → v4 breaking changes:
- manager->read()       → manager->decodePath()
- image->toJpeg($q)     → image->encode(new JpegEncoder($q))
- font->align($h) + font->valign($v) → font->align($h, $v)
- Storage::path()       → Storage::disk('local')->path() (eksplisit)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Saufi
2026-05-18 21:01:47 +08:00
parent 0fd202f974
commit 29d85eea86

View File

@@ -9,6 +9,7 @@ use App\Models\CertificateTemplate;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManager; use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver; use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\Encoders\JpegEncoder;
use Intervention\Image\Typography\FontFactory; use Intervention\Image\Typography\FontFactory;
class CertificateService class CertificateService
@@ -32,21 +33,19 @@ class CertificateService
throw new \RuntimeException('Template sijil tidak dijumpai.'); 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)) { if (! file_exists($templatePath)) {
throw new \RuntimeException('Fail template sijil tidak dijumpai di storage.'); throw new \RuntimeException('Fail template sijil tidak dijumpai di storage.');
} }
$image = $this->manager->read($templatePath); $image = $this->manager->decodePath($templatePath);
$config = $template->config_json ?? []; $config = $template->config_json ?? [];
$fields = $config['fields'] ?? []; $fields = $config['fields'] ?? [];
// Overlay name
if (isset($fields['name'])) { if (isset($fields['name'])) {
$this->writeText($image, $certificate->participant->name, $fields['name']); $this->writeText($image, $certificate->participant->name, $fields['name']);
} }
// Overlay certificate number if configured
if (isset($fields['certificate_no']) && $certificate->certificate_no) { if (isset($fields['certificate_no']) && $certificate->certificate_no) {
$this->writeText($image, $certificate->certificate_no, $fields['certificate_no']); $this->writeText($image, $certificate->certificate_no, $fields['certificate_no']);
} }
@@ -54,8 +53,9 @@ class CertificateService
$outputDir = 'certificates/' . $certificate->program_id; $outputDir = 'certificates/' . $certificate->program_id;
$outputFile = $outputDir . '/' . $certificate->uuid . '.jpg'; $outputFile = $outputDir . '/' . $certificate->uuid . '.jpg';
Storage::makeDirectory($outputDir); Storage::disk('local')->makeDirectory($outputDir);
$image->toJpeg(90)->save(Storage::path($outputFile)); $image->encode(new JpegEncoder(90))
->save(Storage::disk('local')->path($outputFile));
$certificate->update([ $certificate->update([
'file_path' => $outputFile, 'file_path' => $outputFile,
@@ -73,8 +73,8 @@ class CertificateService
public function generatePreview(CertificateTemplate $template, string $sampleName, string $sampleNo = ''): string public function generatePreview(CertificateTemplate $template, string $sampleName, string $sampleNo = ''): string
{ {
$templatePath = Storage::path($template->image_path); $templatePath = Storage::disk('local')->path($template->image_path);
$image = $this->manager->read($templatePath); $image = $this->manager->decodePath($templatePath);
$config = $template->config_json ?? []; $config = $template->config_json ?? [];
$fields = $config['fields'] ?? []; $fields = $config['fields'] ?? [];
@@ -86,10 +86,10 @@ class CertificateService
$this->writeText($image, $sampleNo, $fields['certificate_no']); $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'); $fontFile = $this->resolveFontPath($cfg['font_file'] ?? 'DejaVuSans-Bold.ttf');
$fontSize = (int) ($cfg['font_size'] ?? 48); $fontSize = (int) ($cfg['font_size'] ?? 48);
@@ -103,8 +103,7 @@ class CertificateService
$font->filename($fontFile); $font->filename($fontFile);
$font->size($fontSize); $font->size($fontSize);
$font->color($fontColor); $font->color($fontColor);
$font->align($align); $font->align($align, $valign); // v4: satu kaedah untuk horizontal + vertical
$font->valign($valign);
}); });
} }