File size: 12,050 Bytes
1501522
 
 
 
 
 
 
d4d07bc
 
 
 
 
 
 
 
 
 
 
 
 
 
1501522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
071a77d
1501522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
071a77d
1501522
 
071a77d
 
 
 
 
 
1501522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php

namespace App\Services;

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use RuntimeException;
use function imagecreatetruecolor;
use function imageantialias;
use function imagepng;
use function imagedestroy;
use function imageline;
use function imagecolorallocate;
use function imagecolorallocatealpha;
use function imagefilledellipse;
use function imagefilledrectangle;
use function imagettftext;
use function imagettfbbox;
use function imagestring;
use function imagearc;
use function imagefilledpolygon;

class PosterImageService
{
    public function __construct(private ?HuggingFaceMediaStorageService $hfMediaStorage = null)
    {
    }

    public function generate(string $prompt, string $aspect = 'portrait', int $count = 1): array
    {
        if (! extension_loaded('gd')) {
            throw new RuntimeException('GD extension belum aktif, jadi fallback poster image belum bisa dipakai.');
        }

        $count = max(1, min($count, 4));
        [$width, $height] = $this->sizeFromAspect($aspect);
        $urls = [];

        for ($variant = 1; $variant <= $count; $variant++) {
            $image = imagecreatetruecolor($width, $height);
            if ($image === false) {
                throw new RuntimeException('Gagal menyiapkan canvas poster image.');
            }

            if (function_exists('imageantialias')) {
                @imageantialias($image, true);
            }

            $palette = $this->paletteFromPrompt($prompt, $variant);
            $this->drawBackground($image, $width, $height, $palette);
            $this->drawGlassPanels($image, $width, $height, $palette);
            $this->drawHeadline($image, $width, $height, $prompt);

            ob_start();
            imagepng($image);
            $binary = ob_get_clean();
            imagedestroy($image);

            if (! is_string($binary) || $binary === '') {
                throw new RuntimeException('Gagal mengekspor poster image ke PNG.');
            }

            $filename = 'poster_'.now()->format('Ymd_His').'_'.Str::lower(Str::random(8)).'.png';
            $path = 'images/'.$filename;
            Storage::disk('public')->put($path, $binary);
            $urls[] = $this->offloadPublicPath($path);
        }

        return $urls;
    }

    private function offloadPublicPath(string $relativePath): string
    {
        try {
            return $this->hfMedia()->offloadPublicRelativePath($relativePath);
        } catch (\Throwable) {
            return Storage::url($relativePath);
        }
    }

    private function hfMedia(): HuggingFaceMediaStorageService
    {
        if ($this->hfMediaStorage instanceof HuggingFaceMediaStorageService) {
            return $this->hfMediaStorage;
        }

        $service = app(HuggingFaceMediaStorageService::class);
        if (! $service instanceof HuggingFaceMediaStorageService) {
            throw new RuntimeException('Service HuggingFaceMediaStorageService tidak tersedia.');
        }

        $this->hfMediaStorage = $service;

        return $this->hfMediaStorage;
    }

    private function sizeFromAspect(string $aspect): array
    {
        return match ($aspect) {
            'square' => [1080, 1080],
            'landscape' => [1536, 1024],
            default => [1080, 1920],
        };
    }

    private function paletteFromPrompt(string $prompt, int $variant): array
    {
        $seed = abs(crc32(Str::lower($prompt).'|'.$variant));
        $palettes = [
            ['#07111a', '#102338', '#43d9bd', '#f5b96b'],
            ['#10131f', '#1b2940', '#8de2ff', '#ff7a59'],
            ['#0b1220', '#1e1730', '#9d8cff', '#43d9bd'],
            ['#120e19', '#27324b', '#f07cab', '#f5b96b'],
        ];

        return $palettes[$seed % count($palettes)];
    }

