‪TYPO3CMS  11.5
MetaTagGenerator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
28 
35 {
41  public function ‪generate(array $params)
42  {
43  ‪$metaTagManagerRegistry = GeneralUtility::makeInstance(MetaTagManagerRegistry::class);
44 
45  if (!empty($params['page']['description'])) {
46  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('description');
47  $manager->addProperty('description', $params['page']['description']);
48  }
49 
50  if (!empty($params['page']['og_title'])) {
51  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('og:title');
52  $manager->addProperty('og:title', $params['page']['og_title']);
53  }
54 
55  if (!empty($params['page']['og_description'])) {
56  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('og:description');
57  $manager->addProperty('og:description', $params['page']['og_description']);
58  }
59 
60  if (!empty($params['page']['og_image'])) {
61  $fileCollector = GeneralUtility::makeInstance(FileCollector::class);
62  $fileCollector->addFilesFromRelation('pages', 'og_image', $params['page']);
63  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('og:image');
64 
65  $ogImages = $this->‪generateSocialImages($fileCollector->getFiles());
66  foreach ($ogImages as $ogImage) {
67  $subProperties = [];
68  $subProperties['url'] = $ogImage['url'];
69  $subProperties['width'] = $ogImage['width'];
70  $subProperties['height'] = $ogImage['height'];
71 
72  if (!empty($ogImage['alternative'])) {
73  $subProperties['alt'] = $ogImage['alternative'];
74  }
75 
76  $manager->addProperty(
77  'og:image',
78  $ogImage['url'],
79  $subProperties
80  );
81  }
82  }
83 
84  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('twitter:card');
85  $manager->addProperty('twitter:card', $params['page']['twitter_card'] ?: 'summary');
86 
87  if (!empty($params['page']['twitter_title'])) {
88  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('twitter:title');
89  $manager->addProperty('twitter:title', $params['page']['twitter_title']);
90  }
91 
92  if (!empty($params['page']['twitter_description'])) {
93  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('twitter:description');
94  $manager->addProperty('twitter:description', $params['page']['twitter_description']);
95  }
96 
97  if (!empty($params['page']['twitter_image'])) {
98  $fileCollector = GeneralUtility::makeInstance(FileCollector::class);
99  $fileCollector->addFilesFromRelation('pages', 'twitter_image', $params['page']);
100  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('twitter:image');
101 
102  $twitterImages = $this->‪generateSocialImages($fileCollector->getFiles());
103  foreach ($twitterImages as $twitterImage) {
104  $subProperties = [];
105 
106  if (!empty($twitterImage['alternative'])) {
107  $subProperties['alt'] = $twitterImage['alternative'];
108  }
109 
110  $manager->addProperty(
111  'twitter:image',
112  $twitterImage['url'],
113  $subProperties
114  );
115  }
116  }
117 
118  $noIndex = ((bool)$params['page']['no_index']) ? 'noindex' : 'index';
119  $noFollow = ((bool)$params['page']['no_follow']) ? 'nofollow' : 'follow';
120 
121  if ($noIndex === 'noindex' || $noFollow === 'nofollow') {
122  $manager = ‪$metaTagManagerRegistry->getManagerForProperty('robots');
123  $manager->addProperty('robots', implode(',', [$noIndex, $noFollow]));
124  }
125  }
126 
131  protected function ‪generateSocialImages(array $fileReferences): array
132  {
133  $imageService = GeneralUtility::makeInstance(ImageService::class);
134 
135  $socialImages = [];
136 
137  foreach ($fileReferences as $fileReference) {
138  $arguments = $fileReference->getProperties();
139  $image = $this->‪processSocialImage($fileReference);
140  $socialImages[] = [
141  'url' => $imageService->getImageUri($image, true),
142  'width' => floor((float)$image->getProperty('width')),
143  'height' => floor((float)$image->getProperty('height')),
144  'alternative' => $arguments['alternative'],
145  ];
146  }
147 
148  return $socialImages;
149  }
150 
151  protected function ‪processSocialImage(‪FileReference $fileReference): ‪FileInterface
152  {
153  $arguments = $fileReference->‪getProperties();
154  $cropVariantCollection = ‪CropVariantCollection::create((string)($arguments['crop'] ?? ''));
155  $cropVariantName = ($arguments['cropVariant'] ?? false) ?: 'social';
156  $cropArea = $cropVariantCollection->getCropArea($cropVariantName);
157  $crop = $cropArea->makeAbsoluteBasedOnFile($fileReference);
158 
159  $processingConfiguration = [
160  'crop' => $crop,
161  'maxWidth' => 2000,
162  ];
163 
164  // The image needs to be processed if:
165  // - the image width is greater than the defined maximum width, or
166  // - there is a cropping other than the full image (starts at 0,0 and has a width and height of 100%) defined
167  $needsProcessing = $fileReference->‪getProperty('width') > $processingConfiguration['maxWidth']
168  || !$cropArea->isEmpty();
169  if (!$needsProcessing) {
170  return $fileReference->‪getOriginalFile();
171  }
172 
173  return $fileReference->‪getOriginalFile()->‪process(
175  $processingConfiguration
176  );
177  }
178 }
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGECROPSCALEMASK
‪const CONTEXT_IMAGECROPSCALEMASK
Definition: ProcessedFile.php:59
‪TYPO3\CMS\Core\Resource\FileReference\getProperty
‪mixed getProperty($key)
Definition: FileReference.php:110
‪TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry
Definition: MetaTagManagerRegistry.php:28
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Seo\MetaTag\MetaTagGenerator
Definition: MetaTagGenerator.php:35
‪TYPO3\CMS\Seo\MetaTag
Definition: MetaTagGenerator.php:18
‪TYPO3\CMS\Core\Resource\File\process
‪ProcessedFile process($taskType, array $configuration)
Definition: File.php:250
‪TYPO3\CMS\Frontend\Resource\FileCollector
Definition: FileCollector.php:41
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:37
‪TYPO3\CMS\Seo\MetaTag\MetaTagGenerator\generateSocialImages
‪array generateSocialImages(array $fileReferences)
Definition: MetaTagGenerator.php:131
‪TYPO3\CMS\Core\Resource\FileReference\getProperties
‪array getProperties()
Definition: FileReference.php:139
‪TYPO3\CMS\Seo\MetaTag\MetaTagGenerator\processSocialImage
‪processSocialImage(FileReference $fileReference)
Definition: MetaTagGenerator.php:151
‪TYPO3\CMS\Core\Resource\FileReference\getOriginalFile
‪File getOriginalFile()
Definition: FileReference.php:483
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:45
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static CropVariantCollection create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:42
‪TYPO3\CMS\Seo\MetaTag\MetaTagGenerator\generate
‪generate(array $params)
Definition: MetaTagGenerator.php:41
‪$metaTagManagerRegistry
‪$metaTagManagerRegistry
Definition: ext_localconf.php:86