33namespace Statikbe \FilamentFlexibleContentBlockPages \Http \Controllers ;
44
55use Artesaos \SEOTools \Facades \SEOTools ;
6- use Artesaos \SEOTools \SEOMeta ;
6+ use Artesaos \SEOTools \Facades \ SEOMeta ;
77use Carbon \Carbon ;
88use Illuminate \Foundation \Validation \ValidatesRequests ;
99use Illuminate \Http \Response ;
1212use Illuminate \Support \Facades \Cache ;
1313use Illuminate \Support \Facades \Storage ;
1414use Mcamara \LaravelLocalization \Facades \LaravelLocalization ;
15+ use Spatie \MediaLibrary \Conversions \ConversionCollection ;
1516use Statikbe \FilamentFlexibleContentBlockPages \Facades \FilamentFlexibleContentBlockPages ;
1617use Statikbe \FilamentFlexibleContentBlockPages \Models \Page ;
1718use Statikbe \FilamentFlexibleContentBlockPages \Models \Settings ;
@@ -20,6 +21,8 @@ class PageController extends Controller
2021{
2122 use ValidatesRequests;
2223
24+ const CACHE_SEO_IMAGE_DIMENSIONS = 'seo_image_dimensions:%s ' ;
25+ const CACHE_SEO_IMAGE_TTL = 60 * 60 * 8 ; // in seconds
2326 const TEMPLATE_PATH = 'filament-flexible-content-block-pages::pages. ' ;
2427
2528 // 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)
9598
9699 protected function setSEOImage (Page $ page )
97100 {
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());
102112 $this->setSEODefaultImage($page);
103113 }
114+
115+ if($seoUrl && $imageDimensions) {
116+ SEOTools::opengraph()->addImage($seoUrl, $imageDimensions);
117+ SEOTools::twitter()->addValue('image', $seoUrl);
118+ }*/
104119 }
105120
106121 protected function setSEODefaultImage (Page $ page )
@@ -122,34 +137,20 @@ protected function setSEODefaultImage(Page $page)
122137 *
123138 * @return array|mixed
124139 */
125- protected function getSEOImageDimensions ($ path , bool $ isUrl = false )
140+ protected function getSEOImageDimensions (Media $ seoMedia , string $ conversion )
126141 {
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+ });
154155 }
155156}
0 commit comments