X Tutup
Skip to content

Commit 21f2d28

Browse files
committed
fix: create previews from first page on multi-page documents
1 parent b3902c9 commit 21f2d28

File tree

7 files changed

+66
-12
lines changed

7 files changed

+66
-12
lines changed

changelog/unreleased/41045

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Bugfix: create previews from first page
2+
3+
On multi-page documents (doc, odt, pdf etc) the preview will now be generated
4+
from the first page and no longer from the last page.
5+
6+
https://github.com/owncloud/core/pull/41045

lib/private/Preview/Bitmap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ public function isAvailable(FileInfo $file) {
8383
private function getResizedPreview($stream, $maxX, $maxY) {
8484
$bp = new Imagick();
8585

86-
$bp->readImageFile($stream);
86+
# using [0] as file name will tell Imagick to use the first page
87+
$bp->readImageFile($stream, '[0]');
8788

8889
$bp = $this->resize($bp, $maxX, $maxY);
8990

lib/private/PreviewManager.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,6 @@ protected function registerCoreProviders() {
327327
// This library adds imagick support for SVG, WMF, OpenEXR, DjVu and Graphviz:
328328

329329
if (\extension_loaded('imagick')) {
330-
$checkImagick = new \Imagick();
331-
332330
$imagickProviders = [
333331
'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => '\OC\Preview\SVG'],
334332
'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => '\OC\Preview\TIFF'],
@@ -348,12 +346,12 @@ protected function registerCoreProviders() {
348346
continue;
349347
}
350348

351-
if (\count($checkImagick->queryFormats($queryFormat)) === 1) {
349+
if (\count(\Imagick::queryFormats($queryFormat)) === 1) {
352350
$this->registerCoreProvider($class, $provider['mimetype']);
353351
}
354352
}
355353

356-
if (\count($checkImagick->queryFormats('PDF')) === 1) {
354+
if (\count(\Imagick::queryFormats('PDF')) === 1) {
357355
// Office previews are only supported if either LibreOffice or OpenOffice is installed on the server
358356
if (\OC_Helper::is_function_enabled('shell_exec')) {
359357
$officeFound = \is_string($this->config->getSystemValue('preview_libreoffice_path', null));

tests/data/testimage.odt

-20.6 KB
Binary file not shown.

tests/data/testimage.pdf

9.29 KB
Binary file not shown.

tests/lib/Preview/PDFTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* @author Olivier Paroz <owncloud@interfasys.ch>
4+
*
5+
* @copyright Copyright (c) 2018, ownCloud GmbH
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
*
20+
*/
21+
22+
namespace lib\Preview;
23+
24+
use OC\Preview\PDF;
25+
use OCP\Files\NotFoundException;
26+
use Test\Preview\Provider;
27+
28+
/**
29+
* Class SVGTest
30+
*
31+
* @group DB
32+
*
33+
* @package Test\Preview
34+
*/
35+
class PDFTest extends Provider {
36+
/**
37+
* @throws NotFoundException
38+
*/
39+
public function setUp(): void {
40+
if (\count(\Imagick::queryFormats('SVG')) === 1) {
41+
parent::setUp();
42+
43+
$fileName = 'testimage.pdf';
44+
$this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName);
45+
$this->width = 595;
46+
$this->height = 842;
47+
$this->provider = new PDF();
48+
} else {
49+
$this->markTestSkipped('No PDF provider present');
50+
}
51+
}
52+
}

tests/lib/Preview/Provider.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121

2222
namespace Test\Preview;
2323

24+
use OC\Preview\TXT;
2425
use OCP\Files\File;
2526
use OCP\Files\Node;
2627
use OCP\Preview\IProvider2;
28+
use Test\TestCase;
2729
use Test\Traits\UserTrait;
2830

29-
abstract class Provider extends \Test\TestCase {
31+
abstract class Provider extends TestCase {
3032
use UserTrait;
3133

3234
/** @var File */
@@ -92,15 +94,10 @@ public function testGetThumbnail($widthAdjustment, $heightAdjustment) {
9294
$this->maxWidth = $this->width - $widthAdjustment;
9395
$this->maxHeight = $this->height - $heightAdjustment;
9496

95-
// Testing code
96-
/*print_r("w $this->width ");
97-
print_r("h $this->height ");
98-
print_r("r $ratio ");*/
99-
10097
$preview = $this->getPreview($this->provider);
10198
// The TXT provider uses the max dimensions to create its canvas,
10299
// so the ratio will always be the one of the max dimension canvas
103-
if (!$this->provider instanceof \OC\Preview\TXT) {
100+
if (!$this->provider instanceof TXT) {
104101
$this->doesRatioMatch($preview, $ratio);
105102
}
106103
$this->doesPreviewFit($preview);

0 commit comments

Comments
 (0)
X Tutup