    private function drawBackground($image, int $width, int $height, array $palette): void
    {
        [$top, $bottom, $accentA, $accentB] = $palette;

        for ($y = 0; $y < $height; $y++) {
            $ratio = $height > 1 ? $y / ($height - 1) : 0;
            [$r, $g, $b] = $this->mixHex($top, $bottom, $ratio);
            imageline($image, 0, $y, $width, $y, imagecolorallocate($image, $r, $g, $b));
        }

        $this->drawGlow($image, (int) ($width * 0.2), (int) ($height * 0.14), (int) ($width * 0.38), $accentA, 88);
        $this->drawGlow($image, (int) ($width * 0.82), (int) ($height * 0.18), (int) ($width * 0.3), $accentB, 72);
        $this->drawGlow($image, (int) ($width * 0.76), (int) ($height * 0.78), (int) ($width * 0.35), '#ffffff', 24);
    }

    private function drawGlassPanels($image, int $width, int $height, array $palette): void
    {
        $stroke = imagecolorallocatealpha($image, 255, 255, 255, 90);
        $glass = imagecolorallocatealpha($image, 255, 255, 255, 112);
        $accent = $this->allocateHex($image, $palette[2], 45);
        $soft = imagecolorallocatealpha($image, 255, 255, 255, 118);

        $this->roundedRectangle($image, 72, 96, $width - 72, $height - 136, 42, $glass, true);
        $this->roundedRectangle($image, 72, 96, $width - 72, $height - 136, 42, $stroke, false);
        $this->roundedRectangle($image, 118, 162, $width - 118, (int) ($height * 0.55), 36, $soft, true);
        $this->roundedRectangle($image, 118, 162, $width - 118, (int) ($height * 0.55), 36, $stroke, false);
        $this->roundedRectangle($image, 118, (int) ($height * 0.64), (int) ($width * 0.7), (int) ($height * 0.73), 28, $accent, true);
        $this->roundedRectangle($image, 118, (int) ($height * 0.78), (int) ($width * 0.58), (int) ($height * 0.845), 22, $soft, true);

        imagefilledellipse(
            $image,
            (int) ($width * 0.24),
            (int) ($height * 0.34),
            (int) ($width * 0.22),
            (int) ($width * 0.22),
            $this->allocateHex($image, $palette[3], 28)
        );
    }

    private function drawHeadline($image, int $width, int $height, string $prompt): void
    {
        $textColor = imagecolorallocate($image, 246, 251, 255);
        $muted = imagecolorallocate($image, 194, 207, 225);
        $fontBold = $this->resolveFont([
            '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',
            '/usr/share/fonts/truetype/liberation2/LiberationSans-Bold.ttf',
        ]);
        $fontRegular = $this->resolveFont([
            '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
            '/usr/share/fonts/truetype/liberation2/LiberationSans-Regular.ttf',
        ]);

        $title = Str::title(Str::limit(trim($prompt), 90, ''));
        $subtitle = 'VERTICAL STORY ASSET 9:16';

        if ($fontBold !== null && $fontRegular !== null && function_exists('imagettftext')) {
            $lines = $this->wrapText($title, $fontBold, 64, $width - 260, 4);
            $y = (int) ($height * 0.64);

            foreach ($lines as $line) {
                imagettftext($image, 64, 0, 120, $y, $textColor, $fontBold, $line);
                $y += 82;
            }

            imagettftext($image, 28, 0, 120, $height - 150, $muted, $fontRegular, $subtitle);
            imagettftext($image, 20, 0, 120, $height - 112, $muted, $fontRegular, 'Generated locally as premium fallback media for social automation.');

            return;
        }

        imagestring($image, 5, 120, (int) ($height * 0.65), Str::limit($title, 36, ''), $textColor);
        imagestring($image, 3, 120, $height - 140, $subtitle, $muted);
    }

    private function wrapText(string $text, string $font, int $size, int $maxWidth, int $maxLines): array
    {
        $words = preg_split('/\s+/', trim($text)) ?: [];
        $lines = [];
        $current = '';

        $hasFreeType = function_exists('imagettfbbox');
        foreach ($words as $word) {
            $candidate = trim($current.' '.$word);
            if ($hasFreeType) {
                $box = imagettfbbox($size, 0, $font, $candidate);
                $lineWidth = is_array($box) ? abs($box[2] - $box[0]) : 0;
            } else {
                $lineWidth = (int) (strlen($candidate) * $size * 0.55);
            }

            if ($current !== '' && $lineWidth > $maxWidth) {
                $lines[] = $current;
                $current = $word;
            } else {
                $current = $candidate;
            }

            if (count($lines) >= $maxLines - 1) {
                break;
            }
        }

        if ($current !== '') {
            $remaining = array_slice($words, count(explode(' ', trim(implode(' ', $lines).' '.$current))));
            if ($remaining !== []) {
                $current = trim($current.' '.implode(' ', $remaining));
            }

            $lines[] = Str::limit($current, 42, '...');
        }

        return array_slice($lines, 0, $maxLines);
    }

