예제 #1
0
파일: files.php 프로젝트: PseudoAj/mfcs
 public static function processObjectFiles($assetsID, $options)
 {
     // Disable PHP's max execution time
     set_time_limit(0);
     $saveBase = mfcs::config('convertedPath');
     $originalsFilepath = self::getSaveDir($assetsID, 'archive');
     $originalFiles = scandir($originalsFilepath);
     // Setup return array
     $return = array('processed' => array(), 'combine' => array(), 'thumbs' => array(), 'ocr' => array());
     // Remove dot files from array
     foreach ($originalFiles as $I => $filename) {
         if ($filename[0] == '.') {
             unset($originalFiles[$I]);
         }
     }
     // Needed to put the files in the right order for processing
     if (natcasesort($originalFiles) === FALSE) {
         return FALSE;
     }
     try {
         // If combine files is checked, read this image and add it to the combined object
         if (isset($options['combine']) && str2bool($options['combine'])) {
             try {
                 $errors = array();
                 $createThumb = TRUE;
                 // Create us some temp working space
                 $tmpDir = mfcs::config('mfcstmp') . DIRECTORY_SEPARATOR . uniqid();
                 mkdir($tmpDir, 0777, TRUE);
                 // Ensure that the HOCR file is created
                 if (!self::createHOCR("{$saveBase}/hocr.cfg")) {
                     return FALSE;
                 }
                 $gsTemp = $tmpDir . DIRECTORY_SEPARATOR . uniqid();
                 touch($gsTemp);
                 foreach ($originalFiles as $filename) {
                     // Figure some stuff out about the file
                     $originalFile = $originalsFilepath . DIRECTORY_SEPARATOR . $filename;
                     $_filename = pathinfo($originalFile);
                     $filename = $_filename['filename'];
                     $baseFilename = $tmpDir . DIRECTORY_SEPARATOR . $filename;
                     // Create a thumbnail of the first image
                     if ($createThumb === TRUE) {
                         if (($return['combine'][] = self::createThumbnail($originalFile, $filename, $options, $assetsID, TRUE)) === FALSE) {
                             throw new Exception("Failed to create thumbnail: " . $filename);
                         }
                         // Prevent making multiple thumbnails
                         $createThumb = FALSE;
                     }
                     // perform hOCR on the original uploaded file which gets stored in combined as an HTML file
                     $_exec = shell_exec(sprintf('tesseract %s %s -l eng %s 2>&1', escapeshellarg($originalFile), escapeshellarg($baseFilename), escapeshellarg("{$saveBase}/hocr.cfg")));
                     // If a new-line char is in the output, assume it's an error
                     // Tesseract failed, let's normalize the image and try again
                     if (strpos(trim($_exec), "\n") !== FALSE) {
                         $errors[] = "Unable to process OCR for " . basename($originalFile) . ". Continuing…";
                         errorHandle::warningMsg("Unable to process OCR for " . basename($originalFile) . ". Continuing…");
                         // Ensure HTML file exists
                         touch($baseFilename . ".html");
                     }
                     // Create an OCR'd pdf of the file
                     $_exec = shell_exec(sprintf('hocr2pdf -i %s -s -o %s < %s 2>&1', escapeshellarg($originalFile), escapeshellarg($baseFilename . ".pdf"), escapeshellarg($baseFilename . ".html")));
                     if (trim($_exec) !== 'Writing unmodified DCT buffer.') {
                         if (strpos($_exec, 'Warning:') !== FALSE) {
                             errorHandle::newError("hocr2pdf Warning: " . $_exec, errorHandle::DEBUG);
                         } else {
                             errorHandle::errorMsg("Failed to Create PDF: " . basename($filename, "jpg") . ".pdf");
                             throw new Exception("hocr2pdf Error: " . $_exec);
                         }
                     }
                     // Add this pdf to a temp file that will be read in by gs
                     file_put_contents($gsTemp, $baseFilename . ".pdf" . PHP_EOL, FILE_APPEND);
                     // We're done with this file, delete it
                     unlink($baseFilename . ".html");
                 }
                 // Combine all PDF files in directory
                 $_exec = shell_exec(sprintf('gs -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=%s @%s 2>&1', self::getSaveDir($assetsID, 'combine') . "combined.pdf", $gsTemp));
                 if (!is_empty($_exec)) {
                     errorHandle::errorMsg("Failed to combine PDFs into single PDF.");
                     throw new Exception("GhostScript Error: " . $_exec);
                 }
                 $return['combine'][] = array('name' => 'combined.pdf', 'path' => self::getSaveDir($assetsID, 'combine', FALSE), 'size' => filesize(self::getSaveDir($assetsID, 'combine') . 'combined.pdf'), 'type' => 'application/pdf', 'errors' => $errors);
                 // Lastly, we delete our temp working dir (always nice to cleanup after yourself)
                 if (self::cleanupTempDirectory($tmpDir) === FALSE) {
                     errorHandle::errorMsg("Unable to clean up temporary directory: " . $tmpDir);
                     throw new Exception("Unable to clean up temporary directory: " . $tmpDir);
                 }
             } catch (Exception $e) {
                 // We need to delete our working dir
                 if (isset($tmpDir) && is_dir($tmpDir)) {
                     if (self::cleanupTempDirectory($tmpDir) === FALSE) {
                         errorHandle::errorMsg("Unable to clean up temporary directory (in Exception): " . $tmpDir);
                     }
                 }
                 throw new Exception($e->getMessage(), $e->getCode(), $e);
             }
         }
         // If Combine
         // This conditional needs updated when different conversion options are added or removed.
         // If the file has no processing to do, don't do any ...
         if (!isset($options['convert']) && !isset($options['thumbnail']) && !isset($options['ocr']) && !isset($options['mp3'])) {
             return $return;
         }
         foreach ($originalFiles as $filename) {
             $originalFile = $originalsFilepath . DIRECTORY_SEPARATOR . $filename;
             $_filename = pathinfo($originalFile);
             $filename = $_filename['filename'];
             // Convert uploaded files into some ofhter size/format/etc
             if (isset($options['convert']) && str2bool($options['convert'])) {
                 // we create the Imagick object here so that we can pass it to thumbnail creation
                 $image = new Imagick();
                 $image->readImage($originalFile);
                 // Convert it
                 if (($image = self::convertImage($image, $options, $assetsID, $filename)) === FALSE) {
                     throw new Exception("Failed to create processed image: " . $originalFile);
                 }
                 $filename = $filename . '.' . strtolower($image->getImageFormat());
                 // Create a thumbnail that includes converted options
                 if (isset($options['thumbnail']) && str2bool($options['thumbnail'])) {
                     if (($return['thumbs'][] = self::createThumbnail($image, $filename, $options, $assetsID)) === FALSE) {
                         throw new Exception("Failed to create thumbnail: " . $filename);
                     }
                 }
                 // Set the return array
                 $return['processed'][] = array('name' => $filename, 'path' => self::getSaveDir($assetsID, 'processed', FALSE), 'size' => filesize(self::getSaveDir($assetsID, 'processed') . $filename), 'type' => self::getMimeType(self::getSaveDir($assetsID, 'processed') . $filename), 'errors' => '');
             } else {
                 if (isset($options['thumbnail']) && str2bool($options['thumbnail'])) {
                     if (($return['thumbs'][] = self::createThumbnail($originalFile, $filename, $options, $assetsID)) === FALSE) {
                         throw new Exception("Failed to create thumbnail: " . $filename);
                     }
                 }
             }
             // Create an OCR text file
             if (isset($options['ocr']) && str2bool($options['ocr'])) {
                 if (($return['ocr'][] = self::createOCRTextFile($originalFile, $assetsID, $filename)) === FALSE) {
                     errorHandle::errorMsg("Failed to create OCR text file: " . $filename);
                     throw new Exception("Failed to create OCR file for {$filename}");
                 }
             }
             // Create MP3
             if (isset($options['mp3']) && str2bool($options['mp3'])) {
                 $return['mp3'][] = self::createMP3($originalFile);
             }
         }
         // Foreach File
     } catch (Exception $e) {
         errorHandle::newError(__METHOD__ . "() - {$e->getMessage()} {$e->getLine()}:{$e->getFile()}", errorHandle::HIGH);
     }
     return $return;
 }