use Cubist\PDF\PDFTools;
use Fluidbook\Tools\SVG\SVGTools;
-class ProcessFile {
+class ProcessFile
+{
protected $format = 'jpg';
/**
* @var int|string
*/
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;
/**
* @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';
}
/**
* @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();
}
/**
* @return string
*/
- public function getVersion(): string {
+ public function getVersion(): string
+ {
if ($this->getFormat() === 'svg') {
return 'html';
} else if ($this->getFormat() === 'swf') {
/**
* @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;
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);
}
}
- 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;
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();
/**
* @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)));
}
}