3
3
namespace Statikbe \FilamentFlexibleContentBlockPages \Http \Controllers ;
4
4
5
5
use Artesaos \SEOTools \Facades \SEOTools ;
6
- use Artesaos \SEOTools \SEOMeta ;
6
+ use Artesaos \SEOTools \Facades \ SEOMeta ;
7
7
use Carbon \Carbon ;
8
8
use Illuminate \Foundation \Validation \ValidatesRequests ;
9
9
use Illuminate \Http \Response ;
12
12
use Illuminate \Support \Facades \Cache ;
13
13
use Illuminate \Support \Facades \Storage ;
14
14
use Mcamara \LaravelLocalization \Facades \LaravelLocalization ;
15
+ use Spatie \MediaLibrary \Conversions \ConversionCollection ;
15
16
use Statikbe \FilamentFlexibleContentBlockPages \Facades \FilamentFlexibleContentBlockPages ;
16
17
use Statikbe \FilamentFlexibleContentBlockPages \Models \Page ;
17
18
use Statikbe \FilamentFlexibleContentBlockPages \Models \Settings ;
@@ -20,6 +21,8 @@ class PageController extends Controller
20
21
{
21
22
use ValidatesRequests;
22
23
24
+ const CACHE_SEO_IMAGE_DIMENSIONS = 'seo_image_dimensions:%s ' ;
25
+ const CACHE_SEO_IMAGE_TTL = 60 * 60 * 8 ; // in seconds
23
26
const TEMPLATE_PATH = 'filament-flexible-content-block-pages::pages. ' ;
24
27
25
28
// TODO make an abstract model with the table name to use as class to resolve in the route param
@@ -95,12 +98,24 @@ protected function setBasicSEO(Page $page)
95
98
96
99
protected function setSEOImage (Page $ page )
97
100
{
98
- if ($ seoImage = $ page ->getSEOImageUrl ()) {
99
- SEOTools::opengraph ()->addImage ($ seoImage , $ this ->getSEOImageDimensions ($ seoImage , true ));
100
- SEOTools::twitter ()->addValue ('image ' , $ seoImage );
101
- } else {
101
+ //TODO copy getSEOMedia in HasSEOAttributesTrait
102
+ $ seoMedia = $ page ->getFallbackImageMedia ($ page ->SEOImage ()->first (), $ page ->getSEOImageCollection ());
103
+ $ seoUrl = null ;
104
+ $ imageDimensions = null ;
105
+
106
+ /*if ($seoMedia) {
107
+ $seoUrl = $seoMedia->getUrl($page->getSEOImageConversionName());
108
+ $imageDimensions = $this->getSEOImageDimensions($seoMedia, $page->getSEOImageConversionName());
109
+ } else if()
110
+ $seoUrl = $seoMedia->getUrl($page->getSEOImageConversionName());
111
+ $imageDimensions = $this->getSEOImageDimensions($seoMedia, $page->getSEOImageConversionName());
102
112
$this->setSEODefaultImage($page);
103
113
}
114
+
115
+ if($seoUrl && $imageDimensions) {
116
+ SEOTools::opengraph()->addImage($seoUrl, $imageDimensions);
117
+ SEOTools::twitter()->addValue('image', $seoUrl);
118
+ }*/
104
119
}
105
120
106
121
protected function setSEODefaultImage (Page $ page )
@@ -122,34 +137,20 @@ protected function setSEODefaultImage(Page $page)
122
137
*
123
138
* @return array|mixed
124
139
*/
125
- protected function getSEOImageDimensions ($ path , bool $ isUrl = false )
140
+ protected function getSEOImageDimensions (Media $ seoMedia , string $ conversion )
126
141
{
127
- // TODO optimize loading of image: avoiding url download.
128
- $ seoParams = [];
129
- if ($ path ) {
130
- $ cacheKey = sprintf (self ::CACHE_SEO_IMAGE_DIMENSIONS , $ path );
131
- if (Cache::has ($ cacheKey )) {
132
- $ seoParams = Cache::get ($ cacheKey , []);
133
- } else {
134
- try {
135
- if (! $ isUrl ) {
136
- $ path = Storage::disk ('public ' )->path ($ path );
137
- }
138
- $ dimensions = getimagesize ($ path );
139
- if (count ($ dimensions ) > 1 ) {
140
- $ seoParams = ['width ' => $ dimensions [0 ], 'height ' => $ dimensions [1 ]];
141
- }
142
- } catch (\League \Glide \Filesystem \FileNotFoundException $ ex ) {
143
- // do nothing we will just pass the empty seo params
144
- } catch (ErrorException $ ex ) {
145
- // do nothing we will just pass the empty seo params
146
- }
147
-
148
- Cache::put ($ cacheKey , $ seoParams , Carbon::now ()->addMinutes (self ::CACHE_SEO_IMAGE_TTL ));
149
-
150
- }
151
- }
152
-
153
- return $ seoParams ;
142
+ $ cacheKey = sprintf (self ::CACHE_SEO_IMAGE_DIMENSIONS , $ seoMedia ->uuid );
143
+
144
+ return Cache::remember ($ cacheKey ,
145
+ self ::CACHE_SEO_IMAGE_TTL ,
146
+ function () use ($ seoMedia , $ conversion ) {
147
+ $ conversionCollection = ConversionCollection::createForMedia ($ seoMedia );
148
+ $ conversion = $ conversionCollection ->getByName ($ conversion );
149
+
150
+ return [
151
+ 'width ' => $ conversion ->getWidth (),
152
+ 'height ' => $ conversion ->getHeight (),
153
+ ];
154
+ });
154
155
}
155
156
}
0 commit comments