![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/ts.corals.io/corals-api/vendor/spatie/pdf-to-image/src/ |
<?php namespace Spatie\PdfToImage; use Imagick; use Spatie\PdfToImage\Exceptions\InvalidFormat; use Spatie\PdfToImage\Exceptions\PageDoesNotExist; use Spatie\PdfToImage\Exceptions\PdfDoesNotExist; class Pdf { protected $pdfFile; protected $resolution = 144; protected $outputFormat = 'jpg'; protected $page = 1; public $imagick; protected $numberOfPages; protected $validOutputFormats = ['jpg', 'jpeg', 'png']; protected $layerMethod = Imagick::LAYERMETHOD_FLATTEN; protected $colorspace; protected $compressionQuality; protected $thumbnailWidth; public function __construct(string $pdfFile) { if (! file_exists($pdfFile)) { throw new PdfDoesNotExist("File `{$pdfFile}` does not exist"); } $this->imagick = new Imagick(); $this->imagick->pingImage($pdfFile); $this->numberOfPages = $this->imagick->getNumberImages(); $this->pdfFile = $pdfFile; } public function setResolution(int $resolution) { $this->resolution = $resolution; return $this; } public function setOutputFormat(string $outputFormat) { if (! $this->isValidOutputFormat($outputFormat)) { throw new InvalidFormat("Format {$outputFormat} is not supported"); } $this->outputFormat = $outputFormat; return $this; } public function getOutputFormat(): string { return $this->outputFormat; } /** * Sets the layer method for Imagick::mergeImageLayers() * If int, should correspond to a predefined LAYERMETHOD constant. * If null, Imagick::mergeImageLayers() will not be called. * * @param int|null * * @return $this * * @throws \Spatie\PdfToImage\Exceptions\InvalidLayerMethod * * @see https://secure.php.net/manual/en/imagick.constants.php * @see Pdf::getImageData() */ public function setLayerMethod(?int $layerMethod) { $this->layerMethod = $layerMethod; return $this; } public function isValidOutputFormat(string $outputFormat): bool { return in_array($outputFormat, $this->validOutputFormats); } public function setPage(int $page) { if ($page > $this->getNumberOfPages() || $page < 1) { throw new PageDoesNotExist("Page {$page} does not exist"); } $this->page = $page; return $this; } public function getNumberOfPages(): int { return $this->numberOfPages; } public function saveImage(string $pathToImage): bool { if (is_dir($pathToImage)) { $pathToImage = rtrim($pathToImage, '\/').DIRECTORY_SEPARATOR.$this->page.'.'.$this->outputFormat; } $imageData = $this->getImageData($pathToImage); return file_put_contents($pathToImage, $imageData) !== false; } public function saveAllPagesAsImages(string $directory, string $prefix = ''): array { $numberOfPages = $this->getNumberOfPages(); if ($numberOfPages === 0) { return []; } return array_map(function ($pageNumber) use ($directory, $prefix) { $this->setPage($pageNumber); $destination = "{$directory}/{$prefix}{$pageNumber}.{$this->outputFormat}"; $this->saveImage($destination); return $destination; }, range(1, $numberOfPages)); } public function getImageData(string $pathToImage): Imagick { /* * Reinitialize imagick because the target resolution must be set * before reading the actual image. */ $this->imagick = new Imagick(); $this->imagick->setResolution($this->resolution, $this->resolution); if ($this->colorspace !== null) { $this->imagick->setColorspace($this->colorspace); } if ($this->compressionQuality !== null) { $this->imagick->setCompressionQuality($this->compressionQuality); } if (filter_var($this->pdfFile, FILTER_VALIDATE_URL)) { return $this->getRemoteImageData($pathToImage); } $this->imagick->readImage(sprintf('%s[%s]', $this->pdfFile, $this->page - 1)); if (is_int($this->layerMethod)) { $this->imagick = $this->imagick->mergeImageLayers($this->layerMethod); } if ($this->thumbnailWidth !== null) { $this->imagick->thumbnailImage($this->thumbnailWidth, 0); } $this->imagick->setFormat($this->determineOutputFormat($pathToImage)); return $this->imagick; } public function setColorspace(int $colorspace) { $this->colorspace = $colorspace; return $this; } public function setCompressionQuality(int $compressionQuality) { $this->compressionQuality = $compressionQuality; return $this; } public function width(int $thumbnailWidth) { $this->thumbnailWidth = $thumbnailWidth; return $this; } protected function getRemoteImageData(string $pathToImage): Imagick { $this->imagick->readImage($this->pdfFile); $this->imagick->setIteratorIndex($this->page - 1); if (is_int($this->layerMethod)) { $this->imagick = $this->imagick->mergeImageLayers($this->layerMethod); } $this->imagick->setFormat($this->determineOutputFormat($pathToImage)); return $this->imagick; } protected function determineOutputFormat(string $pathToImage): string { $outputFormat = pathinfo($pathToImage, PATHINFO_EXTENSION); if ($this->outputFormat != '') { $outputFormat = $this->outputFormat; } $outputFormat = strtolower($outputFormat); if (! $this->isValidOutputFormat($outputFormat)) { $outputFormat = 'jpg'; } return $outputFormat; } }