Created
July 11, 2014 08:56
-
-
Save helhum/e4a6cbdfd18c4f2f59f7 to your computer and use it in GitHub Desktop.
FAL Processing
This file contains 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 | |
namespace Helhum\ProcessingServices\Resource\Processing; | |
use TYPO3\CMS\Core\Utility; | |
class YoutubeProcessing { | |
/** | |
* @var \TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor | |
*/ | |
protected $processor; | |
/** | |
* @param \TYPO3\CMS\Core\Resource\ProcessedFile $processedFile | |
* @return bool | |
*/ | |
protected function needsReprocessing($processedFile) { | |
return ($processedFile->isNew() || (!$processedFile->usesOriginalFile() && !$processedFile->exists()) || | |
$processedFile->isOutdated()); | |
} | |
public function processFile(\TYPO3\CMS\Core\Resource\Service\FileProcessingService $fileProcessingService, \TYPO3\CMS\Core\Resource\Driver\AbstractDriver $driver, \TYPO3\CMS\Core\Resource\ProcessedFile $processedFile, \TYPO3\CMS\Core\Resource\File $file, $taskType, array $configuration) { | |
if ($file->getExtension() === 'ytb' && $this->needsReprocessing($processedFile)) { | |
if ($taskType === 'Image.Preview') { | |
// Merge custom configuration with default configuration | |
$configuration = array_merge(array('width' => 64, 'height' => 64), $configuration); | |
$configuration['width'] = Utility\MathUtility::forceIntegerInRange($configuration['width'], 1, 1000); | |
$configuration['height'] = Utility\MathUtility::forceIntegerInRange($configuration['height'], 1, 1000); | |
// By definition these files contain the video ID | |
$videoId = trim($file->getContents()); | |
$temporaryFileName = PATH_site . $this->getPreviewImageFromYoutube($videoId); | |
$temporaryFileNameForResizedThumb = uniqid(PATH_site . 'typo3temp/youtube_' . $videoId) . '.jpg'; | |
$this->resizeImage($temporaryFileName, $temporaryFileNameForResizedThumb, $configuration); | |
$processedFile->setName($this->getTargetFileName($processedFile)); | |
$processedFile->updateProperties( | |
array('width' => $configuration['width'], 'height' => $configuration['height'], 'size' => filesize($temporaryFileNameForResizedThumb), 'checksum' => $processedFile->getTask()->getConfigurationChecksum()) | |
); | |
$processedFile->updateWithLocalFile($temporaryFileNameForResizedThumb); | |
/** @var $processedFileRepository \TYPO3\CMS\Core\Resource\ProcessedFileRepository */ | |
$processedFileRepository = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ProcessedFileRepository'); | |
$processedFileRepository->add($processedFile); | |
} elseif ($taskType === 'Image.CropScaleMask') { | |
// By definition these files contain the video ID | |
$videoId = trim($file->getContents()); | |
$temporaryFileName = PATH_site . $this->getPreviewImageFromYoutube($videoId); | |
$result = $this->cropscaleImage($temporaryFileName, $configuration); | |
$processedFile->setName($this->getTargetFileName($processedFile, 'csm_')); | |
$processedFile->updateProperties( | |
array('width' => $result['width'], 'height' => $result['height'], 'size' => $result['filePath'], 'checksum' => $processedFile->getTask()->getConfigurationChecksum()) | |
); | |
$processedFile->updateWithLocalFile($result['filePath']); | |
/** @var $processedFileRepository \TYPO3\CMS\Core\Resource\ProcessedFileRepository */ | |
$processedFileRepository = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ProcessedFileRepository'); | |
$processedFileRepository->add($processedFile); | |
} | |
} | |
} | |
protected function getPreviewImageFromYoutube($videoId) { | |
$temporaryFileName = 'typo3temp/youtube_' . md5($videoId) . '.jpg'; | |
if (!file_exists(PATH_site . $temporaryFileName)) { | |
$previewImage = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl( | |
sprintf('http://img.youtube.com/vi/%s/0.jpg', $videoId) | |
); | |
if ($previewImage !== FALSE) { | |
file_put_contents(PATH_site . $temporaryFileName, $previewImage); | |
} | |
} | |
return $temporaryFileName; | |
} | |
/** | |
* @param \TYPO3\CMS\Core\Resource\ProcessedFile $processedFile | |
* @return string | |
*/ | |
protected function getTargetFileName(\TYPO3\CMS\Core\Resource\ProcessedFile $processedFile, $prefix='preview_') { | |
return $prefix . $processedFile->getTask()->getConfigurationChecksum() . '_' . $processedFile->getOriginalFile()->getNameWithoutExtension() . '.jpg'; | |
} | |
/** | |
* @param string $originalFileName | |
* @param string $temporaryFileName | |
* @param array $configuration | |
*/ | |
protected function resizeImage($originalFileName, $temporaryFileName, $configuration) { | |
// Create the temporary file | |
if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) { | |
$parameters = '-sample ' . $configuration['width'] . 'x' . $configuration['height'] . ' ' | |
. $this->getProcessor()->wrapFileName($originalFileName) . '[0] ' . $this->processor->wrapFileName($temporaryFileName); | |
$cmd = Utility\GeneralUtility::imageMagickCommand('convert', $parameters) . ' 2>&1'; | |
Utility\CommandUtility::exec($cmd); | |
if (!file_exists($temporaryFileName)) { | |
// Create a error gif | |
$this->getProcessor()->getTemporaryImageWithText($temporaryFileName, 'No thumb', 'generated!', basename($originalFileName)); | |
} | |
} | |
} | |
protected function cropscaleImage($originalFileName, $configuration) { | |
/** @var $gifBuilder \TYPO3\CMS\Frontend\Imaging\GifBuilder */ | |
$gifBuilder = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Imaging\\GifBuilder'); | |
$gifBuilder->init(); | |
$options = $this->getConfigurationForImageCropScaleMask($configuration, $gifBuilder); | |
$result = NULL; | |
// Normal situation (no masking) | |
if (!(is_array($configuration['maskImages']) && $GLOBALS['TYPO3_CONF_VARS']['GFX']['im'])) { | |
// the result info is an array with 0=width,1=height,2=extension,3=filename | |
$result = $gifBuilder->imageMagickConvert( | |
$originalFileName, | |
$configuration['fileExtension'], | |
$configuration['width'], | |
$configuration['height'], | |
$configuration['additionalParameters'], | |
$configuration['frame'], | |
$options | |
); | |
} else { | |
} | |
// check if the processing really generated a new file | |
if ($result !== NULL) { | |
if ($result[3] !== $originalFileName) { | |
$result = array( | |
'width' => $result[0], | |
'height' => $result[1], | |
'filePath' => $result[3], | |
); | |
} else { | |
// No file was generated | |
$result = NULL; | |
} | |
} | |
return $result; | |
} | |
/** | |
* @return \TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor | |
*/ | |
protected function getProcessor() { | |
if (!$this->processor) { | |
$this->processor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Processing\\LocalImageProcessor'); | |
} | |
return $this->processor; | |
} | |
/** | |
* @param array $configuration | |
* @param \TYPO3\CMS\Frontend\Imaging\GifBuilder $gifBuilder | |
* | |
* @return array | |
*/ | |
protected function getConfigurationForImageCropScaleMask($configuration, \TYPO3\CMS\Frontend\Imaging\GifBuilder $gifBuilder) { | |
if ($configuration['useSample']) { | |
$gifBuilder->scalecmd = '-sample'; | |
} | |
$options = array(); | |
if ($configuration['maxWidth']) { | |
$options['maxW'] = $configuration['maxWidth']; | |
} | |
if ($configuration['maxHeight']) { | |
$options['maxH'] = $configuration['maxHeight']; | |
} | |
if ($configuration['minWidth']) { | |
$options['minW'] = $configuration['minWidth']; | |
} | |
if ($configuration['minHeight']) { | |
$options['minH'] = $configuration['minHeight']; | |
} | |
$options['noScale'] = $configuration['noScale']; | |
return $options; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can i use such a processor for manipulation in the TYPO3 backend? I like every image that needs to be processed in a way run through specific tasks. Can you may be provide a short snippet? Thank you.