Implement default image for og:image meta tag

This commit is contained in:
Björn Fromme
2019-10-10 14:19:42 +02:00
parent 8209d07284
commit b06aa109d5
4 changed files with 54 additions and 0 deletions
@@ -0,0 +1,45 @@
<?php
namespace EP\EpTheme\MetaTag;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Service\ImageService;
class OpenGraphMetaTagManager extends \TYPO3\CMS\Seo\MetaTag\OpenGraphMetaTagManager
{
public function renderAllProperties(): string
{
if (!array_key_exists('og:image', $this->properties)) {
/** @var ConfigurationManager $configurationManager */
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
$settings = GeneralUtility::removeDotsFromTS(
$configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT)
);
$imageSource = $settings['plugin']['tx_eptheme']['settings']['ogDefaultImage'];
$resourceFactory = ResourceFactory::getInstance();
$image = $resourceFactory->getFileObjectFromCombinedIdentifier(sprintf('1:%s', $imageSource));
if (null !== $image) {
/** @var ImageService $imageService */
$imageService = GeneralUtility::makeInstance(ImageService::class);
$processedImage = $imageService->applyProcessingInstructions($image, ['width' => '1200']);
$this->addProperty(
'og:image',
$imageService->getImageUri($processedImage, true),
[
'url' => $imageService->getImageUri($processedImage, true),
'width' => $processedImage->getProperty('width'),
'height' => $processedImage->getProperty('height'),
'type' => $processedImage->getMimeType(),
]
);
}
}
return parent::renderAllProperties();
}
}