3
3
namespace App \Http \Controllers ;
4
4
5
5
use Illuminate \Http \Request ;
6
- use Illuminate \Support \Facades \Cache ;
7
6
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 ;
11
10
12
11
class CoverController extends Controller
13
12
{
@@ -18,46 +17,34 @@ class CoverController extends Controller
18
17
*/
19
18
public function image (Request $ request )
20
19
{
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 );
22
25
23
- $ key = 'cover- ' .sha1 ($ text );
26
+ $ width = 1920 ;
27
+ $ height = 1080 ;
24
28
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 ;
28
31
29
- $ start_x = 230 ;
30
- $ start_y = $ height / 2 + 40 ;
31
- $ max_len = 22 ;
32
+ $ manager = new ImageManager (new Driver ());
32
33
33
- $ mbWrap = new MbString ();
34
- $ textWrap = $ mbWrap ->wordWrap ($ text , $ max_len );
34
+ $ image = $ manager ->read (public_path ('/img/share/socials.png ' ));
35
35
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 ' ));
37
44
38
- $ font_size = min (100 - $ lines ->count () * 5 , 100 );
39
- $ font_height = min (75 - $ lines ->count () * 5 , 75 );
45
+ $ image ->scaleDown ($ width , $ height );
40
46
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 ' );
62
49
}
63
50
}
0 commit comments