Skip to content

Commit 39d0058

Browse files
committed
WIP
1 parent 9d9f32b commit 39d0058

File tree

3 files changed

+129
-90
lines changed

3 files changed

+129
-90
lines changed

app/Http/Controllers/CoverController.php

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
namespace App\Http\Controllers;
44

55
use Illuminate\Http\Request;
6-
use Illuminate\Support\Facades\Cache;
76
use Illuminate\Support\Str;
8-
use Intervention\Image\AbstractFont;
9-
use Intervention\Image\Facades\Image;
10-
use Laminas\Stdlib\StringWrapper\MbString;
7+
use Intervention\Image\ImageManager;
8+
use Intervention\Image\Drivers\GD\Driver;
9+
use Intervention\Image\Typography\FontFactory;
1110

1211
class CoverController extends Controller
1312
{
@@ -18,46 +17,34 @@ class CoverController extends Controller
1817
*/
1918
public function image(Request $request)
2019
{
21-
$text = Str::limit($request->input('text', config('site.name')), 60);
20+
$text = Str::of($request->input('text', config('site.name')))
21+
->replaceMatches('/[^\p{L}\p{N}\p{Z}\p{P}]/u', '') // remove all non-letters, non-numbers, non-punctuation
22+
->trim()
23+
->squish()
24+
->limit(60);
2225

23-
$key = 'cover-'.sha1($text);
26+
$width = 1920;
27+
$height = 1080;
2428

25-
$data = Cache::remember($key, now()->addHours(4), function () use ($text) {
26-
$width = 1920;
27-
$height = 1080;
29+
$start_x = 230;
30+
$start_y = $height / 2 + 40;
2831

29-
$start_x = 230;
30-
$start_y = $height / 2 + 40;
31-
$max_len = 22;
32+
$manager = new ImageManager(new Driver());
3233

33-
$mbWrap = new MbString();
34-
$textWrap = $mbWrap->wordWrap($text, $max_len);
34+
$image = $manager->read(public_path('/img/share/socials.png'));
3535

36-
$lines = Str::of($textWrap)->explode("\n");
36+
$image
37+
->text($text, $start_x, $start_y, fn(FontFactory $font) => $font->filename(public_path('fonts/cover.ttf'))
38+
->size(90)
39+
->color('#222222')
40+
->align('left')
41+
->wrap(1100)
42+
->lineHeight(1.6)
43+
->valign('center'));
3744

38-
$font_size = min(100 - $lines->count() * 5, 100);
39-
$font_height = min(75 - $lines->count() * 5, 75);
45+
$image->scaleDown($width, $height);
4046

41-
$y = round($start_y - ((count($lines) - 1) * $font_height));
42-
43-
$image = Image::make(public_path('/img/share/socials.png'), 0, 0);
44-
45-
$image->fit($width, $height, fn ($constraint) => $constraint->aspectRatio());
46-
47-
$lines->each(function ($line) use ($image, $start_x, $font_size, $font_height, &$y) {
48-
49-
$image->text($line, $start_x, round($y), fn (AbstractFont $font) => $font->file(public_path('fonts/cover.ttf'))
50-
->size($font_size)
51-
->color('#222222')
52-
->align('left')
53-
->valign('center'));
54-
55-
$y += $font_height * 2;
56-
});
57-
58-
return (string) $image->encode('data-url');
59-
});
60-
61-
return Image::make($data)->encode('jpg', 75)->response();
47+
return response($image->toJpeg(75))
48+
->header('Content-Type', 'image/jpeg');
6249
}
6350
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"esplora/spire": "0.0.1",
2424
"guzzlehttp/guzzle": "^7.2",
2525
"hotwired/turbo-laravel": "^1.12",
26-
"intervention/image": "^2.7",
26+
"intervention/image": "^3.4",
2727
"jolicode/jolitypo": "^1.4",
2828
"laminas/laminas-stdlib": "^3.18",
2929
"laravel-notification-channels/telegram": "^4.0",

composer.lock

Lines changed: 103 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)