Ejemplo n.º 1
0
 /**
  * This method actually does the processing of files locally
  *
  * takes the original file (on remote storages this will be fetched from the remote server)
  * does the IM magic on the local server by creating a temporary typo3temp/ file
  * copies the typo3temp/ file to the processing folder of the target storage
  * removes the typo3temp/ file
  *
  * @param TaskInterface $task
  * @return array
  */
 public function process(TaskInterface $task)
 {
     $targetFile = $task->getTargetFile();
     $sourceFile = $task->getSourceFile();
     // Merge custom configuration with default configuration
     $configuration = array_merge(array('width' => 64, 'height' => 64), $task->getConfiguration());
     $configuration['width'] = Utility\MathUtility::forceIntegerInRange($configuration['width'], 1);
     $configuration['height'] = Utility\MathUtility::forceIntegerInRange($configuration['height'], 1);
     $originalFileName = $sourceFile->getForLocalProcessing(FALSE);
     // Create a temporaryFile
     $temporaryFileName = Utility\GeneralUtility::tempnam('preview_', '.' . $task->getTargetFileExtension());
     // Check file extension
     if ($sourceFile->getType() != Resource\File::FILETYPE_IMAGE && !Utility\GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $sourceFile->getExtension())) {
         // Create a default image
         $this->processor->getTemporaryImageWithText($temporaryFileName, 'Not imagefile!', 'No ext!', $sourceFile->getName());
     } else {
         // Create the temporary file
         if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) {
             $parameters = '-sample ' . $configuration['width'] . 'x' . $configuration['height'] . ' ' . $this->processor->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->processor->getTemporaryImageWithText($temporaryFileName, 'No thumb', 'generated!', $sourceFile->getName());
             }
         }
     }
     return array('filePath' => $temporaryFileName);
 }
