Skip to content

Commit

Permalink
[11.x] Support spatie/pdf-to-image v3 (#3633)
Browse files Browse the repository at this point in the history
* Support spatie/pdf-to-image v3

* Check for v3

* Fix workflow

* Revert workflow
  • Loading branch information
Jubeki authored Jun 12, 2024
1 parent 255e227 commit d289f81
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"pestphp/pest": "^2.28",
"phpstan/extension-installer": "^1.3.1",
"spatie/laravel-ray": "^1.33",
"spatie/pdf-to-image": "^2.2",
"spatie/pdf-to-image": "^2.2|^3.0",
"spatie/pest-plugin-snapshots": "^2.1"
},
"conflict": {
Expand Down
13 changes: 12 additions & 1 deletion src/Conversions/ImageGenerators/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Spatie\MediaLibrary\Conversions\ImageGenerators;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Illuminate\Support\Collection;
use Imagick;
use Spatie\MediaLibrary\Conversions\Conversion;
Expand All @@ -14,11 +16,20 @@ public function convert(string $file, ?Conversion $conversion = null): string

$pageNumber = $conversion ? $conversion->getPdfPageNumber() : 1;

(new \Spatie\PdfToImage\Pdf($file))->setPage($pageNumber)->saveImage($imageFile);
if ($this->usesPdfToImageV3()) {
(new \Spatie\PdfToImage\Pdf($file))->selectPage($pageNumber)->save($imageFile);
} else {
(new \Spatie\PdfToImage\Pdf($file))->setPage($pageNumber)->saveImage($imageFile);

Check failure on line 22 in src/Conversions/ImageGenerators/Pdf.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Spatie\PdfToImage\Pdf::setPage().

Check failure on line 22 in src/Conversions/ImageGenerators/Pdf.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Spatie\PdfToImage\Pdf::setPage().
}

return $imageFile;
}

private function usesPdfToImageV3(): bool
{
return InstalledVersions::satisfies(new VersionParser, 'spatie/pdf-to-image', '^3.0');
}

public function requirementsAreInstalled(): bool
{
if (! class_exists(Imagick::class)) {
Expand Down

0 comments on commit d289f81

Please sign in to comment.