From: Vincent Vanwaelscappel Date: Fri, 1 Sep 2023 15:06:53 +0000 (+0200) Subject: wip #6237 @0.25 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=6403402f862efe484adafb2f5649dc49c776e9e5;p=fluidbook_tools.git wip #6237 @0.25 --- diff --git a/src/Jobs/ProcessFile.php b/src/Jobs/ProcessFile.php index c86b4d4..818c02f 100644 --- a/src/Jobs/ProcessFile.php +++ b/src/Jobs/ProcessFile.php @@ -6,7 +6,8 @@ use Cubist\Util\Files\Files; use Cubist\PDF\PDFTools; use Fluidbook\Tools\SVG\SVGTools; -class ProcessFile { +class ProcessFile +{ protected $format = 'jpg'; /** * @var int|string @@ -32,7 +33,8 @@ class ProcessFile { */ protected $job; - public function __construct($format = 'jpg', $resolution = 150, $quality = 85, $withGraphics = true, $withTexts = true, $version = 'html') { + public function __construct($format = 'jpg', $resolution = 150, $quality = 85, $withGraphics = true, $withTexts = true, $version = 'html') + { $this->format = $format; $this->resolution = $resolution; $this->withGraphics = $withGraphics; @@ -44,14 +46,16 @@ class ProcessFile { /** * @param string $format */ - public function setFormat(string $format) { + public function setFormat(string $format) + { $this->format = $format; } /** * @return string */ - public function getFormat(): string { + public function getFormat(): string + { if ($this->format === 'jpeg') { return 'jpg'; } @@ -61,56 +65,64 @@ class ProcessFile { /** * @param int|string $resolution */ - public function setResolution(int|string $resolution) { + public function setResolution(int|string $resolution) + { $this->resolution = $resolution; } /** * @return int|string */ - public function getResolution(): int|string { + public function getResolution(): int|string + { return $this->resolution; } /** * @return bool */ - public function isWithGraphics(): bool { + public function isWithGraphics(): bool + { return $this->withGraphics; } /** * @param bool $withGraphics */ - public function setWithGraphics(bool $withGraphics) { + public function setWithGraphics(bool $withGraphics) + { $this->withGraphics = $withGraphics; } /** * @return bool */ - public function isWithTexts(): bool { + public function isWithTexts(): bool + { return $this->withTexts; } /** * @param bool $withTexts */ - public function setWithTexts(bool $withTexts) { + public function setWithTexts(bool $withTexts) + { $this->withTexts = $withTexts; } /** * @return int */ - public function getPage(): int { + public function getPage(): int + { return $this->job->getPage(); } /** * @return mixed */ - public function getOut() { + public function getOut() + { return $this->job->getOut(); } @@ -118,7 +130,8 @@ class ProcessFile { /** * @return string */ - public function getVersion(): string { + public function getVersion(): string + { if ($this->getFormat() === 'svg') { return 'html'; } else if ($this->getFormat() === 'swf') { @@ -131,54 +144,39 @@ class ProcessFile { /** * @param string $version */ - public function setVersion(string $version) { + public function setVersion(string $version) + { $this->version = $version; } /** * @return ProcessPage */ - public function getJob(): ProcessPage { + public function getJob(): ProcessPage + { return $this->job; } /** * @param ProcessPage $job */ - public function setJob(ProcessPage $job) { + public function setJob(ProcessPage $job) + { $this->job = $job; return $this; } - public function getPath($force = false) { + public function getPath($force = false) + { $dir = rtrim($this->getOut() . $this->getVersion(), '/') . '/'; $minsize = 1; - $res = ''; if ($this->getFormat() === 'svg') { - $prefix = $this->isWithGraphics() ? 'f' : 't'; - $res = $dir . $prefix . 'o' . $this->getPage(); - if ($this->isWithGraphics()) { - if ($this->getResolution() == 0) { - $res = $dir . $prefix . 'p' . $this->getPage(); - } else { - $res .= '-' . $this->getResolution(); - } - } - $res .= '.svg'; $reffile = $this->makeSVGFile(); $minsize = 100; - } else if (in_array($this->getFormat(), ['png', 'jpg'])) { - $q = ($this->getFormat() === 'jpg' && $this->getQuality() !== 85) ? '-' . $this->getQuality() : ''; - $prefix = $this->isWithTexts() ? 't' : 'h'; - if ($this->getResolution() === 'thumb') { - $res = $dir . 'p' . $this->getPage() . $q . '.' . $this->getFormat(); - } else { - $res = $dir . $prefix . $this->getPage() . '-' . $this->getResolution() . $q . '.' . $this->getFormat(); - } - } else if ($this->getFormat() === 'swf') { - $res = $dir . 'p' . $this->getPage() . '.' . $this->getFormat(); } + $res = $dir . self::getFilename($this->getPage(), $this->getFormat(), $this->getResolution(), $this->getQuality(), $this->isWithGraphics(), $this->isWithTexts(), $this->getVersion()); + $do = false; if (!file_exists($res) || filesize($res) < $minsize) { $do = true; @@ -192,7 +190,38 @@ class ProcessFile { return $res; } - public function makeFile($file) { + public static function getFilename($page, $format = 'jpg', $resolution = 150, $quality = 85, $withGraphics = true, $withTexts = true, $version = 'html') + { + $res = ''; + if ($format === 'svg') { + $prefix = $withGraphics ? 'f' : 't'; + $res = $prefix . 'o' . $page; + if ($withGraphics) { + if ($resolution == 0) { + $res = $prefix . 'p' . $page; + } else { + $res .= '-' . $resolution; + } + } + $res .= '.svg'; + + } else if (in_array($format, ['png', 'jpg'])) { + $q = ($format === 'jpg' && $quality !== 85) ? '-' . $quality : ''; + $prefix = $withTexts ? 't' : 'h'; + if ($resolution === 'thumb') { + $res = 'p' . $page . $q . '.' . $format; + } else { + $res = $prefix . $page . '-' . $resolution . $q . '.' . $format; + } + } else if ($format === 'swf') { + $res = 'p' . $page . '.' . $format; + } + + return $res; + } + + public function makeFile($file) + { $lock = $file . '.lock'; if (file_exists($lock) && filemtime($lock) > time() - 300) { sleep(10); @@ -226,7 +255,8 @@ class ProcessFile { } } - public function makeSVGFile($force = false) { + public function makeSVGFile($force = false) + { $svgFile = $this->getOut() . '/html/fp' . $this->getPage() . '.svg'; if (!$force && file_exists($svgFile) && filesize($svgFile) > 0) { return $svgFile; @@ -235,29 +265,34 @@ class ProcessFile { return $svgFile; } - public function makeTextSVGFile($out) { + public function makeTextSVGFile($out) + { $in = $this->makeSVGFile(); $inter = str_replace('/to', '/tp', $out); PDFTools::makeTextSVGFile($in, $inter); SVGTools::optimizeSVG($inter, $out); } - public function getResolutionRatio(): float { + public function getResolutionRatio(): float + { return $this->getJob()->getResolutionRatio(); } - public function getMobileRatio(): float { + public function getMobileRatio(): float + { return $this->getJob()->getMobileFirstRatio(); } - public function makeOptimizedSVGFile($out) { + public function makeOptimizedSVGFile($out) + { $in = $this->makeSVGFile(); SVGTools::optimizeSVGImages($in, $out, $this->getResolution()); SVGTools::optimizeSVG($out, $out); } - public function getSplittedPDFPage() { + public function getSplittedPDFPage() + { $res = $this->getOut() . 'pdf/p' . $this->getPage() . '.pdf'; if (!file_exists($res)) { $this->getJob()->splitDoc(); @@ -268,14 +303,16 @@ class ProcessFile { /** * @return int */ - public function getQuality(): int { + public function getQuality(): int + { return $this->quality; } /** * @param float|int|string $quality */ - public function setQuality(float|int|string $quality): void { + public function setQuality(float|int|string $quality): void + { $this->quality = max(0, min(100, round((float)$quality))); } }