예제 #1
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);
     }
 }