示例#1
0
 public static function convertPDFToPNG(erLhcoreClassModelDocShare &$fileObject)
 {
     $pdfFile = $fileObject->pdf_file_path_server;
     if ($fileObject->pdf_file != '' && file_exists($pdfFile)) {
         try {
             $config = erConfigClassLhConfig::getInstance();
             $objectData = erLhcoreClassModelChatConfig::fetch('doc_sharer');
             $dataDocSharer = (array) $objectData->data;
             erLhcoreClassFileUpload::mkdirRecursive($fileObject->pdftoimg_path, true, $dataDocSharer['http_user_name'], $dataDocSharer['http_user_group_name']);
             $ocrParsed = true;
             // Prepare to run ImageMagick command
             $descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
             $appendCommand = '';
             if ($dataDocSharer['pdftoppm_limit'] > 0) {
                 $appendCommand .= ' -l ' . $dataDocSharer['pdftoppm_limit'];
             }
             $command = $dataDocSharer['pdftoppm_path'] . $appendCommand . ' -png -r 200 ' . escapeshellarg($pdfFile) . ' ' . $fileObject->pdftoimg_path . $fileObject->id;
             // Open color_indexer process
             $imageProcess = proc_open($command, $descriptors, $pipes);
             // Close STDIN pipe
             fclose($pipes[0]);
             $errorString = '';
             $outputString = '';
             // Read STDERR
             do {
                 $errorString .= rtrim(fgets($pipes[2], 1024), "\n");
             } while (!feof($pipes[2]));
             // Wait for process to terminate and store return value
             $status = proc_get_status($imageProcess);
             while ($status['running'] !== false) {
                 // Sleep 1/100 second to wait for convert to exit
                 usleep(10000);
                 $status = proc_get_status($imageProcess);
             }
             $return = proc_close($imageProcess);
             $data = ezcBaseFile::findRecursive($fileObject->pdftoimg_path, array("@{$fileObject->id}-.*\\.png@"), array());
             $contentPages = array();
             $pagesCount = 0;
             foreach ($data as $key => $file) {
                 $pagesCount++;
                 $parts = explode('-', $file);
                 array_pop($parts);
                 $parts[] = '-' . $pagesCount . '.png';
                 $newName = implode('', $parts);
                 rename($file, $newName);
                 $file = $newName;
                 chown($file, $dataDocSharer['http_user_name']);
                 chgrp($file, $dataDocSharer['http_user_group_name']);
                 chmod($file, 0664);
             }
             $fileObject->pdf_to_img_converted = 1;
             $fileObject->pages_pdf_count = $pagesCount;
             $fileObject->saveThis();
         } catch (Exception $e) {
             throw $e;
         }
     }
 }