    private function roundedRectangle($image, int $x1, int $y1, int $x2, int $y2, int $radius, int $color, bool $filled): void
    {
        $diameter = $radius * 2;

        if ($filled) {
            imagefilledrectangle($image, $x1 + $radius, $y1, $x2 - $radius, $y2, $color);
            imagefilledrectangle($image, $x1, $y1 + $radius, $x2, $y2 - $radius, $color);
            imagefilledellipse($image, $x1 + $radius, $y1 + $radius, $diameter, $diameter, $color);
            imagefilledellipse($image, $x2 - $radius, $y1 + $radius, $diameter, $diameter, $color);
            imagefilledellipse($image, $x1 + $radius, $y2 - $radius, $diameter, $diameter, $color);
            imagefilledellipse($image, $x2 - $radius, $y2 - $radius, $diameter, $diameter, $color);

            return;
        }

        imageline($image, $x1 + $radius, $y1, $x2 - $radius, $y1, $color);
        imageline($image, $x1 + $radius, $y2, $x2 - $radius, $y2, $color);
        imageline($image, $x1, $y1 + $radius, $x1, $y2 - $radius, $color);
        imageline($image, $x2, $y1 + $radius, $x2, $y2 - $radius, $color);
        imagearc($image, $x1 + $radius, $y1 + $radius, $diameter, $diameter, 180, 270, $color);
        imagearc($image, $x2 - $radius, $y1 + $radius, $diameter, $diameter, 270, 360, $color);
        imagearc($image, $x1 + $radius, $y2 - $radius, $diameter, $diameter, 90, 180, $color);
        imagearc($image, $x2 - $radius, $y2 - $radius, $diameter, $diameter, 0, 90, $color);
    }

    private function drawGlow($image, int $centerX, int $centerY, int $radius, string $hex, int $maxAlpha): void
    {
        for ($i = $radius; $i > 0; $i -= max(6, (int) ($radius / 12))) {
            $alpha = max(0, min(127, (int) ($maxAlpha + (($radius - $i) / max(1, $radius)) * 34)));
            imagefilledellipse(
                $image,
                $centerX,
                $centerY,
                $i * 2,
                $i * 2,
                $this->allocateHex($image, $hex, $alpha)
            );
        }
    }

    private function mixHex(string $from, string $to, float $ratio): array
    {
        $ratio = max(0, min(1, $ratio));
        [$fr, $fg, $fb] = $this->hexToRgb($from);
        [$tr, $tg, $tb] = $this->hexToRgb($to);

        return [
            (int) round($fr + (($tr - $fr) * $ratio)),
            (int) round($fg + (($tg - $fg) * $ratio)),
            (int) round($fb + (($tb - $fb) * $ratio)),
        ];
    }

    private function allocateHex($image, string $hex, int $alpha = 0): int
    {
        [$r, $g, $b] = $this->hexToRgb($hex);

        return imagecolorallocatealpha($image, $r, $g, $b, max(0, min(127, $alpha)));
    }

    private function hexToRgb(string $hex): array
    {
        $value = ltrim($hex, '#');
        if (strlen($value) === 3) {
            $value = preg_replace('/(.)/', '$1$1', $value) ?? $value;
        }

        return [
            hexdec(substr($value, 0, 2)),
            hexdec(substr($value, 2, 2)),
            hexdec(substr($value, 4, 2)),
        ];
    }

    private function resolveFont(array $candidates): ?string
    {
        foreach ($candidates as $candidate) {
            if (is_file($candidate)) {
                return $candidate;
            }
        }

        return null;
    }
}