44
55use App \Models \Article ;
66use Illuminate \Http \Response ;
7+ use Illuminate \Support \Facades \Cache ;
78use Intervention \Image \Drivers \Gd \Driver ;
89use Intervention \Image \ImageManager ;
910
@@ -30,14 +31,21 @@ public function handle(): Response
3031 $ image = new ImageManager (new Driver );
3132 $ text = wordwrap ($ this ->article ->title (), self ::CHARACTERS_PER_LINE );
3233
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+ );
4250 }
4351}
0 commit comments