}
}
+ $thumb = false;
+ if ($book->parametres->pdfThumbnails) {
+ $thumb = $this->getThumbFromPDF($workingDir . '/' . $book->parametres->pdfThumbnails, $i);
+ }
+ if (!$thumb) {
+ $thumb = $base . '.jpg';
+ }
+
if ($i == 1) {
- $flexLight->addBitmap($base . '.jpg', 'thumb1');
+ $flexLight->addBitmap($thumb, 'thumb1');
} else {
- $flex->addBitmap($base . '.jpg', 'thumb' . $i);
+ $flex->addBitmap($thumb, 'thumb' . $i);
}
$sizes[$i] = $fsize;
}
$flexLight->addVariable('checksum', $hash, false, true, 'String');
}
+ public function getThumbFromPDF($pdf, $page) {
+ if (!file_exists($pdf)) {
+ return false;
+ }
+ $dir = WS_CACHE . '/thumbs/' . sha1($pdf) . '/';
+ if (!file_exists($dir)) {
+ mkdir($dir, 0777, true);
+ }
+ $jpeg = $dir . '/p' . $page . '.jpg';
+ $mtime = filemtime($jpeg);
+
+ if (!file_exists($jpeg) || $mtime < filemtime(__FILE__) || $mtime < filemtime($pdf)) {
+ wsPDFConvert::makeMiniShot($pdf, $jpeg, $page);
+ }
+
+ return $jpeg;
+ }
+
public static function getWorkingFile($path, $book_id, $dir = "") {
if (substr($path, 0, 1) == '/' && file_exists($path)) {
return $path;
}
$workingDir = WS_BOOKS . '/working/' . $book_id . '/';
- return $workingDir . $dir . '/' . $path;
+ return $workingDir . trim($dir,'/') . '/' . $path;
}
public function compileWidget($book, $pages) {
--- /dev/null
+<?php\r
+\r
+/**\r
+ * Created by IntelliJ IDEA.\r
+ * User: Vincent\r
+ * Date: 17/11/2016\r
+ * Time: 14:24\r
+ */\r
+class wsPDFConvert {\r
+\r
+ public static function makeMiniShot($in, $out, $page) {\r
+ self::makeShotFixedWidth($in, $out, $page, 'p', 100, 90, 4, 'PNM');\r
+ }\r
+\r
+ public static function makeShotFixedWidth($in, $out, $page, $prefix = '', $w = 100, $quality = 90, $antialiasing = 4, $method = 'PNM') {\r
+ // Make thumbs of $w width\r
+ self::makeShot($in, $out, $page, $prefix, null, $quality, $antialiasing, $method, $w, -1);\r
+ }\r
+\r
+ public static function makeShotFixedHeight($in, $out, $page, $prefix = '', $h = '', $quality = 90, $antialiasing = 4, $method = 'PNM') {\r
+ // Make thumbs of $h height\r
+ self::makeShot($in, $out, $page, $prefix, null, $quality, $antialiasing, $method, -1, $h);\r
+ }\r
+\r
+ public static function makeShot($in, $out, $page, $prefix = '', $resolution = 72, $quality = 90, $antialiasing = 4, $method = 'PNM', $width = null, $height = null) {\r
+ $error = false;\r
+ if ($method == 'GS') {\r
+ self::makeShotGS($in, $out, $page, $prefix, $resolution, $quality, $antialiasing, $width, $height);\r
+ } elseif ($method == 'PNM') {\r
+ self::makeShotPNM($in, $out, $page, $prefix, $resolution, $quality, $antialiasing, true, $width, $height);\r
+ }\r
+ // Test the result by checking all files\r
+ if (!file_exists($out)) {\r
+ $error = true;\r
+ }\r
+ // If error, we try to make thumbs with other method\r
+ if ($error) {\r
+ if ($method == 'GS') {\r
+ self::makeShotPNM($in, $out, $page, $prefix, $resolution, $quality, $antialiasing, true, $width, $height);\r
+ } elseif ($method == 'PNM') {\r
+ self::makeShotGS($in, $out, $page, $prefix, $resolution, $quality, $antialiasing, $width, $height);\r
+ }\r
+ }\r
+ }\r
+\r
+ protected function makeShotGS($in, $out, $page, $prefix = '', $resolution = 72, $quality = 90, $antialiasing = 4, $width = null, $height = null) {\r
+\r
+ // Fabrication des thumbanails avec ghostscript\r
+ $gs = new cubeCommandLine('gs', null, true);\r
+ $gs->setPath(CONVERTER_PATH);\r
+ $gs->setEnv('GS_FONTPATH', FONT_PATH);\r
+ $gs->setArg('-dBATCH');\r
+ $gs->setArg('-dNOPAUSE');\r
+ $gs->setArg('-dNOPROMPT');\r
+ // Antialias\r
+ $gs->setArg('-dDOINTERPOLATE');\r
+ $gs->setArg('-dTextAlphaBits=' . $antialiasing);\r
+ $gs->setArg('-dGraphicsAlphaBits=' . $antialiasing);\r
+ // Device\r
+ $gs->setArg('-sDEVICE=jpeg');\r
+ // Dispotion & colors\r
+ // $gs->setArg('-dUseCIEColor');\r
+ $gs->setArg('-dAutoRotatePages=/None');\r
+ $gs->setArg('-dUseCropBox');\r
+ // Resolution & Quality\r
+ $gs->setArg('-r' . round($resolution));\r
+ $gs->setArg('-dJPEGQ=' . $quality);\r
+ // Performances\r
+ $gs->setArg('-dNumRenderingThreads=4');\r
+ // Page range\r
+ $gs->setArg('-dFirstPage=' . $page);\r
+ $gs->setArg('-dLastPage=' . $page);\r
+ // Files\r
+ $gs->setArg('-sOutputFile=' . $out);\r
+\r
+ $gs->setArg(null, $in);\r
+ $gs->execute();\r
+ }\r
+\r
+ protected function makeShotPNM($in, $out, $page, $prefix = '', $resolution = 72, $quality = 90, $antialiasing = 4, $texts = true, $width = null, $height = null) {\r
+ $tmp = cubeFiles::tempnam();\r
+\r
+ $antialiasing = $antialiasing ? 'yes' : 'no';\r
+ $freetype = $texts ? 'yes' : 'no';\r
+ // Exporte les fichiers\r
+ $pdftoppm = new cubeCommandLine('pdftoppm', null, true);\r
+ $pdftoppm->setPath(CONVERTER_PATH);\r
+\r
+ $pdftoppm->setArg('f', $page);\r
+ $pdftoppm->setArg('l', $page);\r
+ $pdftoppm->setArg('-cropbox');\r
+ $pdftoppm->setArg('-freetype ' . $freetype);\r
+ $pdftoppm->setArg('-singlefile');\r
+ $pdftoppm->setArg('-aa ' . $antialiasing);\r
+ $pdftoppm->setArg('-aaVector ' . $antialiasing);\r
+ if (null !== $resolution) {\r
+ $pdftoppm->setArg('r', $resolution);\r
+ }\r
+ if (null !== $width) {\r
+ $pdftoppm->setArg('-scale-to-x ' . $width);\r
+ }\r
+ if (null !== $height) {\r
+ $pdftoppm->setArg('-scale-to-y ' . $height);\r
+ }\r
+ $pdftoppm->setArg(null, $in);\r
+ $pdftoppm->setArg(null, $tmp);\r
+ $pdftoppm->execute();\r
+ $tmp .= '.ppm';\r
+\r
+\r
+ if (file_exists($tmp)) {\r
+ $cjpeg = new cubeCommandLine('cjpeg', null, true);\r
+ $cjpeg->setArg('-quality ' . ($quality + 6));\r
+ $cjpeg->setArg('-outfile ' . $out);\r
+ $cjpeg->setArg(null, $tmp);\r
+ $cjpeg->execute();\r
+ unlink($tmp);\r
+ }\r
+ }\r
+\r
+\r
+}
\ No newline at end of file