Skip to content

Commit

Permalink
nitpicks
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed Jun 10, 2024
1 parent c4a2a3f commit ec42f28
Showing 1 changed file with 65 additions and 8 deletions.
73 changes: 65 additions & 8 deletions src/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Pdf

protected ?int $resizeHeight = null;

private ?int $numberOfPages = null;
protected ?int $numberOfPages = null;

public function __construct(string $filename)
{
Expand All @@ -50,13 +50,27 @@ public function __construct(string $filename)
$this->filename = $filename;
}

/**
* Sets the resolution of the generated image in DPI.
* Default is 144.
*
* @param int $dpiResolution
* @return static
*/
public function resolution(int $dpiResolution): static
{
$this->resolution = $dpiResolution;

return $this;
}

/**
* Sets the output format of the generated image.
* Default is OutputFormat::Jpg.
*
* @param \Spatie\PdfToImage\Enums\OutputFormat $outputFormat
* @return static
*/
public function format(OutputFormat $outputFormat): static
{
$this->outputFormat = $outputFormat;
Expand Down Expand Up @@ -125,6 +139,11 @@ public function selectPages(int ...$pages): static
return $this;
}

/**
* Returns the number of pages in the PDF.
*
* @return int
*/
public function pageCount(): int
{
if ($this->imagick === null) {
Expand All @@ -139,6 +158,12 @@ public function pageCount(): int
return $this->numberOfPages;
}

/**
* Returns a DTO representing the size of the PDF, which
* contains the width and height in pixels.
*
* @return \Spatie\PdfToImage\DTOs\PageSize
*/
public function getSize(): PageSize
{
if ($this->imagick === null) {
Expand All @@ -156,7 +181,7 @@ public function getSize(): PageSize
* a directory if multiple pages have been selected (otherwise the image will be overwritten).
* Returns an array of paths to the saved images.
*
* @return string[]
* @return array<string>
*/
public function save(string $pathToImage, string $prefix = ''): array
{
Expand All @@ -180,12 +205,20 @@ public function save(string $pathToImage, string $prefix = ''): array
return $result;
}

/**
* Saves all pages of the PDF as images. Expects a directory to save the images to,
* and an optional prefix for the image filenames. Returns an array of paths to the saved images.
*
* @param string $directory
* @param string $prefix
* @return array<string>
*/
public function saveAllPages(string $directory, string $prefix = ''): array
{
$numberOfPages = $this->pageCount();

if ($numberOfPages === 0) {
return false;
return [];
}

$this->selectPages(...range(1, $numberOfPages));
Expand Down Expand Up @@ -230,14 +263,22 @@ public function getImageData(string $pathToImage, int $pageNumber): Imagick
return $this->imagick;
}

public function colorspace(int $colorspace)
public function colorspace(int $colorspace): static
{
$this->colorspace = $colorspace;

return $this;
}

public function quality(int $compressionQuality)
/**
* Set the compression quality for the image. The value should be between 1 and 100, where
* 1 is the lowest quality and 100 is the highest.
*
* @param int $compressionQuality
* @return static
* @throws \Spatie\PdfToImage\Exceptions\InvalidQuality
*/
public function quality(int $compressionQuality): static
{
if ($compressionQuality < 1 || $compressionQuality > 100) {
throw InvalidQuality::for($compressionQuality);
Expand All @@ -252,11 +293,11 @@ public function quality(int $compressionQuality)
* Set the thumbnail size for the image. If no height is provided, the thumbnail height will
* be scaled according to the width.
*
* @return $this
* @return static
*
* @throws \Spatie\PdfToImage\Exceptions\InvalidSize
*/
public function thumbnailSize(int $width, ?int $height = null)
public function thumbnailSize(int $width, ?int $height = null): static
{
if ($width < 0) {
throw InvalidSize::forThumbnail($width, 'width');
Expand All @@ -272,7 +313,15 @@ public function thumbnailSize(int $width, ?int $height = null)
return $this;
}

public function size(int $width, ?int $height = null)
/**
* Set the size of the image. If no height is provided, the height will be scaled according to the width.
*
* @param int $width
* @param int|null $height
* @return static
* @throws \Spatie\PdfToImage\Exceptions\InvalidSize
*/
public function size(int $width, ?int $height = null): static
{
if ($width < 0) {
throw InvalidSize::forImage($width, 'width');
Expand Down Expand Up @@ -303,6 +352,14 @@ protected function determineOutputFormat(string $pathToImage): OutputFormat
return $outputFormat;
}

/**
* Validate that the page numbers are within the range of the PDF, which is 1 to the number of pages.
* Throws a PageDoesNotExist exception if a page number is out of range.
*
* @param int ...$pageNumbers
* @return void
* @throws \Spatie\PdfToImage\Exceptions\PageDoesNotExist
*/
protected function validatePageNumbers(int ...$pageNumbers): void
{
$count = $this->pageCount();
Expand Down

0 comments on commit ec42f28

Please sign in to comment.