4
4
5
5
use App \Models \Article ;
6
6
use Illuminate \Http \Response ;
7
+ use Illuminate \Support \Facades \Cache ;
7
8
use Intervention \Image \Drivers \Gd \Driver ;
8
9
use Intervention \Image \ImageManager ;
9
10
@@ -30,14 +31,21 @@ public function handle(): Response
30
31
$ image = new ImageManager (new Driver );
31
32
$ text = wordwrap ($ this ->article ->title (), self ::CHARACTERS_PER_LINE );
32
33
33
- return response (
34
- $ image ->read (resource_path ('images/ ' . self ::TEMPLATE ))
35
- ->text ($ text , self ::TEXT_X_POSITION , self ::TEXT_Y_POSITION , function ($ font ) {
36
- $ font ->file (resource_path ('fonts/ ' . self ::FONT ));
37
- $ font ->size (self ::FONT_SIZE );
38
- $ font ->color (self ::TEXT_COLOUR );
39
- })
40
- ->toPng ()
41
- )->header ('Content-Type ' , 'image/png ' );
34
+ return Cache::remember (
35
+ 'articleSocialImage- ' . $ this ->article ->id ,
36
+ now ()->addDay (),
37
+ function () use ($ image , $ text ) {
38
+ return response (
39
+ $ image ->read (resource_path ('images/ ' . self ::TEMPLATE ))
40
+ ->text ($ text , self ::TEXT_X_POSITION , self ::TEXT_Y_POSITION , function ($ font ) {
41
+ $ font ->file (resource_path ('fonts/ ' . self ::FONT ));
42
+ $ font ->size (self ::FONT_SIZE );
43
+ $ font ->color (self ::TEXT_COLOUR );
44
+ })
45
+ ->toPng ()
46
+ )->header ('Content-Type ' , 'image/png ' )
47
+ ->header ('Cache-Control ' , 'max-age=86400, public ' );
48
+ }
49
+ );
42
50
}
43
51
}
0 commit comments