/**
  * The actual processing TASK
  *
  * Should return an array with database properties for sys_file_metadata to write
  *
  * @param File $file
  * @param array $previousExtractedData optional, contains the array of already extracted data
  * @return array
  */
 public function extractMetaData(File $file, array $previousExtractedData = [])
 {
     $metadata = [];
     try {
         if (!class_exists('ColorThief\\ColorThief')) {
             throw new \RuntimeException('Class ColorThief\\ColorThief does not exist', 1470749087524);
         }
         $path = $file->getForLocalProcessing();
         $averageColor = ColorThief::getColor($path);
         if (!is_array($averageColor)) {
             throw new \RuntimeException('$averageColor is not an array', 1470749109020);
         }
         if (count($averageColor) !== 3) {
             throw new \RuntimeException('$averageColor is an array, but has less than 3 items', 1470749136303);
         }
         $r = dechex((int) $averageColor[0]);
         $g = dechex((int) $averageColor[1]);
         $b = dechex((int) $averageColor[2]);
         $metadata['average_color'] = '#' . $r . $g . $b;
         $this->logger->debug(sprintf('Extracted average color "%s"', $metadata['average_color']), ['file' => $file->getUid()]);
     } catch (\Exception $e) {
         $this->logger->error($e->getCode() . ': ' . $e->getMessage(), ['file' => $file->getUid()]);
     }
     return $metadata;
 }
Esempio n. 2
0
 /**
  * Convert PNG image using pngquant command.
  *
  * @param AbstractFile $file
  */
 public function convertPngImage(AbstractFile $file)
 {
     try {
         if (self::EXTENSION_PNG !== $file->getExtension()) {
             return;
         }
         // Ignore processed file which uses original file
         if ($file instanceof ProcessedFile && $file->usesOriginalFile()) {
             $this->logger->debug('Do not convert processed file identical with its original file', array('file' => $inputFilePath));
             return;
         }
         // Set input/output files for pngquant command
         // Input file is the the specified file we want to quantize
         // Output file is a temporary file in typo3temp directory
         $inputFilePath = PATH_site . $file->getPublicUrl();
         $outputFilePath = GeneralUtility::tempnam('sg_pngquant_', '.' . self::EXTENSION_PNG);
         // Build command line
         $cmd = $this->buildCommand($inputFilePath, $outputFilePath);
         $result = CommandUtility::exec($cmd, $output, $returnValue);
         if (0 === $returnValue) {
             // Replace content
             if ($file instanceof ProcessedFile) {
                 // For processed file, only convert real processed file (i.e. different than their original file)
                 // Temporary file is automatically removed when updating a processed file
                 $this->logger->debug('Update processed file', array('cmd' => $cmd));
                 $file->updateWithLocalFile($outputFilePath);
             } elseif (!$this->confArray['keepOriginal']) {
                 // Convert original files according to extension configuration
                 // After conversion the temporary file is removed
                 $this->logger->debug('Update original file', array('cmd' => $cmd));
                 $contents = @file_get_contents($outputFilePath);
                 $file->setContents($contents);
             }
         } else {
             $this->logger->error('Convert image', array('cmd' => $cmd, 'result' => $result, 'output' => $output, 'returnValue' => $returnValue));
         }
     } catch (\RuntimeException $e) {
         $this->logger->error($e->getMessage());
     }
     // Remove temporary file, if exists
     if (file_exists($outputFilePath)) {
         $this->removeTemporaryFile($outputFilePath);
     }
 }