Created
May 14, 2017 10:45
-
-
Save liayn/494a0c32d81808bfc5c9df16aa6561cc to your computer and use it in GitHub Desktop.
Grayscale Image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
/* | |
* | |
* This file is part of the "Skill Display" Extension for TYPO3 CMS. | |
* | |
* For the full copyright and license information, please read the | |
* LICENSE.txt file that was distributed with this source code. | |
* | |
* Copyright (c) Reelworx GmbH | |
* | |
*/ | |
namespace SkillDisplay\Skills\ViewHelpers; | |
use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException; | |
use TYPO3\CMS\Core\Resource\FileInterface; | |
use TYPO3\CMS\Core\Resource\FileReference; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder; | |
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception; | |
use TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper; | |
use TYPO3\CMS\Frontend\Imaging\GifBuilder; | |
class GrayImageViewHelper extends ImageViewHelper | |
{ | |
/** | |
* Resizes a given image (if required) and renders the respective img tag | |
* | |
* @see http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4164427 | |
* | |
* @param string $src a path to a file, a combined FAL identifier or an uid (int). If $treatIdAsReference is set, the integer is considered the uid of the sys_file_reference record. If you already got a FAL object, consider using the $image parameter instead | |
* @param string $width width of the image. This can be a numeric value representing the fixed width of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options. | |
* @param string $height height of the image. This can be a numeric value representing the fixed height of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options. | |
* @param int $minWidth minimum width of the image | |
* @param int $minHeight minimum height of the image | |
* @param int $maxWidth maximum width of the image | |
* @param int $maxHeight maximum height of the image | |
* @param bool $treatIdAsReference given src argument is a sys_file_reference record | |
* @param FileInterface|AbstractFileFolder $image a FAL object | |
* @param string|bool $crop overrule cropping of image (setting to FALSE disables the cropping set in FileReference) | |
* @param bool $absolute Force absolute URL | |
* | |
* @throws Exception | |
* @throws \UnexpectedValueException | |
* @return string Rendered tag | |
*/ | |
public function render($src = null, $width = null, $height = null, $minWidth = null, $minHeight = null, | |
$maxWidth = null, $maxHeight = null, $treatIdAsReference = false, $image = null, $crop = null, $absolute = false | |
) | |
{ | |
if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) { | |
throw new Exception('You must either specify a string src or a File object.', 1382284106); | |
} | |
try { | |
$image = $this->imageService->getImage($src, $image, $treatIdAsReference); | |
if ($crop === null) { | |
$crop = $image instanceof FileReference ? $image->getProperty('crop') : null; | |
} | |
$processingInstructions = [ | |
'width' => $width, | |
'height' => $height, | |
'minWidth' => $minWidth, | |
'minHeight' => $minHeight, | |
'maxWidth' => $maxWidth, | |
'maxHeight' => $maxHeight, | |
'crop' => $crop, | |
]; | |
$processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions); | |
$conf = [ | |
1 => 'IMAGE', | |
'1.' => [ | |
'file' => $processedImage->getForLocalProcessing(false) | |
], | |
20 => 'EFFECT', | |
'20.' => [ | |
'value' => 'gray' | |
] | |
]; | |
$conf['XY'] = '[1.w],[1.h]'; | |
$conf['transparentBackground'] = true; | |
/** @var GifBuilder $gifCreator */ | |
$gifCreator = GeneralUtility::makeInstance(GifBuilder::class); | |
$gifCreator->init(); | |
$gifCreator->start($conf, []); | |
$imageUri = $gifCreator->gifBuild(); | |
$this->tag->addAttribute('src', $imageUri); | |
$this->tag->addAttribute('width', $processedImage->getProperty('width')); | |
$this->tag->addAttribute('height', $processedImage->getProperty('height')); | |
$alt = $image->getProperty('alternative'); | |
$title = $image->getProperty('title'); | |
// The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty | |
if (empty($this->arguments['alt'])) { | |
$this->tag->addAttribute('alt', $alt); | |
} | |
if (empty($this->arguments['title']) && $title) { | |
$this->tag->addAttribute('title', $title); | |
} | |
} catch (ResourceDoesNotExistException $e) { | |
// thrown if file does not exist | |
} catch (\UnexpectedValueException $e) { | |
// thrown if a file has been replaced with a folder | |
} catch (\RuntimeException $e) { | |
// RuntimeException thrown if a file is outside of a storage | |
} catch (\InvalidArgumentException $e) { | |
// thrown if file storage does not exist | |
} | |
return $this->tag->render(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment