‪TYPO3CMS  ‪main
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 {
36  public function ‪__construct(
38  protected ‪ImageService $imageService
39  ) {}
40 
44  public function ‪generate(array $params)
45  {
46  if (!empty($params['page']['description'])) {
47  $manager = $this->metaTagManagerRegistry->getManagerForProperty('description');
48  $manager->addProperty('description', $params['page']['description']);
49  }
50 
51  if (!empty($params['page']['og_title'])) {
52  $manager = $this->metaTagManagerRegistry->getManagerForProperty('og:title');
53  $manager->addProperty('og:title', $params['page']['og_title']);
54  }
55 
56  if (!empty($params['page']['og_description'])) {
57  $manager = $this->metaTagManagerRegistry->getManagerForProperty('og:description');
58  $manager->addProperty('og:description', $params['page']['og_description']);
59  }
60 
61  if (!empty($params['page']['og_image'])) {
62  $fileCollector = GeneralUtility::makeInstance(FileCollector::class);
63  $fileCollector->addFilesFromRelation('pages', 'og_image', $params['page']);
64  $manager = $this->metaTagManagerRegistry->getManagerForProperty('og:image');
65 
66  $ogImages = $this->‪generateSocialImages($fileCollector->getFiles());
67  foreach ($ogImages as $ogImage) {
68  $subProperties = [];
69  $subProperties['url'] = $ogImage['url'];
70  $subProperties['width'] = $ogImage['width'];
71  $subProperties['height'] = $ogImage['height'];
72 
73  if (!empty($ogImage['alternative'])) {
74  $subProperties['alt'] = $ogImage['alternative'];
75  }
76 
77  $manager->addProperty(
78  'og:image',
79  $ogImage['url'],
80  $subProperties
81  );
82  }
83  }
84 
85  $manager = $this->metaTagManagerRegistry->getManagerForProperty('twitter:card');
86  $manager->addProperty('twitter:card', $params['page']['twitter_card'] ?: 'summary');
87 
88  if (!empty($params['page']['twitter_title'])) {
89  $manager = $this->metaTagManagerRegistry->getManagerForProperty('twitter:title');
90  $manager->addProperty('twitter:title', $params['page']['twitter_title']);
91  }
92 
93  if (!empty($params['page']['twitter_description'])) {
94  $manager = $this->metaTagManagerRegistry->getManagerForProperty('twitter:description');
95  $manager->addProperty('twitter:description', $params['page']['twitter_description']);
96  }
97 
98  if (!empty($params['page']['twitter_image'])) {
99  $fileCollector = GeneralUtility::makeInstance(FileCollector::class);
100  $fileCollector->addFilesFromRelation('pages', 'twitter_image', $params['page']);
101  $manager = $this->metaTagManagerRegistry->getManagerForProperty('twitter:image');
102 
103  $twitterImages = $this->‪generateSocialImages($fileCollector->getFiles());
104  foreach ($twitterImages as $twitterImage) {
105  $subProperties = [];
106 
107  if (!empty($twitterImage['alternative'])) {
108  $subProperties['alt'] = $twitterImage['alternative'];
109  }
110 
111  $manager->addProperty(
112  'twitter:image',
113  $twitterImage['url'],
114  $subProperties
115  );
116  }
117  }
118 
119  $noIndex = ((bool)$params['page']['no_index']) ? 'noindex' : 'index';
120  $noFollow = ((bool)$params['page']['no_follow']) ? 'nofollow' : 'follow';
121 
122  if ($noIndex === 'noindex' || $noFollow === 'nofollow') {
123  $manager = $this->metaTagManagerRegistry->getManagerForProperty('robots');
124  $manager->addProperty('robots', implode(',', [$noIndex, $noFollow]));
125  }
126  }
127 
131  protected function ‪generateSocialImages(array $fileReferences): array
132  {
133  $socialImages = [];
134 
135  foreach ($fileReferences as $fileReference) {
136  $arguments = $fileReference->getProperties();
137  $image = $this->‪processSocialImage($fileReference);
138  $socialImages[] = [
139  'url' => $this->imageService->getImageUri($image, true),
140  'width' => floor((float)$image->getProperty('width')),
141  'height' => floor((float)$image->getProperty('height')),
142  'alternative' => $arguments['alternative'],
143  ];
144  }
145 
146  return $socialImages;
147  }
148 
149  protected function ‪processSocialImage(‪FileReference $fileReference): ‪FileInterface
150  {
151  $arguments = $fileReference->‪getProperties();
152  $cropVariantCollection = ‪CropVariantCollection::create((string)($arguments['crop'] ?? ''));
153  $cropVariantName = ($arguments['cropVariant'] ?? false) ?: 'social';
154  $cropArea = $cropVariantCollection->getCropArea($cropVariantName);
155  $crop = $cropArea->makeAbsoluteBasedOnFile($fileReference);
156 
157  $processingConfiguration = [
158  'crop' => $crop,
159  'maxWidth' => 2000,
160  ];
161 
162  // The image needs to be processed if:
163  // - the image width is greater than the defined maximum width, or
164  // - there is a cropping other than the full image (starts at 0,0 and has a width and height of 100%) defined
165  $needsProcessing = $fileReference->‪getProperty('width') > $processingConfiguration['maxWidth']
166  || !$cropArea->isEmpty();
167  if (!$needsProcessing) {
168  return $fileReference->getOriginalFile();
169  }
170 
171  return $fileReference->getOriginalFile()->process(
173  $processingConfiguration
174  );
175  }
176 }
‪TYPO3\CMS\Core\Resource\FileReference\getProperty
‪getProperty(string $key)
Definition: FileReference.php:107
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:37
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGECROPSCALEMASK
‪const CONTEXT_IMAGECROPSCALEMASK
Definition: ProcessedFile.php:61
‪TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry
Definition: MetaTagManagerRegistry.php:28
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:36
‪TYPO3\CMS\Seo\MetaTag\MetaTagGenerator
Definition: MetaTagGenerator.php:35
‪TYPO3\CMS\Seo\MetaTag
Definition: MetaTagGenerator.php:18
‪TYPO3\CMS\Frontend\Resource\FileCollector
Definition: FileCollector.php:47
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:37
‪TYPO3\CMS\Core\Resource\FileReference\getProperties
‪array getProperties()
Definition: FileReference.php:136
‪TYPO3\CMS\Seo\MetaTag\MetaTagGenerator\__construct
‪__construct(protected MetaTagManagerRegistry $metaTagManagerRegistry, protected ImageService $imageService)
Definition: MetaTagGenerator.php:36
‪TYPO3\CMS\Seo\MetaTag\MetaTagGenerator\processSocialImage
‪processSocialImage(FileReference $fileReference)
Definition: MetaTagGenerator.php:149
‪TYPO3\CMS\Seo\MetaTag\MetaTagGenerator\generateSocialImages
‪generateSocialImages(array $fileReferences)
Definition: MetaTagGenerator.php:131
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Seo\MetaTag\MetaTagGenerator\generate
‪generate(array $params)
Definition: MetaTagGenerator.php:44
‪$metaTagManagerRegistry
‪$metaTagManagerRegistry
Definition: ext_localconf.php:74