From 23915e7380da05ae96d1d9737ec85c4fc09f6288 Mon Sep 17 00:00:00 2001 From: Patrick Organ Date: Tue, 28 May 2024 02:49:11 -0400 Subject: [PATCH] remove initialization of imagick instance from constructor as it is unneeded --- src/Pdf.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Pdf.php b/src/Pdf.php index ca372c5..f96142b 100644 --- a/src/Pdf.php +++ b/src/Pdf.php @@ -14,7 +14,7 @@ class Pdf { - protected string $pdfFile; + protected string $filename; protected int $resolution = 144; @@ -36,17 +36,13 @@ class Pdf private ?int $numberOfPages = null; - public function __construct(string $pdfFile) + public function __construct(string $filename) { - if (! file_exists($pdfFile)) { - throw PdfDoesNotExist::for($pdfFile); + if (! file_exists($filename)) { + throw PdfDoesNotExist::for($filename); } - $this->pdfFile = $pdfFile; - - $this->imagick = new Imagick(); - - $this->imagick->readImage($this->pdfFile); + $this->filename = $filename; } public function resolution(int $dpiResolution): static @@ -126,6 +122,11 @@ public function selectPages(int ...$pages): static public function pageCount(): int { + if ($this->imagick === null) { + $this->imagick = new Imagick(); + $this->imagick->readImage($this->filename); + } + if ($this->numberOfPages === null) { $this->numberOfPages = $this->imagick->getNumberImages(); } @@ -195,7 +196,7 @@ public function getImageData(string $pathToImage, int $pageNumber): Imagick $this->imagick->setCompressionQuality($this->compressionQuality); } - $this->imagick->readImage(sprintf('%s[%s]', $this->pdfFile, $pageNumber - 1)); + $this->imagick->readImage(sprintf('%s[%s]', $this->filename, $pageNumber - 1)); if ($this->layerMethod !== LayerMethod::None) { $this->imagick = $this->imagick->mergeImageLayers($this->layerMethod->value);