|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace SMS\FluidComponents\Service; |
| 4 | + |
| 5 | +use TYPO3\CMS\Core\Information\Typo3Version; |
| 6 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 7 | +use TYPO3\CMS\Core\View\ViewFactoryData; |
| 8 | +use TYPO3\CMS\Core\View\ViewFactoryInterface; |
| 9 | +use TYPO3\CMS\Fluid\View\StandaloneView; |
| 10 | +use TYPO3\CMS\Frontend\Imaging\GifBuilder; |
| 11 | + |
| 12 | +class PlaceholderImageService |
| 13 | +{ |
| 14 | + const int STROKE_WIDTH = 18; |
| 15 | + const int STROKE_HEIGHT = 3; |
| 16 | + const string BACKGROUND_COLOR = '#C0C0C0'; |
| 17 | + const string COLOR = '#0B0B13'; |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + private readonly GifBuilder $gifBuilder, |
| 21 | + private readonly ?ViewFactoryInterface $viewFactory = null, |
| 22 | + ) { |
| 23 | + } |
| 24 | + |
| 25 | + public function generate(int $width, int $height, string $format): string |
| 26 | + { |
| 27 | + $text = sprintf('%dx%d.%s', $width, $height, $format); |
| 28 | + if ($format !== 'svg') { |
| 29 | + return $this->generateBitmap($width, $height, $format, $text); |
| 30 | + } |
| 31 | + |
| 32 | + if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) { |
| 33 | + $view = new StandaloneView(); |
| 34 | + $view->setTemplatePathAndFilename('EXT:fluid_components/Resources/Private/Templates/Placeholder.svg'); |
| 35 | + } else { |
| 36 | + $view = $this->viewFactory->create(new ViewFactoryData( |
| 37 | + templatePathAndFilename: 'EXT:fluid_components/Resources/Private/Templates/Placeholder.svg', |
| 38 | + )); |
| 39 | + } |
| 40 | + |
| 41 | + $view->assignMultiple([ |
| 42 | + 'width' => $width, |
| 43 | + 'height' => $height, |
| 44 | + 'backgroundColor' => self::BACKGROUND_COLOR, |
| 45 | + 'color' => self::COLOR, |
| 46 | + 'text' => $text, |
| 47 | + ]); |
| 48 | + return 'data:image/svg+xml;base64,' . base64_encode($view->render()); |
| 49 | + } |
| 50 | + |
| 51 | + private function generateBitmap(int $width, int $height, string $format, string $text): string |
| 52 | + { |
| 53 | + $configuration = [ |
| 54 | + 'XY' => implode(',', [$width, $height]), |
| 55 | + 'backColor' => self::BACKGROUND_COLOR, |
| 56 | + 'format' => $format, |
| 57 | + '10' => 'TEXT', |
| 58 | + '10.' => [ |
| 59 | + 'text' => $text, |
| 60 | + 'fontColor' => self::COLOR, |
| 61 | + 'fontSize' => round($width / 9), |
| 62 | + 'align' => 'center', |
| 63 | + 'offset' => implode(',', [0, $height / 1.75]), |
| 64 | + ], |
| 65 | + ]; |
| 66 | + |
| 67 | + $strokes = [ |
| 68 | + [0 ,0 , self::STROKE_WIDTH, self::STROKE_HEIGHT], |
| 69 | + [0 ,0 , self::STROKE_HEIGHT, self::STROKE_WIDTH], |
| 70 | + [($width - self::STROKE_WIDTH), 0, $width , self::STROKE_HEIGHT], |
| 71 | + [($width - self::STROKE_HEIGHT), 0, $width, self::STROKE_WIDTH], |
| 72 | + [0, ($height - self::STROKE_HEIGHT), self::STROKE_WIDTH, $height], |
| 73 | + [0, ($height - self::STROKE_WIDTH), self::STROKE_HEIGHT, $height], |
| 74 | + [($width - self::STROKE_WIDTH), ($height - self::STROKE_HEIGHT), $width, $height], |
| 75 | + [($width - self::STROKE_HEIGHT), ($height - self::STROKE_WIDTH), $width, $height], |
| 76 | + ]; |
| 77 | + |
| 78 | + foreach ($strokes as $key => $dimensions) { |
| 79 | + $configuration[10 * ($key + 2)] = 'BOX'; |
| 80 | + $configuration[10 * ($key + 2) . '.'] = [ |
| 81 | + 'dimensions' => implode(',', $dimensions), |
| 82 | + 'color' => self::COLOR, |
| 83 | + ]; |
| 84 | + } |
| 85 | + |
| 86 | + $this->gifBuilder->start($configuration, []); |
| 87 | + |
| 88 | + if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13 && is_string($imagePath = $this->gifBuilder->gifBuild())) { |
| 89 | + return $imagePath; |
| 90 | + } |
| 91 | + return (string) $this->gifBuilder->gifBuild()->getPublicUrl(); |
| 92 | + } |
| 93 | +} |
0 commit comments