Ejemplo n.º 2
0
 /**
  * This method actually does the processing of files locally
  *
  * takes the original file (on remote storages this will be fetched from the remote server)
  * does the IM magic on the local server by creating a temporary typo3temp/ file
  * copies the typo3temp/ file to the processing folder of the target storage
  * removes the typo3temp/ file
  *
  * The returned array has the following structure:
  *   width => 100
  *   height => 200
  *   filePath => /some/path
  *
  * If filePath isn't set but width and height are the original file is used as ProcessedFile
  * with the returned width and height. This is for example useful for SVG images.
  *
  * @param TaskInterface $task
  * @return array|NULL
  */
 public function process(TaskInterface $task)
 {
     $sourceFile = $task->getSourceFile();
     // Merge custom configuration with default configuration
     $configuration = array_merge(array('width' => 64, 'height' => 64), $task->getConfiguration());
     $configuration['width'] = MathUtility::forceIntegerInRange($configuration['width'], 1);
     $configuration['height'] = MathUtility::forceIntegerInRange($configuration['height'], 1);
     // Do not scale up if the source file has a size and the target size is larger
     if ($sourceFile->getProperty('width') > 0 && $sourceFile->getProperty('height') > 0 && $configuration['width'] > $sourceFile->getProperty('width') && $configuration['height'] > $sourceFile->getProperty('height')) {
         return NULL;
     }
     return $this->generatePreviewFromFile($sourceFile, $configuration, $this->getTemporaryFilePath($task));
 }
 /**
  * @param TaskInterface $task
  * @return array|null
  */
 protected function process(TaskInterface $task)
 {
     $result = NULL;
     $sourceFile = $task->getOriginalProcessedFile() ?: $task->getSourceFile();
     $sourceFilePath = realpath($sourceFile->getForLocalProcessing(FALSE));
     $targetFilePath = realpath(GeneralUtility::tempnam('_processed_/nlx-tempfile-', '.' . $sourceFile->getExtension()));
     switch ($sourceFile->getExtension()) {
         case 'jpg':
         case 'jpeg':
             if ($this->settings['jpg.']['enabled'] === FALSE) {
                 return $result;
             }
             $library = 'jpegtran';
             $arguments = sprintf('-copy none -optimize %s -outfile %s %s', $this->settings['jpg.']['progressive'] === TRUE ? '-progressive' : '', escapeshellarg($targetFilePath), escapeshellarg($sourceFilePath));
             break;
         case 'png':
             if ($this->settings['png.']['enabled'] === FALSE) {
                 return $result;
             }
             $library = 'optipng';
             $arguments = sprintf('-o%u -strip all -fix -clobber -force -out %s %s', $this->settings['png.']['optimizationLevel'], escapeshellarg($targetFilePath), escapeshellarg($sourceFilePath));
             break;
         case 'gif':
             if ($this->settings['gif.']['enabled'] === FALSE) {
                 return $result;
             }
             $library = 'gifsicle';
             $arguments = sprintf('--batch -O%u -o %s %s', $this->settings['gif.']['optimizationLevel'], escapeshellarg($targetFilePath), escapeshellarg($sourceFilePath));
             break;
         case 'svg':
             if ($this->settings['svg.']['enabled'] === FALSE) {
                 return $result;
             }
             $library = 'svgo';
             $arguments = sprintf('%s %s', escapeshellarg($sourceFilePath), escapeshellarg($targetFilePath));
             break;
         default:
             return $result;
     }
     $cmd = escapeshellcmd($library) . ' ' . $arguments;
     $output = [];
     exec($cmd, $output, $return);
     if ($return === 0) {
         $result = array('filePath' => $targetFilePath);
     }
     return $result;
 }
 /**
  * Processing the focus point crop (fallback to LocalCropScaleMaskHelper)
  *
  * @param TaskInterface $task
  *
  * @return array|NULL
  */
 public function process(TaskInterface $task)
 {
     $sourceFile = $task->getSourceFile();
     try {
         $ratio = $this->getCurrentRatioConfiguration();
         $this->dimensionService->getRatio($ratio);
         $newFile = $this->focusCropService->getCroppedImageSrcByFile($sourceFile, $ratio);
         $file = ResourceFactory::getInstance()->retrieveFileOrFolderObject($newFile);
         $targetFile = $task->getTargetFile();
         ObjectAccess::setProperty($targetFile, 'originalFile', $file, TRUE);
         ObjectAccess::setProperty($targetFile, 'originalFileSha1', $file->getSha1(), TRUE);
         ObjectAccess::setProperty($targetFile, 'storage', $file->getStorage(), TRUE);
         ObjectAccess::setProperty($task, 'sourceFile', $file, TRUE);
         ObjectAccess::setProperty($task, 'targetFile', $targetFile, TRUE);
     } catch (\Exception $ex) {
     }
     return parent::process($task);
 }
 /**
  * Returns the filename for a cropped/scaled/masked file.
  *
  * @param TaskInterface $task
  * @return string
  */
 protected function getFilenameForImageCropScaleMask(TaskInterface $task)
 {
     $configuration = $task->getTargetFile()->getProcessingConfiguration();
     $targetFileExtension = $task->getSourceFile()->getExtension();
     $processedFileExtension = $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'] ? 'png' : 'gif';
     if (is_array($configuration['maskImages']) && $GLOBALS['TYPO3_CONF_VARS']['GFX']['im'] && $task->getSourceFile()->getExtension() != $processedFileExtension) {
         $targetFileExtension = 'jpg';
     } elseif ($configuration['fileExtension']) {
         $targetFileExtension = $configuration['fileExtension'];
     }
     return $task->getTargetFile()->generateProcessedFileNameWithoutExtension() . '.' . ltrim(trim($targetFileExtension), '.');
 }
Ejemplo n.º 6
0
 /**
  * Check if the to be processed target file already exists
  * if exist take info from that file and mark task as done
  *
  * @param TaskInterface $task
  * @return bool
  */
 protected function checkForExistingTargetFile(TaskInterface $task)
 {
     // the storage of the processed file, not of the original file!
     $storage = $task->getTargetFile()->getStorage();
     $processingFolder = $storage->getProcessingFolder($task->getSourceFile());
     // explicitly check for the raw filename here, as we check for files that existed before we even started
     // processing, i.e. that were processed earlier
     if ($processingFolder->hasFile($task->getTargetFileName())) {
         // When the processed file already exists set it as processed file
         $task->getTargetFile()->setName($task->getTargetFileName());
         // If the processed file is stored on a remote server, we must fetch a local copy of the file, as we
         // have no API for fetching file metadata from a remote file.
         $localProcessedFile = $storage->getFileForLocalProcessing($task->getTargetFile(), false);
         $task->setExecuted(true);
         $imageDimensions = $this->getGraphicalFunctionsObject()->getImageDimensions($localProcessedFile);
         $properties = ['width' => $imageDimensions[0], 'height' => $imageDimensions[1], 'size' => filesize($localProcessedFile), 'checksum' => $task->getConfigurationChecksum()];
         $task->getTargetFile()->updateProperties($properties);
         return true;
     } else {
         return false;
     }
 }