namespace App\Jobs;
-use Cubist\Util\CommandLine;
+use App\Models\Publication;
use Cubist\Util\Files\Files;
use Cubist\Util\Files\VirtualDirectory;
use Cubist\Util\PHP;
-use Fluidbook\Tools\FluidbookTools;
+use Exception;
use Fluidbook\Tools\Jobs\ProcessFile;
+use Fluidbook\Tools\Jobs\ProcessPage;
use Fluidbook\Tools\PDF\Document;
use Fluidbook\Tools\Search\SearchIndex;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\Jobs\SyncJob;
use Illuminate\Queue\SerializesModels;
use JsonException;
+use Spatie\MediaLibrary\MediaCollections\Models\Media;
+use stdClass;
class ProcessFluidbook implements ShouldQueue, ShouldBeUnique
{
* @var Document
*/
protected $in;
+
+ /**
+ * @var string
+ */
protected $out;
+
+ /**
+ * @var string
+ */
protected $stub;
+ /**
+ * @var Publication
+ */
+ protected $pub;
+
+
/** @var VirtualDirectory */
protected $vdir;
+ /**
+ * @var stdClass
+ */
+ protected $config;
+
/**
* Create a new job instance.
*
*/
public function __construct()
{
- $this->in = new Document(storage_path('fluidbook/in/in.pdf'));
+
+ $this->pub = Publication::find(1);
+
+ /** @var Media $media */
+ $media = $this->pub->getMediaInField($this->pub->getAttributeValue('document'))->first();
+
+ $this->in = new Document($media->getPath());
$this->out = storage_path('fluidbook/out/');
$this->stub = resource_path('fluidbook/');
start_measure('Process Pages');
$out = $this->getConvertPath();
Files::mkdir($out);
- $this->in->processPages($out, [new ProcessFile('jpg', 'thumb'),
- new ProcessFile('jpg', 150, true, false),
- new ProcessFile('svg', 300, false, true),
- ], $sync);
+ $this->in->processPages($out, $this->getProcessFiles(), $sync);
stop_measure('Process Pages');
}
+ /**
+ * @return ProcessFile[]
+ */
+ public function getProcessFiles()
+ {
+ return [
+ 'thumb' => new ProcessFile('jpg', 'thumb'),
+ 'back150' => new ProcessFile('jpg', 150, true, false),
+ 'text' => new ProcessFile('svg', 300, false, true),
+ ];
+ }
+
+ /**
+ * @param $name
+ * @param null|int $page
+ * @return ProcessFile
+ * @throws Exception
+ */
+ public function getProcessFile($name, $page = null)
+ {
+ $files = $this->getProcessFiles();
+ if (isset($files[$name])) {
+ $res = $files[$name];
+ if (null !== $page) {
+ $res->setJob(new ProcessPage($this->in, $page, $this->getConvertPath()));
+ }
+ return $res;
+ }
+ throw new Exception(sprintf('File %s does not exist', $name));
+ }
+
public function getConvertPath()
{
return storage_path('fluidbook/convert/' . $this->in->getHash() . '/');
start_measure('Process links');
}
+ /**
+ * @throws JsonException
+ */
protected function compileFluidbook()
{
start_measure('Compile fluidbook');
public function compileContents()
{
- $cp = $this->getConvertPath();
for ($i = 1; $i <= $this->in->getPages(); $i++) {
- $this->vdir->copy($cp . 'html/t' . $i . '-150.jpg', 'data/background/150/p' . $i . '.jpg');
- $this->vdir->copy($cp . 'html/to' . $i . '.svg', 'data/contents/p' . $i . '.svg');
- $this->vdir->copy($cp . 'html/p' . $i . '.jpg', 'data/thumbnails/p' . $i . '.jpg');
+
+ $this->vdir->copy($this->getProcessFile('back150', $i)->getPath(), 'data/background/150/p' . $i . '.jpg');
+ $this->vdir->copy($this->getProcessFile('text', $i)->getPath(), 'data/contents/p' . $i . '.svg');
+ $this->vdir->copy($this->getProcessFile('thumb', $i)->getPath(), 'data/thumbnails/p' . $i . '.jpg');
}
}
public function compileConfig()
{
$settings = mb_substr(file_get_contents($this->stub . '/data/datas.js'), 13, -2);
- $config = json_decode($settings, false, 512, JSON_THROW_ON_ERROR);
+ $this->config = json_decode($settings, false, 512, JSON_THROW_ON_ERROR);
+
+ $this->compileDimensions();
+ $this->compilePageNumbers();
+ $this->compileChapters();
+ $this->config->links = $this->compileLinks();
+
+ $c = 'var SETTINGS=' . json_encode($this->config, JSON_THROW_ON_ERROR) . ';';
+ $this->vdir->file_put_contents('data/datas.js', $c);
+ }
+
+ protected function compileDimensions()
+ {
+ $width = round($this->in->getWidth(), 8);
+ $height = round($this->in->getHeight(), 8);
- $w = $this->in->getWidth();
- $h = $this->in->getHeight();
+ $this->config->pageZoomFactor = 3;
+ $this->config->optimalWidth = 567;
+ $this->config->optimalHeight = 709;
+ $imagesize = getimagesize($this->getProcessFile('back150', 1)->getPath());
+ $this->config->pdfZoomFactor = round(($imagesize[0] * 0.48) / $width, 12);
+
+ $this->config->cssScale = $this->config->pageZoomFactor * min($this->config->optimalWidth / $width, $this->config->optimalHeight / $height);
+ $cssWidth = $width * $this->config->cssScale;
+ $cssHeight = $height * $this->config->cssScale;
+
+ $this->config->pages = $this->in->getPages();
+ $this->config->width = $cssWidth;
+ $this->config->height = $cssHeight;
+
+ $this->config->multiply = $this->config->pdfZoomFactor * $this->config->cssScale;
+
+ $this->config->pagesDimensions = [];
+
+ $thumbDimensions = getimagesize($this->getProcessFile('thumb', 1)->getPath());
+ $this->config->thumbWidth = $thumbDimensions[0];
+ $this->config->thumbHeight = $thumbDimensions[1];
- $config->pages = $this->in->getPages();
- $config->width = $w;
- $config->height = $h;
- $config->pagesDimensions = [];
for ($i = 1; $i <= $this->in->getPages(); $i++) {
- $config->pagesDimensions[$i] = [$w, $h];
+ $this->config->pagesDimensions[$i] = [$this->config->width, $this->config->height];
}
- $config->links = $this->compileLinks();
- $this->vdir->file_put_contents('data/datas.js', 'var SETTINGS=' . json_encode($config, JSON_THROW_ON_ERROR) . ';');
+ }
+
+ protected function compilePageNumbers()
+ {
+ $this->config->numerotation = $this->in->getPageNumbers();
+ }
+
+ protected function compileChapters()
+ {
+ $this->config->chapters = $this->in->getChapters();
}