From 27abc7e52caf6ac9dd443deccf45bfc8dbf08609 Mon Sep 17 00:00:00 2001 From: "vincent@cubedesigners.com" Date: Tue, 2 Mar 2010 12:46:11 +0000 Subject: [PATCH] --- inc/ws/Metier/_common.php | 1 + inc/ws/Metier/class.ws.box.php | 22 ++++ inc/ws/Metier/class.ws.document.php | 197 +++++++++++++++++++++++----- 3 files changed, 187 insertions(+), 33 deletions(-) create mode 100644 inc/ws/Metier/class.ws.box.php diff --git a/inc/ws/Metier/_common.php b/inc/ws/Metier/_common.php index f9f0fda47..d146463aa 100644 --- a/inc/ws/Metier/_common.php +++ b/inc/ws/Metier/_common.php @@ -7,5 +7,6 @@ $__autoload['wsLangueTextes'] = dirname(__FILE__) . '/class.ws.langue.textes.php $__autoload['wsDocument'] = dirname(__FILE__) . '/class.ws.document.php'; $__autoload['wsLink'] = dirname(__FILE__) . '/class.ws.link.php'; $__autoload['wsDocumentLocalInfos'] = dirname(__FILE__) . '/class.ws.document.localinfos.php'; +$__autoload['wsBox'] = dirname(__FILE__) . '/class.ws.box.php'; ?> \ No newline at end of file diff --git a/inc/ws/Metier/class.ws.box.php b/inc/ws/Metier/class.ws.box.php new file mode 100644 index 000000000..4ba23a8cb --- /dev/null +++ b/inc/ws/Metier/class.ws.box.php @@ -0,0 +1,22 @@ +top = $top; + $this->left = $left; + $this->right = $right; + $this->bottom = $bottom; + + $this->width = $this->right - $this->left; + $this->height = $this->bottom - $this->top; + } +} + +?> \ No newline at end of file diff --git a/inc/ws/Metier/class.ws.document.php b/inc/ws/Metier/class.ws.document.php index 091dd932d..72fad2527 100644 --- a/inc/ws/Metier/class.ws.document.php +++ b/inc/ws/Metier/class.ws.document.php @@ -20,6 +20,7 @@ class wsDocument extends cubeMetier { protected $bookmarks; protected $numberSections; protected $links; + protected $crop; protected $out; protected $in; @@ -27,6 +28,8 @@ class wsDocument extends cubeMetier { protected $log; protected $log_pointer; protected $infos; + protected $cropped; + protected $uncropped; private $_basicInfos = null; private $_advancedInfos = null; @@ -50,22 +53,15 @@ class wsDocument extends cubeMetier { if (!file_exists($this->out)) { mkdir($this->out, 0755, true); } + $this->cropped = $this->out . 'crop.pdf'; + $this->uncropped = $this->out . 'uncrop.pdf'; $this->log_pointer = fopen($this->log, 'a'); } public function copyOriginal($tmp_file) { move_uploaded_file($tmp_file, $this->in); - //$this->uncompress(); - } - - public function uncompress() - { - $pdftk = new cubeCommandLine('pdftk'); - $pdftk->setPath(CONVERTER_PATH); - $pdftk->setManualArg($this->in . ' output ' . $this->uncompressed . ' uncompress '); - $pdftk->execute(); - $this->addToLog($pdftk); + $this->uncropDocument(); } public function getInfos() @@ -76,6 +72,81 @@ class wsDocument extends cubeMetier { $this->parseInfos($pdfinfo . $pdftk); file_put_contents($this->infos, $pdfinfo . $pdftk); + $this->crop = $this->findCutDisposition(); + } + + public function findCutDisposition() + { + $spreads = $this->detectSpreads(); + if ($spreads) { + return $spreads; + } + return $this->detectPageDifferences(); + } + + protected function detectPageDifferences() + { + // Vérifie si la cropbox et la trimbox sont identiques pour toutes les pages + $difference = false; + foreach($this->generalInfos['page'] as $page => $infos) { + if ($infos['crop'] != $infos['trim']) { + $difference = true; + } + } + if (!$difference) { + return false; + } + // Vérifie si la trimbox définie toutes les pages de la même taille + $heights = array(); + $widths = array(); + foreach($this->generalInfos['page'] as $page => $infos) { + $heights[] = round($infos['trim']->height); + $widths[] = round($infos['trim']->width); + } + $heights = array_unique($heights); + $widths = array_unique($widths); + if (count($heights) == 1 && count($widths) == 1) { + return 'TTRIM'; + } else { + return 'TMANUEL'; + } + } + + protected function detectSpreads() + { + $spread = false; + // Détection des spreads + foreach($this->generalInfos['page'] as $page => $infos) { + if ($page == 1) { + $first = $infos['size']; + } elseif ($page == $this->generalInfos['pages']) { + $last = $infos['size']; + } elseif ($page == 2) { + $second = $infos['size']; + } + } + + if ($first == $last && $last == $second) { + $ratio = $first[0] / $first[1]; + if ($ratio <= 1) { + return false; + } elseif ($ratio >= 6) { + return 'CSL8'; + } elseif ($ratio >= 3) { + return 'CSL4'; + } elseif ($ratio >= 2) { + return 'CSL3'; + } else { + return 'CS-14-23'; + } + } + + if ($first == $last && round($first[0] * 2) == round($second[0])) { + return 'C1-23-4'; + } + if (round($first[0] * 2) == round($second[0]) && $last == $second) { + return 'C1-23'; + } } public function getBasicInfos() @@ -132,7 +203,7 @@ class wsDocument extends cubeMetier { if ($k == 'Pages' || $k == 'NumberOfPages') { $this->pages = $this->generalInfos['pages'] = $v; } elseif (preg_match('|Page ([0-9]+) (.*)Box: ([0-9.]*) ([0-9.]*) ([0-9.]*) ([0-9.]*)|iu', $line, $m)) { - $this->generalInfos['page'][$m[1]][strtolower($m[2])] = array($m[3], $m[4], $m[5], $m[6]); + $this->generalInfos['page'][$m[1]][strtolower($m[2])] = new wsBox($m[3], $m[4], $m[5], $m[6]); } elseif (preg_match('|Page ([0-9]+) size: ([0-9.]*)([\sx]+)([0-9.]*)(.*)|iu', $line, $m)) { $this->generalInfos['page'][$m[1]]['size'] = array($m[2], $m[4]); $this->generalInfos['size'][0] = max($this->generalInfos['size'][0], $m[2]); @@ -155,6 +226,7 @@ class wsDocument extends cubeMetier { $this->numberSections[] = $section; } } + return $res; } @@ -166,15 +238,48 @@ class wsDocument extends cubeMetier { public function globalOperations() { + $this->getInfos(); + $this->Crop(); $this->getLinks(); $this->getTexts(); } + public function Crop() + { + if ($this->crop == 'TTRIM') { + $this->trimDocument(); + } else { + copy($this->in, $this->cropped); + } + } + + public function trimDocument() + { + $fwstk = new cubeCommandLine('fwstk'); + $fwstk->setPath(CONVERTER_PATH); + $fwstk->setArg('--input ' . $this->in); + $fwstk->setArg('--trim'); + $fwstk->setArg('--output ' . $this->cropped); + $fwstk->execute(); + $this->addToLog($fwstk); + } + + public function uncropDocument() + { + $fwstk = new cubeCommandLine('fwstk'); + $fwstk->setPath(CONVERTER_PATH); + $fwstk->setArg('--input ' . $this->in); + $fwstk->setArg('--reset'); + $fwstk->setArg('--output ' . $this->uncropped); + $fwstk->execute(); + $this->addToLog($fwstk); + } + public function processOnePage($page) { $this->makeMiniShot($page); $this->makeRealShot($page); - $this->makeSWFFiles($page); + $this->makeSWFFiles($page, 150, 90, true, 1800, 1); $this->makeAS3($page); } @@ -200,15 +305,19 @@ class wsDocument extends cubeMetier { $pdftotext->setArg('q'); $pdftotext->setArg('-eol', 'unix'); $pdftotext->setArg('-enc', 'UTF-8'); - $pdftotext->setArg(null, $this->in); + $pdftotext->setArg(null, $this->cropped); $pdftotext->setArg(null, $temp); $pdftotext->execute(); $this->addToLog($pdftotext); - $pages = preg_split("|\x{u000c}|", file_get_contents($temp)); + $pages = explode("\f", file_get_contents($temp)); foreach($pages as $i => $page) { $i++; - file_put_contents($this->out . 'p' . $i . '.txt', cubeText::removeAccents($page)); + $txt = mb_strtolower($page); + $txt = cubeText::removeAccents($txt); + $txt = trim($txt); + + file_put_contents($this->out . 'p' . $i . '.txt', $txt); } unlink($temp); @@ -216,12 +325,12 @@ class wsDocument extends cubeMetier { public function makeMiniShot($page) { - $this->makeShotFixedWidth($page, 'p', 100, 70, 4, 'GS'); + $this->makeShotFixedWidth($page, 'p', 100, 90, 4, 'GS'); } public function makeRealShot($page) { - $this->makeShot($page, 'te', 72); + $this->makeShot($page, 'te', 150, 60, 4, 'GS', $this->uncropped); } public function makeShotFixedWidth($page , $prefix = '', $w = 100, $quality = 90, $antialiasing = 4, $method = 'GS') @@ -242,13 +351,16 @@ class wsDocument extends cubeMetier { $this->makeShot($page, $prefix, round(72 / $ratio, 2), $quality, $antialiasing, $method); } - public function makeShot($page , $prefix = '', $resolution = 72, $quality = 90, $antialiasing = 4, $method = 'GS') + public function makeShot($page , $prefix = '', $resolution = 72, $quality = 90, $antialiasing = 4, $method = 'GS', $in = null) { + if (is_null($in)) { + $in = $this->cropped; + } // Delete all old files if ($method == 'GS') { - $this->makeShotGS($page, $prefix, $resolution, $quality, $antialiasing); + $this->makeShotGS($page, $prefix, $resolution, $quality, $antialiasing, $in); } elseif ($method == 'PNM') { - $this->makeShotPNM($page, $prefix, $resolution, $quality, $antialiasing); + $this->makeShotPNM($page, $prefix, $resolution, $quality, $antialiasing, $in); } // Test the result by checking all files if (!file_exists($this->out . $prefix . $page . '.jpg')) { @@ -257,15 +369,18 @@ class wsDocument extends cubeMetier { // If error, we try to make thumbs with other method if ($error) { if ($method == 'GS') { - $this->makeShotPNM($page, $prefix, $resolution, $quality, $antialiasing); + $this->makeShotPNM($page, $prefix, $resolution, $quality, $antialiasing, $in); } elseif ($method == 'PNM') { - $this->makeShotGS($page, $prefix, $resolution, $quality, $antialiasing); + $this->makeShotGS($page, $prefix, $resolution, $quality, $antialiasing, $in); } } } - protected function makeShotGS($page, $prefix = '', $resolution = 72, $quality = 90, $antialiasing = 4) + protected function makeShotGS($page, $prefix = '', $resolution = 72, $quality = 90, $antialiasing = 4, $in = null) { + if (is_null($in)) { + $in = $this->cropped; + } // Fabrication des thumbanails avec ghostscript $gs = new cubeCommandLine('gs', null, true); $gs->setPath(CONVERTER_PATH); @@ -274,23 +389,34 @@ class wsDocument extends cubeMetier { $gs->setArg('-dNOPAUSE'); $gs->setArg('-dNOPROMPT'); $gs->setArg('-sDEVICE=jpeg'); + // Dispotion & colors $gs->setArg('-dUseCIEColor'); + $gs->setArg('-dAutoRotatePages=/None'); + // Resolution & Quality $gs->setArg('-r' . $resolution); $gs->setArg('-dJPEGQ=' . $quality); + // Antialias + $gs->setArg('-dDOINTERPOLATE'); $gs->setArg('-dTextAlphaBits=' . $antialiasing); $gs->setArg('-dGraphicsAlphaBits=' . $antialiasing); - // $gs->setArg('-dUseCropBox'); + // Performances + $gs->setArg('-dNumRenderingThreads=4'); + // Page range $gs->setArg('-dFirstPage=' . $page); $gs->setArg('-dLastPage=' . $page); - $gs->setArg('-sOutputFile=' . $this->out . '/' . $prefix . '%d.jpg'); - $gs->setArg('-dAutoRotatePages=/None'); - $gs->setArg(null, $this->in); + // Files + $gs->setArg('-sOutputFile=' . $this->out . '/' . $prefix . $page . '.jpg'); + + $gs->setArg(null, $in); $gs->execute(); $this->addToLog($gs); } - protected function makeShotPNM($page, $prefix = '', $resolution = 72, $quality = 90, $antialiasing = 4) + protected function makeShotPNM($page, $prefix = '', $resolution = 72, $quality = 90, $antialiasing = 4, $in = null) { + if (is_null($in)) { + $in = $this->crop; + } $antialiasing = $antialiasing?'yes':'no'; $resolution = round($resolution); // Exporte les fichiers @@ -307,7 +433,7 @@ class wsDocument extends cubeMetier { $pdftoppm->setArg('-t1lib yes'); } $pdftoppm->setArg('r', $resolution); - $pdftoppm->setArg(null, $this->in); + $pdftoppm->setArg(null, $in); $pdftoppm->setArg(null, $this->out . 'ppm'); $pdftoppm->execute(); $this->addToLog($pdftoppm); @@ -337,6 +463,11 @@ class wsDocument extends cubeMetier { if ($maxObjects <= 1) { $method = self::POLY2BITMAP; } + // Pour les fichiers croppés, on utilise la méthode flatten qui ne prends + // pas en compte les objets hors de la box + if ($this->crop) { + $method = max($method, self::FLATTEN); + } $out = $this->pdf2swf($page, $resolution, $quality, $storeAllChars, $method); if ($method < self::BARBARE_PNM) { @@ -502,7 +633,7 @@ disablelinks Disable links. } $pdf2swf->setArg('stop'); - $pdf2swf->setManualArg('-v'); + // $pdf2swf->setManualArg('-v'); $pdf2swf->setArg('T', 8); if ($storeAllChars) { $pdf2swf->setArg('fonts'); @@ -517,7 +648,7 @@ disablelinks Disable links. $pdf2swf->setArg('set disablelinks'); $pdf2swf->setArg('set dots'); - $pdf2swf->setArg(null, $this->in); + $pdf2swf->setArg(null, $this->cropped); $pdf2swf->setArg('output', $this->out . 'p%.swf'); $pdf2swf->execute(); @@ -536,7 +667,7 @@ disablelinks Disable links. $swfcombine->setArg('merge'); $swfcombine->setArg('stack1'); $swfcombine->setArg('z'); - $swfcombine->setManualArg('-vvvv'); + $swfcombine->setManualArg('-v'); $swfcombine->setArg('o', $swffile); $swfcombine->setArg(null, ROOT . '/swf/as3Container.swf'); $swfcombine->setManualArg('content=' . $swffile); @@ -546,7 +677,7 @@ disablelinks Disable links. protected function getLinks() { - $fwstk = new cubeCommandLine('fwstk', 'C:\Users\Vincent\Desktop\out.txt', false); + $fwstk = new cubeCommandLine('fwstk'); $fwstk->setPath(CONVERTER_PATH); $fwstk->setArg('--input ' . $this->in); $fwstk->setArg('--extractLinks ' . $this->out . 'p%d.csv'); -- 2.39.5