Skip to content

Commit 82cb288

Browse files
authored
Add thumbs creation for pdf if enabled in Imagick. (#3329)
1 parent a9329e7 commit 82cb288

4 files changed

Lines changed: 74 additions & 0 deletions

File tree

app/Actions/Diagnostics/Errors.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use App\Actions\Diagnostics\Pipes\Checks\ForeignKeyListInfo;
2121
use App\Actions\Diagnostics\Pipes\Checks\GDSupportCheck;
2222
use App\Actions\Diagnostics\Pipes\Checks\ImageOptCheck;
23+
use App\Actions\Diagnostics\Pipes\Checks\ImagickPdfCheck;
2324
use App\Actions\Diagnostics\Pipes\Checks\IniSettingsCheck;
2425
use App\Actions\Diagnostics\Pipes\Checks\MigrationCheck;
2526
use App\Actions\Diagnostics\Pipes\Checks\OpCacheCheck;
@@ -61,6 +62,7 @@ class Errors
6162
CachePasswordCheck::class,
6263
CacheTemporaryUrlCheck::class,
6364
SupporterCheck::class,
65+
ImagickPdfCheck::class,
6466
];
6567

6668
/**
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* SPDX-License-Identifier: MIT
5+
* Copyright (c) 2017-2018 Tobias Reich
6+
* Copyright (c) 2018-2025 LycheeOrg.
7+
*/
8+
9+
namespace App\Actions\Diagnostics\Pipes\Checks;
10+
11+
use App\Contracts\DiagnosticPipe;
12+
use App\DTO\DiagnosticData;
13+
use App\Models\Configs;
14+
use Illuminate\Support\Facades\Schema;
15+
use Safe\Exceptions\FilesystemException;
16+
use Safe\Exceptions\PcreException;
17+
use function Safe\file_get_contents;
18+
use function Safe\preg_match;
19+
20+
/**
21+
* Verify that if Imagick is installed, it is allowed to work with pdf files.
22+
*/
23+
class ImagickPdfCheck implements DiagnosticPipe
24+
{
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
public function handle(array &$data, \Closure $next): array
29+
{
30+
if (!Schema::hasTable('configs')) {
31+
// @codeCoverageIgnoreStart
32+
return $next($data);
33+
// @codeCoverageIgnoreEnd
34+
}
35+
36+
if (!Configs::hasImagick()) {
37+
$data[] = DiagnosticData::info('Imagick is not enabled. Thumbs will not be created for pdf files.', self::class);
38+
39+
return $next($data);
40+
}
41+
42+
try {
43+
$imagic_policy = file_get_contents('/etc/ImageMagick-6/policy.xml');
44+
if (1 === preg_match('/<policy domain="coder" rights="none" pattern="PDF"/', $imagic_policy)) {
45+
$data[] = DiagnosticData::warn('Imagick is not allowed to create thumbs for pdf files.', self::class,
46+
['Verify that the /etc/ImageMagick-6/policy.xml file contains the line <policy domain="coder" rights="read|write" pattern="PDF"/> .']);
47+
}
48+
} catch (FilesystemException) {
49+
$data[] = DiagnosticData::warn('Could not determine whether Imagick is allowed to work with pdf files.', self::class, [
50+
'Make sure to have the policy.xml file in `/etc/ImageMagick-6/policy.xml`.',
51+
'Verify that the file contains the line <policy domain="coder" rights="read|write" pattern="PDF"/> .',
52+
]);
53+
} catch (PcreException) {
54+
// Just ignore.
55+
}
56+
57+
return $next($data);
58+
}
59+
}

app/Image/Files/BaseMediaFile.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ protected static function getSanitizedAcceptedRawFileExtensions(): array
272272
{
273273
if (count(self::$cachedAcceptedRawFileExtensions) === 0) {
274274
$tmp = explode('|', strtolower(Configs::getValueAsString('raw_formats')));
275+
276+
// We imagick is enabeld, then we can allow PDF files.
277+
if (Configs::hasImagick()) {
278+
$tmp[] = '.pdf';
279+
}
280+
275281
// Explode may return `false` on error
276282
// Our supported file extensions always take precedence over any
277283
// custom configured extension

app/Image/Handlers/ImagickHandler.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use App\DTO\ImageDimension;
1515
use App\Exceptions\ImageProcessingException;
1616
use App\Exceptions\MediaFileOperationException;
17+
use App\Image\Files\BaseMediaFile;
1718
use App\Image\Files\InMemoryBuffer;
1819
use Imagick;
1920

@@ -63,6 +64,12 @@ public function load(MediaFile $file): void
6364

6465
$this->im_image = new \Imagick();
6566
$this->im_image->readImageFile($input_stream);
67+
68+
// If the file is a PDF and the user has chosen to support PDF files then try to create an image from the first page
69+
if ($file->getExtension() === '.pdf' && BaseMediaFile::isSupportedOrAcceptedFileExtension($file->getExtension())) {
70+
$this->im_image->setIteratorIndex(0);
71+
}
72+
6673
$this->autoRotate();
6774
} catch (\ImagickException $e) {
6875
throw new MediaFileOperationException('Failed to load image', $e);

0 commit comments

Comments
 (0)